Combine files and create a single file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Danny
Posts: 15
Joined: 30 Oct 2013 22:52

Combine files and create a single file

#1 Post by Danny » 03 Jan 2014 14:31

Hi,

Below script copies the files to my backup drive with ease, now when all the text files are copied in destination folder i want to combine them and create a single text file while removing the header line the first line in the text file and then delete all the text files and preserve this combined file in the same folder and naming it on date.

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%-%MM%-%YYYY%"

for /d %%a in ("D:\Depository\picture\%dd%%mm%%yyyy%\data\*") do (
      xcopy "%%a\*.tiff"   "F:\Repository\%%~nxa\%datestamp%\download\Images\"
      xcopy "%%a\*.txt"   "F:\Repository\%%~nxa\%datestamp%\download\TEXT\"
)

zpimp
Posts: 12
Joined: 14 Jun 2013 12:58

Re: Combine files and create a single file

#2 Post by zpimp » 03 Jan 2014 16:59

to concatenate all text files in a folder in file.txt, and delete after
copy /b *.txt file.txt
del /f /q *.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Combine files and create a single file

#3 Post by foxidrive » 03 Jan 2014 17:59

Danny wrote:
when all the text files are copied in destination folder i want to combine them and create a single text file while removing the header line the first line in the text file and then delete all the text files and preserve this combined file in the same folder and naming it on date.

xcopy "%%a\*.txt" "F:\Repository\%%~nxa\%datestamp%\download\TEXT\"


Do you mean all the files that are copied by the command above? Are there any other files in the TEXT folder that need to be included in the combinedfile?

Danny
Posts: 15
Joined: 30 Oct 2013 22:52

Re: Combine files and create a single file

#4 Post by Danny » 04 Jan 2014 00:33

foxidrive wrote:Do you mean all the files that are copied by the command above? Are there any other files in the TEXT folder that need to be included in the combinedfile?


Hi,
No, there are no other types of files in the TEXT folder, Only .txt files.

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Combine files and create a single file

#5 Post by foxidrive » 04 Jan 2014 03:04

foxidrive wrote:Do you mean all the files that are copied by the command above? Are there any other files in the TEXT folder that need to be included in the combinedfile?



Will this folder be empty when the batch file runs?

"F:\Repository\%%~nxa\%datestamp%\download\TEXT\"

Danny
Posts: 15
Joined: 30 Oct 2013 22:52

Re: Combine files and create a single file

#6 Post by Danny » 04 Jan 2014 06:01

Hi,

Yes the folder is empty, infact this "F:\Repository\%%~nxa\%datestamp%\download\TEXT\" creates the TEXT folder if it does not exits and then copies from the source to destination.

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Combine files and create a single file

#7 Post by foxidrive » 04 Jan 2014 06:37

This replies on the *.txt files being less than 64 K lines long, and if any TAB characters are in the file then they will be replaced with runs of space characters.

It removes the first line of every text file, saving them all in one file.


Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%DD%-%MM%-%YYYY%"

for /d %%a in ("D:\Depository\picture\%dd%%mm%%yyyy%\data\*") do (
      xcopy "%%a\*.tiff"   "F:\Repository\%%~nxa\%datestamp%\download\Images\"
      md  "F:\Repository\%%~nxa\%datestamp%\download\TEXT\" 2>nul
         for %%b in ("%%a\*.txt") do more +1 "%%b" >>"F:\Repository\%%~nxa\%datestamp%\download\TEXT\%datestamp%.txt"
)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Combine files and create a single file

#8 Post by Squashman » 04 Jan 2014 11:13

zpimp wrote:to concatenate all text files in a folder in file.txt, and delete after
copy /b *.txt file.txt
del /f /q *.txt

cyclical copy. It will never end because it will keep trying to copy itself and then of course your code would then delete all your hard work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Combine files and create a single file

#9 Post by foxidrive » 04 Jan 2014 21:37

Squashman wrote:
zpimp wrote:to concatenate all text files in a folder in file.txt, and delete after
copy /b *.txt file.txt
del /f /q *.txt

cyclical copy. It will never end because it will keep trying to copy itself and then of course your code would then delete all your hard work.


It will end, but you'll get more than you bargained for in the file - and *then* it will delete everything. :D

Post Reply