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).
Related
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"
I need to rename a group of .pdf files
In the \Source folder I have the files:
bbbbbbbbb-56.pdf
vduysdvss-60.pdf
sdvbdsbvv-80.pdf
I have to rename them in the \Destination folder like this:
11111111-bbbbbbbbb-ggg-hhh-56-dddd.pdf
11111111-vduysdvss-ggg-hhh-60-dddd.pdf
11111111-sdvbdsbvv-ggg-hhh-80-dddd.pdf
so I need to insert some fixed parts:
before the name
in the middle of the name
at the end of the name.
Using the command:
cd \Source
copy "\Source" "\Destination"
cd \Destination
ren *.pdf 11111111-?????????-ggg-hhh???-dddd.*
the result is:
11111111--56-ggg-hhh-dddd.pdf
the bbbbbbbbb string disappears
can you help me?
Thanks
By using the following command Copy the files from Source to Destination :
copy "/Source_folder" "/Destination_folder"
Go in /Destination folder
cd "/Destination_folder"
And then ren the file names by the following command :
ren *.pdf ???-new_filename_part.*
(The question mark (?) is also a wildcard, but it represents a character of the original file name. So, in the syntax, we're using three question marks, which means that the output file name will include the first three characters of the original file (which works as a unique identifier to avoid duplication)
(According to your logic you can change the new filenames by using some RegExpressions or some variables)
I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).
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!
I have a simple xcopy script that I'm running from the command line that reads a CSV file of directories and file names. I've used a very similar script with no problems before. Here is the script:
Z:\HOME\>for /f "delims=, tokens=1,2,3,4" %i in (Z:\HOME\MissingImages.csv) do
echo f | xcopy "Y:\%j\%k\%l" "C:\Horizon\%j\%k\%l" >> Z:\HOME\MissingImagesLog.txt
However, it is not copying any of the files over
Here is an entry from the log file:
Does C:\Horizon\K\00\6bef500f.IMG specify a file name
or directory name on the target
(F = file, D = directory)? f
0 File(s) copied
It's finding the images because if I change the root directory to something else the script will just populate the log file with 0 File(s) copied for all entries, so the files are there and can be seen...
Also, the Z:\ drive is on a network and not local, but again I have used a very similar script across a network without problems (it just takes longer).
I've tried different options like /i, /s, etc. but I can't seem to get it to copy any files over.
xcopy will also report 0 File(s) copied if you use forward slashes "/" in paths instead of backslashes "\", though ONLY if you've enclosed the path in quotes.
This fails with "0 File(s) copied"
xcopy "pathname1/file" pathname2\file
This fails with "Invalid number of parameters"
xcopy pathname1/file pathname2\file
This works just fine
xcopy pathname1\file pathname2\file
It asks because it doesn't know whether you want to copy to directory (to be created) or you provide the full target pathname.
This will ask:
xcopy pathname1\file.from pathname2\file.to
However, adding slash will tell that you copy to directory:
xcopy pathname1\file.from pathname2\to\
But I haven't found the way to tell explicitly that I want to copy and rename file, except
echo Y | xcopy pathname1\file.from pathname2\file.to
I played a bit with your case (with for, do and xcopy) and found out that even if it asks Does SOMEFILE specify a file name or directory name on the target (F = file, D = directory)? it is provided with f from echo and it's copied successfully. Thus, it's not a problem with file/directory specifying, but with copying through network itself.
Well, that's annoying; I found the issue. It looks like when I generated my CSV file, it put a space at the end of each line, so xcopy was looking for files that had a space after the extension.
The thing that was throwing me off was that it was finding the files, but couldn't copy them, making me think it was a network or xcopy issue.
I just ran a sed script to remove the eol spaces and the xcopy script is now working as expected.