FileCopy like in DOS: copy file1 + file2 mergedfile Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
newbieforever
Posts: 460
Joined: 24 Aug 2016, 03:34

FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 05:23

In a batch script the copy command allows the following usage:

copy /b first.bin + /b second.bin /b merged.bin

Is it possible to do something like this* in AHK?**



* I.e. to concatenate binary and/or text files, but potentially with unicode characters in file names.
** Not by using DOS commands in the AHK script; not by reading file's content and using FileAppend...
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 05:41

Try this:[list] [*]only use FileAppend[/list]
[code]FileRead, bin_First, first.bin
FileRead, bin_Second, second.bin

FileDelete, merged.bin
FileAppend, %bin_First%, merged.bin
FileAppend, %bin_Second%, merged.bin
[/code]I hope that helps.

alternative: FileCopy for first component.
alternative: use FileObject (most useful for unicode names / binary content)
Last edited by wolf_II on 17 Jun 2018, 05:45, edited 1 time in total.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 05:44

Have you edited in the meantime? IDK, maybe my brain is too weak to remember.

In any case, use a FileObject.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileCopy like in DOS: copy file1 + file2 mergedfile  Topic is solved

17 Jun 2018, 05:59

Try this:

Code: Select all

q:: ;concatenate 2 binary files
vPath1 := A_Desktop "\first.bin"
vPath2 := A_Desktop "\second.bin"
vPathOut := A_Desktop "\merged.bin"
FileGetSize, vSize1, % vPath1
FileGetSize, vSize2, % vPath2
vSize := vSize1 + vSize2
VarSetCapacity(vData, vSize)
vOffset := 0
Loop, 2
{
	if !oFile := FileOpen(vPath%A_Index%, "r")
		return
	oFile.Pos := 0
	oFile.RawRead(&vData+vOffset, oFile.Length)
	vOffset += oFile.Length
	oFile.Close()
}

if !oFile := FileOpen(vPathOut, "w") ;empties file if it already exists
	return
oFile.RawWrite(&vData, vSize) ;appends data, advances pointer
oFile.Close()
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
newbieforever
Posts: 460
Joined: 24 Aug 2016, 03:34

Re: FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 06:49

Great, jeesweg, you are a schatz!

(And it is fast enough! I thought RawRead/Write will be pretty slow.)
Helgef
Posts: 4481
Joined: 17 Jul 2016, 01:02
Contact:

Re: FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 09:51

@ newbieforever, you should consider jeeswg's code an example for you to rewrite as a (more general) function. If you are not interested, at least you should do vData := "".

Cheers.
newbieforever
Posts: 460
Joined: 24 Aug 2016, 03:34

Re: FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 10:19

Thank you, helgef!

Or VarSetCapacity(vData, 0), isn't it so?

This should be done at the beginning or at the end of the script?
Helgef
Posts: 4481
Joined: 17 Jul 2016, 01:02
Contact:

Re: FileCopy like in DOS: copy file1 + file2 mergedfile

17 Jun 2018, 11:18

VarSetCapacity(vData, 0) is fine, do it before the subroutine returns, you need to consider it in two places. If you make a function, it is not needed, since all local (non-static) variables will be made blank, equivalent to x := "", when the function returns.

There is a difference between varsetcapacity x, 0 and x := "",
varsetcapacity wrote:For performance reasons, the memory of a variable whose capacity is less than 4096 bytes is not freed by storing an empty string in it (e.g. Var := ""). However, VarSetCapacity(Var, 0) does free it.
Cheers.

Return to “Ask For Help”

Who is online

Users browsing this forum: TablePerson22 and 37 guests