I need to join two binary files with a *.bat
script on Windows.
How can I achieve that?
If you have control over the machine where you're doing your work, I highly recommend installing GnuWin32. Just "Download All" and let the wget program retrieve all the packages. You will then have access to cat, grep, find, gzip, tar, less, and hundreds of others.
GnuWin32 is one of the first things I install on a new Windows box.
In Windows 10's Redstone 1 release, the Windows added a real Linux subsystem for the NTOS kernel. I think originally it was intended to support Android apps, and maybe docker type scenarios. Microsoft partnered with Canonical and added an actual native bash shell. Also, you can use the apt package manager to get many Ubuntu packages. For example, you can do apt-get gcc to install the GCC tool chain as you would on a Linux box.
If such a thing existed while I was in university, I think I could have done most of my Unix programming assignments in the native Windows bash shell.
If you have to use a batch script and have python installed here is a polygot answer in batch and python:
1>2# : ^
'''
@echo off
python "%~nx0" " %~nx1" "%~nx2" "%~nx3"
exit /b
rem ^
'''
import sys
import os
sys.argv = [argv.strip() for argv in sys.argv]
if len(sys.argv) != 4:
sys.exit(1)
_, file_one, file_two, out_file = sys.argv
for file_name in [file_one, file_two]:
if not os.path.isfile(file_name):
print "Can't find: {0}".format(file_name)
sys.exit(1)
if os.path.isfile(out_file):
print "Output file exists and will be overwritten"
with open(out_file, "wb") as out:
with open(file_one, "rb") as f1:
out.write(f1.read())
with open(file_two, "rb") as f2:
out.write(f2.read())
If saved as join.bat usage would be:
join.bat file_one.bin file_two.bin out_file.bin
Thanks too this answer for the inspiration.
I try to rejoin tar archive which has been splitted in a Linux server.
And I found if I use type
in Windows's cmd.exe
, it will causes the file being joined in wrong order.(i.e. type
sometimes will puts XXXX.ad at first and then XXXX.ac , XXXX.aa etc ...)
So, I found a tool named bat
in GitHub https://github.com/sharkdp/bat which has a Windows build, and has better code highlight and the important thing is, it works fine on Windows to rejoin tar archive!
So i was looking for a similar solution with the abillity to preserve EOL chars and found out there was no way, so i do what i do best and made my own utillity This is a native cat executable for windows - https://mega.nz/#!6AVgwQhL!qJ1sxx-tLtpBkPIUx__iQDGKAIfmb21GHLFerhNoaWk
Usage: cat file1 file2 file3 file4 -o output.txt
-o | Specifies the next arg is the output, we must use this rather than ">>" to preserve the line endings
I call it sharp-cat as its built with C#, feel free to scan with an antivirus and source code will be made available at request
type
can replace only part ofcat
's functionality (due to distinction between binary and text files on Windows). – jfscat
does more than just concatenate files; another function it performs is copy stdin to stdout (when called with no arguments). See this question for how to achieve that on Windows: stackoverflow.com/q/52330841 – Simon Kissane