AutoHotkey: Create .bat file to combine file parts

From TotalcmdWiki
Jump to: navigation, search

This script creates a batch file that can be used to combine file parts created using File - Split File.


  • create a button in the Button Bar or a Start menu entry pointing to this script
  • enter "%L" in the Parameters field
  • use by selecting file parts file.001 - file.nnn (do not forget to sort in ascending order) and clicking on the script button (menu entry)
; Check number of parameters
; %0% is the number of parameters
; In "If" it must be referenced as 0, not %0%
IfNotEqual, 0, 1
	MsgBox, Error: Exactly one parameter "`%L" required.`n`nNumber of parameters passed: %0%`n`nExiting.

; Check if listfile exists
IfNotExist, %1%
	MsgBox, Error: List file "%1%" not found.`n`nExiting.

; Check if default output file does not already exist
FileReadLine, FirstFilePart, %1%, 1
StringGetPos, DotPos, FirstFilePart, ., R
StringLeft, OutputFile, FirstFilePart, DotPos
CrcFile = %OutputFile%.crc
OutputFile = %OutputFile%.bat
IfExist, %OutputFile%
	MsgBox, Error: Output file "%OutputFile%" already exists.`n`nExiting.

; Check if .crc file exists (to retrieve original filename)
IfNotExist, %CrcFile%
	MsgBox, Error: CRC file "%CrcFile%" not found (needed to know original filename).`n`nExiting.

; Create combine command string
CombineString = @copy /b%A_Space%
Loop, Read, %1%
	StringGetPos, BackslashPos, A_LoopReadLine, \, R
	StringTrimLeft, CurrFileName, A_LoopReadLine, BackSlashPos + 1
	CombineString = %CombineString% "%CurrFileName%" +
StringTrimRight, CombineString, CombineString, 1

; Get original filename
FileReadLine, OrigName, %CrcFile%, 1
StringTrimLeft, OrigName, OrigName, 9

CombineString = %CombineString%"%OrigName%"

; Write .bat file
FileAppend, %CombineString%, %OutputFile%

A compiled .exe version of this script is also available: batchcombine.exe (185472 bytes)

Back to AutoHotkey