Loop and merge all files w the same file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Loop and merge all files w the same file name

#1 Post by daillest319 » 30 Oct 2013 12:21

I'm trying to write a bat script to loop in the folder and merge all the files with the same filename (extension as a wildcard) outputting the same filename but extension as txt for the merge file. Sometime there only 1 file so there no need to merge but i would still need it moved to the output folder. For example...

Current File:
TESTGMCS.000
TESTGMCS.001
TESTGMCS.002
TESTSMCS.000 (TESTSMCS.001 and TESTSMCS.002 does't currently exist but can some days)
TESTWMCS.000
TESTWMCS.001
TESTWMCS.002
TESTXMCS.000
TESTXMCS.001 (TESTCMCS.002 does't currently exist but can some days)
TESTCMCS.000 (TESTCMCS.001 and TESTCMCS.002 does't currently exist but can some days)

OutFiles Merged:
TESTGMCS.txt
TESTSMCS.txt
TESTWMCS.txt
TESTXMCS.txt
TESTCMCS.txt



Code: Select all

@ECHO off
SET FILE1="\\hdrive\WS Reports\TESTGMCS.*"
SET FILE2="\\hdrive\WS Reports\TESTSMCS.*"
SET FILE3="\\hdrive\WS Reports\TESTWMCS.*"
SET FILE4="\\hdrive\WS Reports\TESTXMCS.*"
SET FILE5="\\hdrive\WS Reports\TESTCMCS.*"




copy %FILE1% "\\hdrive\WS Reports\Final\TESTGMCS.txt"
copy %FILE2% "\\hdrive\WS Reports\Final\TESTSMCS.txt"
copy %FILE3% "\\hdrive\WS Reports\Final\TESTWMCS.txt"
copy %FILE4% "\\hdrive\WS Reports\Final\TESTXMCS.txt"
copy %FILE5% "\\hdrive\WS Reports\Final\TESTCMCS.txt"


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

Re: Loop and merge all files w the same file name

#2 Post by foxidrive » 30 Oct 2013 13:06

This is untested: note that it deletes the *.txt files to begin with so that old files aren't appended to.
If you don't want them deleted then you will need to ensure that there are no previous files with the same names at that location.

Code: Select all

@echo off
del "\\hdrive\WS Reports\Final\*.txt"

for %%a in (*) do (
if not exist "\\hdrive\WS Reports\Final\%%~na.txt" for %%b in ("%%~na.*") do type "%%b" >>"\\hdrive\WS Reports\Final\%%~na.txt"
)

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Loop and merge all files w the same file name

#3 Post by daillest319 » 30 Oct 2013 13:28

This works great foxidrive but there one issue that occuring. It seems to be combining the last line of the first file with the first line of the second file its merging with and so on. So the word are mixed up.

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

Re: Loop and merge all files w the same file name

#4 Post by foxidrive » 30 Oct 2013 14:22

That's a problem with your files. They don't end with a CR/LF.

See if this solves the issue.

Code: Select all

@echo off
del "\\hdrive\WS Reports\Final\*.txt"

for %%a in (*) do (
if not exist "\\hdrive\WS Reports\Final\%%~na.txt" for %%b in ("%%~na.*") do type "%%b" >>"\\hdrive\WS Reports\Final\%%~na.txt"&echo.>>"\\hdrive\WS Reports\Final\%%~na.txt"
)

daillest319
Posts: 27
Joined: 31 Jan 2012 14:45

Re: Loop and merge all files w the same file name

#5 Post by daillest319 » 30 Oct 2013 14:42

Yup that did it :D, works perfect. Your right my files done end with CR/LF. I hate to be a pain but is there any way i can use the specfic path of where the files are being stored before being merged. path being \\hdrive\WS Reports\.

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

Re: Loop and merge all files w the same file name

#6 Post by foxidrive » 30 Oct 2013 15:17

This should be the same, but makes the network drive/folder current before starting. It will abort if the network is down.

Code: Select all

@echo off
pushd "\\hdrive\WS Reports\" && (
del "Final\*.txt"
  for %%a in (*) do (
     if not exist "Final\%%~na.txt" (
        for %%b in ("%%~na.*") do (
            type "%%b" >>"Final\%%~na.txt"
            echo.>>"Final\%%~na.txt"
         )
      )
  )
popd
)

Post Reply