# Solved: Batch file to concatenate then rename files



## nesr (Nov 5, 2008)

Dear Professionals
I have some climatic data files of two extensions *.PEN and *.CLI
Each directory contains up to hundred files of each extension type
Also there is a file named ???.LST, where the ??? is the brief name of the country.
I concatenated the PEN and the CLI files as follows:

```
copy *.CLI C:\tmp\All.cli
copy *.Pen C:\tmp\All.pen
copy *.LST C:\tmp\
```
as you see, the resultant file is named All.cli or All.pen
I want these files to be named like the name of the .LST file!!
for Example if it is named ABC.LST, so I want the resultant files to be
ABC.cli
ABC.pen
ABC.LST
that's because I run this batch file in many directories so that I ahave to rename the (all.*) files manually otherwise it will be overwritten

any help?


----------



## TheOutcaste (Aug 8, 2007)

Not sure of your complete folder structure, or if you want to run this on all folders at once.
I created a Test and Test1 folder.
Test contains 5 folders named Folder1-Folder5 (the name can be anything, can be any number of folders)
Created in each of these folders various *.cli and *.pen files and one file ???.LST, like USA.LST and MEX.LST

This code will go through all folders under *C:\Test*, concatenate all the .cli and .pen files naming them with the base name of the .lst file, and put them all into the *Test1\tmp* folder.
So *_root* is the folder that contains all the folders that have the files
* _Out* is where the final files will be output.
Change these two values as needed.
It does not change the original files.

If you just want to run the batch file in each folder one at a time, just use the lines in red.

```
[COLOR=Red]@Echo Off[/COLOR]
Set _root=C:\Test
[COLOR=Red]Set _Out=C:\Test1\tmp[/COLOR]
[COLOR=Red] If NOT Exist %_Out% MD %_Out%[/COLOR]
For /D %%A In ("%_root%\*") Do (
PushD %%~fA
[COLOR=Red]For %%I In ("*.lst") Do (
Copy *.CLI "%_Out%\%%~nI.cli"
Copy *.PEN "%_Out%\%%~nI.pen"
Copy %%I "%_Out%\%%I"
)[/COLOR]
PopD
)
```
Best to test on sample data first, though this doesn't modify the original files.

HTH

Jerry


----------



## nesr (Nov 5, 2008)

Thank you for you speedy reply
I tried the code you,kindly, submitted
unfortunately, it did not work!
ٍِas you said you didn't know the directory structure of my files
so I attached it on the picture, where:
1 is the source, 
2 subfolders, 
3 sample subfolder content.
you can see the sample of Morocco, where the brief name of it is MOR
the Merge.bat shown is my old trial to solve the problem...
Is there a problem with long file names?
or what?


----------



## Squashman (Apr 4, 2003)

Can you do this for us. and post the file list here.

dir /a-d /b /s D:\Temp > FileList.txt

That way we can recreate your directory structure to be just like yours to test with. With that FileList.txt file we can recreate you entire directory structure with dummy files to test with.

I am not exactly sure where you want the output to go. Do you want it to remain in the orginating folder? Do you want all the old file deleted after they have been merged?


----------



## TheOutcaste (Aug 8, 2007)

nesr said:


> Is there a problem with long file names?
> or what?


Long file names are OK, it's the 2nd level of folders. I was only checking the top level.
The first example will walk through all folders under *D:\Temp* and concatenate the files in each folder that contains a **.LST* file. The files will all be put into *C:\tmp*.
One problem, if the *.LST* file names are not unique you'll end up with only the last country's data. For example, if you use MON.LST for both Monaco and Mongolia, the Mongolia data (in 2-asia?) will be overwritten by Monaco (in 6-europe?).
So, you can either leave the files in the source folder (remove the %_Out%\ part from the 1st two copy statements and remove the 3rd copy statement), or recreate the folder structure under *C:\tmp*. (use 2nd example)

You can leave the concatenated files in the same folder and not worry about it getting added in if you run the batch file again. Copy will delete the destination file if it exists before concatenating the rest.

As this will also include the *all.cli* and *all.pen* files you show in your sample, you would need to remove/rename those first. This code will rename them to *all.cli.old* and *all.pen.old*. Once all the old *all.cli* and *all.pen* files have been renamed, you can remove the two blue lines from the code.

You show 2 *.LST* files, *MOR.LST* and *MOR2.LST*.
This will create both a *MOR.CLI* and *MOR2.CLI* which will be identical, and a *MOR.PEN* and *MOR2.PEN* which will be identical as well. No harm, just extra files created, so you'll need to remove those extra *.LST* files.

Example 1

```
@Echo Off
Set _root=D:\Temp
Set _Out=C:\tmp
If NOT Exist %_Out% MD %_Out%
For /R "%_root%" %%A In (.) Do (
  PushD %%~fA
  If Exist *.LST (
  [COLOR=Blue]If Exist all.CLI Ren all.CLI all.CLI.old[/COLOR]
  [COLOR=Blue]If Exist all.PEN Ren all.PEN all.PEN.old[/COLOR]
    For %%I In ("*.LST") Do (
      Copy *.CLI "[COLOR=Red]%_Out%\[/COLOR]%%~nI.CLI" >Nul
      Copy *.PEN "[COLOR=Red]%_Out%\[/COLOR]%%~nI.PEN" >Nul
      [COLOR=Red]Copy %%I "%_Out%\%%I" >Nul[/COLOR]
      )
    )
  PopD
)
```
Example 2
This will recreate the folder structure under C:\tmp

```
@Echo Off
Setlocal EnableDelayedExpansion
Set _Root=D:\Temp
Set _OutRoot=C:\tmp
If NOT Exist %_OutRoot% MD %_OutRoot%
For /R "%_Root%" %%A In (.) Do (
  PushD %%~fA
  If Exist *.LST (
  [COLOR=Blue]If Exist all.CLI Ren all.CLI all.CLI.old[/COLOR]
  [COLOR=Blue]If Exist all.PEN Ren all.PEN all.PEN.old[/COLOR]
    For %%I In ("*.LST") Do (
    Set _OutFolder=%%~fA
    Set _OutFolder=!_OutFolder:%_Root%=!
    If NOT Exist "%_OutRoot%!_OutFolder!" MD "%_OutRoot%!_OutFolder!" 
      Copy *.CLI "%_OutRoot%!_OutFolder!\%%~nI.CLI" >Nul
      Copy *.PEN "%_OutRoot%!_OutFolder!\%%~nI.PEN" >Nul
      Copy %%I "%_OutRoot%!_OutFolder!\%%I" >Nul
      )
    )
  PopD
)
```
HTH

Jerry


----------



## nesr (Nov 5, 2008)

Squashman said:


> Can you do this for us. and post the file list here.
> 
> dir /a-d /b /s D:\Temp > FileList.txt
> 
> That way we can recreate your directory structure to be just like yours to test with. With that FileList.txt file we can recreate you entire directory structure with dummy files to test with.


Thank you every body for your care
I did what you requested and attached the actual filelist
the attached is filelist.rar where you can find inside the FileList.txt
I also attached a real country data of Mexico (Directory with its contents)...

thanks in advance


----------



## nesr (Nov 5, 2008)

TheOutcaste said:


> Long file names are OK, it's the 2nd level of folders. I was only checking the top level.
> The first example will walk through all folders under *D:\Temp* and concatenate the files in each folder that contains a **.LST* file. The files will all be put into *C:\tmp*.
> One problem, if the *.LST* file names are not unique you'll end up with only the last country's data. For example, if you use MON.LST for both Monaco and Mongolia, the Mongolia data (in 2-asia?) will be overwritten by Monaco (in 6-europe?).


Thank you very much Sir for your efforts
Both examples work fine :up::up::up:
and the problem is solved...:up:
Many thanks


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------

