How do I append to a file using the COPY command - windows

I'm running Windows 7 Ultimate x64, but my experience dates back to DOS 3.0.
Since like DOS 3.1 you've been able to append a file to another one with this use of the COPY command:
COPY FILE1+FILE2=FILE1
Making the need for a temporary FILE3 unnecessary.
It was a very convenient command since whenever you added a new program you often needed to update your CONFIG.SYS and AUTOEXEC.BAT files.
It also used to be that getting the order correct was importiant, otherwise you'd end up with an empty FILE1.
But today when I tried that, it left FILE1 untouched, and when I reversed the order, it (understandably) made FILE1 a copy of FILE2.
Does anyone know if it's been replaced with another method, and when this change happened?
EDIT:
I've been doing more testing, and oddly even though the above code won't work, you still can sill copy from the console and append that to an existing file like this:
copy file1+con=file1
Type some text to append to file1
^Z ([CTRL]+Z the End Of File character)
I'm wondering if my version of Windows is messed up somehow. Can any body replicate my findings?
EDIT:
It works on 95 / 98 / ME / 2000 / XP / XP Mode / 7 Professional x64 / 8 x64. So I imagine that it's not a 7 Ultimate x64 problem, but rather an issue with my machine.
* Sigh *
EDIT:
Last edit, I promise. :)
It was not an issue with my machine, it was an issue with File1. Apparently when I first appended File2 to it, the [CTRL]+Z (EOF character) never got overwritten, causing the file to look like this:
Original Data
Original Data
[EOF]
Appended Data
Appended Data
Appended Data
You can duplicate this yourself with the following experiment from at the command prompt. (Where ^Z is the character [CTRL]+Z )
At the command prompt type:
copy con file1
File One
^Z^Z
copy con file2
File Two
^Z
copy con file3
File Three
^Z
copy file1+file2=file1
copy file2+file3=file2
TYPE file1
TYPE file2
You will see:
file1
File One
file2
File Two
File Three
You can type file2 >> file1 or use nearly any other method of concatenating files, and when you type file1 it will still only appear to contain File One. BUT if you use FIND "searchterm" file to parse the file it will show you what's REALLY going on. In this case type:
FIND " " file1
And you will be rewarded with:
---------- FILE1
File One
→File Two

Windows 8 x86:
Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.
C:\Users\Nikos>echo foo>file1
C:\Users\Nikos>echo bar>file2
C:\Users\Nikos>copy /b file1 + file2 file1
file1
file2
1 file(s) copied.
C:\Users\Nikos>type file1
foo
bar

What about type file2 >> file1

Answer to: "How do I append to a file using the COPY command"
WARNING: If you have a list of files you wish to combine via COPY command, it's simple but can potentially destroy your files.
Dangerous Way:
copy /b one + two + three -- will append contents of "two" and "three" to the file "one" . So the original "one" now has contents of 3 files in proper sequence. If during copy-process things go wrong, you'll have no way of recovering original "one", as it will be corrupt and your data would essentially be lost. There's almost Never a reason to use this way.
Safe Way:
copy /b one + two new_filename -- will combine 2 files (you can list more than two of course), creating a new_filename containing "one" and "two" in proper sequence, and leaving original files intact.

make sure you start with fresh files you never tried to copy over.
I just found that on my (XP sp3) copy a+b a without /b appends 1A (SUB) to the end of the file which makes anything after it disappear from output of type (but more will show it). Copy /b a+b a works.

#echo off
cls
type "file2.txt" >> "file1.txt"
exit

Did you try copy /b file1 + file2 file1

copy /b input1 + input2 output
del input1
ren output input1
Maybe this? :P

C:\Users\Nikos>type file1
foo
bar
C:\Users\Nikos>copy file1+con=file1
file1
con
ihdui
ohisd
^Z
1 file(s) copied.
C:\Users\Nikos>type file1
foo
bar
ihdui
ohisd

Wait! There is more! (Win7 Pro)
>ver
Microsoft Windows [Version 6.1.7601]
>copy file.a.txt + file.b.txt file.ab.txt
file.a.txt
1 file(s) copied
>copy fileA.txt + fileB.txt fileAB.txt
fileA.txt
fileB.txt
1 files(s) copied
Putting aside the "1 file(s) copied" notification, it just doesn't like files with funny names.

To copy to a binary file ...
Put all files in same folder e.g. G:\Files
g:
cd Files
copy /B *.* TargetFilename.ext
and .ext is the type of file required

echo . 2>new.mp3
rem create a new empty.
for /f "delims=" %%i in ('dir /b *.mp3') do copy /b "%cd%\new.mp3"+"%cd%\%%i.mp3" %cd%\new.mp3
rem loop merge mp3 to new.mp3
rem or you can edit a list to merge files
for /f "delims=" %%i in (list.txt) do copy /b "%cd%\new.mp3"+"%cd%\%%i.mp3" %cd%\new.mp3

Put all the files in the same folder.
copy *.* ´target.ext'
????
Profit!

Related

Is there a way to extract/replace just the last folder in the windows batch file path variable %1? trying to use it as part of the file name

Seems a good place as any to ask
So I want to assign a file name being processed by a batch file to:
LASTFOLDERIN%1PATHNAME-FOLDERNAME.rar using the %1 Windows sends me from the context menu.
So far I'm able to ALMOST make it, I get so far as:
WinRAR.exe a -r -ep1 -u -m0 -y "C:\Sorting\ %~p1-%~n1.rar" "%~1\*.*"
Which returns:
\FULL(DIR1\DIR2\DIR3)PATH\-FOLDERNAME.RAR
But the problem is I get the FULL path (multiple directories), not just the very last one, also, I get the \ that breaks the whole thing.
Question is, is there a way to extract just the last folder in the path variable %~1 that I get from the windows context menu argument?
Or if not, can I somehow replace the \ with - so I can rename the files manually later? to get something like this: -DIR1-DIR2-DIR3-FOLDERNAME.RAR
Any help would be greatly appreciated, I'm stuck here.
PS I'm sending %V to the batch file, since %1 did all kinds of weird things.
In your comment you asked: "Now if anyone knows how to get the last 2 directory variables instead of just the last one... it would be awesome haha."
My answer was: "Simple: after set "PATHNAME=%1" use set "last1=%PATHNAME:\=" & set "last2=!last1!" & set "last1=%" & set "lastTwo=!last2!\!last1!" The result is in %lastTwo%"
I think this is fairly simple code (just two lines). Anyway, here it is such a code, with the mod required to also get the third-to-last directory:
#echo off
setlocal EnableDelayedExpansion
set "PATHNAME=%1"
set "last1=%PATHNAME:\=" & set "last3=!last2!" & set "last2=!last1!" & set "last1=%"
echo Full path name: %PATHNAME%
echo Last dir only: %last1%
echo From 2nd to last: %last2%\%last1%
echo From 3rd to last: %last3%\%last2%\%last1%
Output example:
Full path name: DIR1\DIR2\DIR3\DIR4\DIR5
Last dir only: DIR5
From 2nd to last: DIR4\DIR5
From 3rd to last: DIR3\DIR4\DIR5
ok I've found something that is definitely not elegant but does the job, if anyone can improve by all means, this is the best I could do and it gets the job done, here's the code for the batch files:
(Thanks to Squashman for the working code for spaces and recursive dirs)
For last dir only
for %%G in ("%~dp1\.") do set "PATHNAME=%%~nxG"
"WinRAR.exe" a -r -ep1 -u -m0 -y "C:\[Sorting]\%PATHNAME%-%~n1.rar" "%~1\*.*"
Returns: LASTPATHDIR-SELECTEDDIR.RAR file in a specific folder, id wish I could add this to the drag and drop handlers but alas I cant only DLLS can be there, so this is the next best thing, I just change the batch file whenever I need it stored somewhere else, you can call a path variable instead on the batch file and just update it whenever you need it changed if you like, so you dont have to change all the batch files every time.
For 2nd to last + last dir:
for %%G in ("%~dp1\.") do set "PATHLAST=%%~nxG"
for %%G in ("%~dp1\..") do set "PATH2NDLAST=%%~nxG"
set PATHNAME=%PATH2NDLAST%-%PATHLAST%
ECHO Processing %PATHNAME%-%~n1.rar"
"WinRAR.exe" a -r -ep1 -u -m0 -y "C:\[Sorting]\%PATHNAME%-%~n1.rar" "%~1\*.*"
for third to last + second to last + last:
for %%G in ("%~dp1\.") do set "PATHLAST=%%~nxG"
for %%G in ("%~dp1\..") do set "PATH2NDLAST=%%~nxG"
for %%G in ("%~dp1\..\..") do set "PATH3RDLAST=%%~nxG"
set PATHNAME=%PATH3RDLAST%-%PATH2NDLAST%-%PATHLAST%
ECHO Processing %PATHNAME%-%~n1.rar"
"WinRAR.exe" a -r -ep1 -u -m0 -y "C:\[Sorting]\%PATHNAME%-%~n1.rar" "%~1\*.*"
make sure to make 3 different batch files and call them accordingly.
If anyone cares here's the accompanying code for the right click context menu.
(just make sure the archiver batch file and the winrar path is added to the path environment variable)
Windows Registry Editor Version 5.00
;--============== CR =============--
[HKEY_CLASSES_ROOT\*\shell\CRARFILE1]
#="Add to SORTING (Last)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\*\shell\CRARFILE1\command]
#="\"(ArchiverLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER1]
#="Add to SORTING (Last)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER1\command]
#="\"(ArchiverLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\*\shell\CRARFILE2]
#="Add to SORTING (2ndLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\*\shell\CRARFILE2\command]
#="\"(Archiver2ndLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER2]
#="Add to SORTING (2ndLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER2\command]
#="\"(Archiver2ndLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\*\shell\CRARFILE3]
#="Add to SORTING (3rdLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\*\shell\CRARFILE3\command]
#="\"(Archiver3rdLast).bat\" \"%V\""
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER3]
#="Add to SORTING (3rdLast)"
"Icon"="WinRAR.exe,0"
[HKEY_CLASSES_ROOT\Directory\shell\CRARFOLDER3\command]
#="\"(Archiver3rdLast).bat\" \"%V\""
Delete any entries you don't need (like if you only need the last one)
If you want to do that quickly here's a batch file to do it, it will append this path to your path variable.
#ECHO OFF
setx Path "%PATH%;C:\Program Files\WinRAR\" /m
Hope this helps others, its saved me so much work so far

Windows 10 Command Prompt rename files overwrites file names instead of prepending

I have these files in a folder:
aaa.txt
bbb.txt
ccc.txt
I want to prepend all the filenames with 1_, so I try to write this in a Windows command prompt:
rename * 1_*
Doing so I want to get this result:
1_aaa.txt
1_bbb.txt
1_ccc.txt
But instead i get this:
1_a.txt
1_b.txt
1_c.txt
Instead of prepending it is just overwriting the names from the start. According to this (https://www.computerhope.com/renamehl.htm) article that is indeed the intended behavior.
But in this (https://www.windowscentral.com/how-rename-multiple-files-bulk-windows-10) article they show an example where they are increasing the length of the first part of the filename like this:
ren nyc_*.* newYork_*.*
So that seems to be similar to what I want to do, but when I try that exact example it does not work like that. Again, it just overwrites the first part the name without adding anything, and then I end up with nyc_(1).jpg becoming newYork_.jpg (the unique number is overwritten).
Is the second article plain wrong? How do I simply prepend something to a bunch of files with a batch line?
One method is to knock up a temporary batch file to do your renaming.
First, dump your directory to a text file with formatting turned off:
dir *.txt /B > list.bat
Open it in Notepad and then copy that bare listing into the likes of Excel (or if you have a text editor with powerful features you can use it.) You can then create a formula, like:
="rename " & A1 & " 1_" & A1
Which will build you a list of individual rename commands changing each file one at a time, like this:
A B
aaa.txt rename aaa.txt 1_aaa.txt
bbb.txt rename bbb.txt 1_bbb.txt
ccc.txt rename ccc.txt 1_ccc.txt
Copy that new column back into notepad and save it.
Run and discard your new batchfile and everything will be renamed over.
For your nyc to NewYork, you need a bit more work...
="rename " & A1 & " NewYork" & MID(A1,4,99)
...will strip the left three characters from the name replace them to give you:
rename nyc_(1).jpg NewYork_(1).jpg
forfiles /m *.txt /C "cmd /c rename #file 1_#file"

Windows batch - concatenate multiple text files into one

I need to create a script, which concatenates multiple text files into one.
I know it's simple to use
type *.txt > merged.txt
But the requirement is to "concatenate files from same day into file day_YYYY-DD-MM.txt" I am a Linux user and Windows batch is hell for me. It's Windows XP.
Windows type command works similarly to UNIX cat.
Example 1: Merge with file names (This will merge file1.csv & file2.csv to create concat.csv)
type file1.csv file2.csv > concat.csv
Example 2: Merge files with pattern (This will merge all files with csv extension and create concat.csv)
When using asterisk(*) to concatenate all files. Please DON'T use same extension for target file(Eg. .csv). There should be some difference in pattern else target file will also be considered in concatenation
type *.csv > concat_csv.txt
At its most basic, concatenating files from a batch file is done with 'copy'.
copy file1.txt + file2.txt + file3.txt concattedfile.txt
In Win 7, navigate to the directory where your text files are. On the command prompt use:
copy *.txt combined.txt
Where combined.txt is the name of the newly created text file.
Place all files need to copied in a separate folder, for ease place them in c drive.
Open Command Prompt - windows>type cmd>select command prompt.
You can see the default directory pointing - Ex : C:[Folder_Name]>.
Change the directory to point to the folder which you have placed files to be copied, using ' cd [Folder_Name] ' command.
After pointing to directory - type 'dir' which shows all the files present in folder, just to make sure everything at place.
Now type : 'copy *.txt [newfile_name].txt' and press enter.
Done!
All the text in individual files will be copied to [newfile_name].txt
I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.
Example 1 (files in the same folder):
copy file1.txt+file2.txt+file3.txt file123.txt
Example 2 (files in same folder):
type *.txt > combined.txt
Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):
for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
We can use normal CAT command to merge files..
D:> cat *.csv > outputs.csv
cat "input files" > "output files"
This works in PowerShell, which is the Windows preferred shell in current Windows versions, therefore it works. It is also the only version of the answers above to work with large files, where 'type' or 'copy' fails.
Try this:
#echo off
set yyyy=%date:~6,4%
set mm=%date:~3,2%
set dd=%date:~0,2%
set /p temp= "Enter the name of text file: "
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt
This code ask you to set the name of the file after "day_" where you can input the date.
If you want to name your file like the actual date you can do this:
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
You can do it using type:
type"C:\<Directory containing files>\*.txt"> merged.txt
all the files in the directory will be appendeded to the file merged.txt.
copy is definitely much faster than type - but it sometimes (with large files?) adds a SUB character at the end of the file. So, strictly speaking, it does not simply concatenate the files in the same way as cat in Unix.
So, the correct answer is to use cat - either in something like Git Bash (where it has the same syntax as in Unix), or PowerShell (where it does not).

cat command for windows

I have 4 files
sitefiles.tar.gz.splitaa
sitefiles.tar.gz.splitab
sitefiles.tar.gz.splitac
sitefiles.tar.gz.splitad
I need a command or program that works like "cat" on linux. that way I can join all the 4 files into 1 file
Windows Powershell supports cat.
However you can do it even simpler, using a normal console:
copy /b sitefiles.tar.gz.split* sitefiles.tar.gz
This will append all files into sitefiles.tar.gz.
If you want to join specific files, or if you want to append them in a specific order (default is sorting the filenames alphabetical) append them using +:
copy /b sitefiles.tar.gz.splitaa+sitefiles.tar.gz.splitab+sitefiles.tar.gz.splitac+sitefiles.tar.gz.splitad sitefiles.tar.gz

Creating a blank file from a batch file

I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial blank line. What else I can use to a file starting from the first line?
You just want to create a completely empty file? This will do it:
copy /y nul donald.txt
If you're looking to write something to first line, you want something like this:
echo Hello, World > donald.txt
One of the options is
#echo off
rem Don't destroy an existing file
if exist testfile goto _nocreate
:: Create the zero-byte file
type nul>testfile
:_nocreate
Originally from http://www.netikka.net/tsneti/info/tscmd041.htm which contains some additional methods.
In old MS-DOS days I used this method to create an empty file:
rem > empty.txt
In new Windows this no longer works, but rem may be replaced by any command that show nothing. For example:
cd . > empty.txt
If you're using PowerShell:
function touch {set-content -Path ($args[0]) -Value ($null) }
touch file-name
Source: http://blog.lab49.com/archives/249.
That's because echo. inserts a blank line in your file.
Try using the same code, but without the period appended to the end of the echo statement:
echo "Test string" > donald.txt

Resources