Batch file to copy and rename files

August 4, 2009 at 13:19:58
Specs: Windows XP
I have no batch file experience, but I need a batch file that will rename & copy files from multiple directories to one single directory all on the same drive. The renamed file will add the folder name that it was copied from to the copied files name.

See More: Batch file to copy and rename files

Report •

August 4, 2009 at 16:45:44
Using the search key words > copy batch rename ( change to suit ) gets 2 pages here.

Report •

August 5, 2009 at 20:37:11
Thanks for the response. Unfortunately none of the apps offer the flexibility I need for this specific situation.

Report •

August 5, 2009 at 21:34:34

I made this little and powerfull batch that I think will fit to you.

::--------start of code-------------
@echo off
setLocal EnableDelayedExpansion
set source="c:\my documents\my ebooks"
set target="%userprofile%"\desktop\backup\
for /f "tokens=*" %%a in ('dir %source%\*.* /b /s') do (
set B=%%a
set C=!B:\=_!
set D=!C:~3,88!
copy "%%a" %target%\"!D!"
echo *** Job Done ***
::-------end of code------------------

cut out this code and copy it to your bat file.

Pay attention to the third line: set source="c:\my documents\my ebooks"

you must replace "c:\my documents\my ebooks" with your source folder.

In the fifth line the option "/s" aims to extend the job deep into the folder.
So if you state: set source="c:" it will copy the entire disk c: to the target folder (its a kind of bomb!).

if you dont wanna go deep in target folders, then let me know. I have done a alternative batch that will fix this.

finally, you must also create the target folder and replace its name in the 4th line.
(In the script above "backup" is a folder in your desktop).

Report •

Related Solutions

August 17, 2009 at 13:07:46

Thanks for the script. I updated the necessary varialbes as you directed and ran the script. It worked but I think it needs some tweaking. Unfortunately it only copied the last file in the list of files from each of the sub folders below the source. I had four sub folders with three files in each. I was expecting 12 files.

Also, can the rename append only the name of the folder in which the copied file resides instead of the entire path to the file?

The 88 character limit should be sufficient to include the name of the last folder and the file.

Finally, I am planning to user Windows "Schedule Task" to execute this batch file at least once a day if not more. Therefore, if the file already exists in the Target folder, I don't want it to re-write that file. I'd prefer that it just abort that attempt.

Let me know if you can make these adjustments. Thanks for your assistance.


Report •

August 17, 2009 at 23:42:13
Hi Mitch, I have fixed the batch. This should work, if not let know please.
(all files being copyed)
(if the file already exists in the target folder, it will not be overwritten).
(append only the name of the last path)

@echo off
setLocal EnableDelayedExpansion
set source="C:\NewsCorp\NC-DataSecurityAssessment\NewsCorp-Data Security Assessment"
set target=C:\newscorp\delloite-internal
set /a cnt=89
set /a cnt2=0
set p-copy=no
for /f "tokens=*" %%a in ('dir %source%\*.* /b /s') do (
set B=%%a
:loop (
set /a cnt=%cnt%-1
set char=!B:~%cnt%,1!
if %char%-==.- set p-copy=yes
if %char%-==\- set /a cnt2=%cnt2%+1
if %cnt2% lss 2 call :loop

set /a cnt=%cnt%+1
set W=!B:~%cnt%,88!
set C=!W:\=_!
if exist "%target%\!C!" set p-copy=no
if %p-copy%==yes copy "!B!" "%target%"\"!C!"
set /a cnt=89
set /a cnt2=0
set p-copy=no

Report •

August 18, 2009 at 06:03:27

Thanks again for the script, however, I think we need one more adjustment.

After testing several times, no files were being copied in the folders below the source. I then put a file in the source location and that was copied. There will be sub-folders in the source location and I need the files beneath those sub-folders to be copied.

On a good note, the "no overwrite" and "append only the last folder name in the path" did work.

Please try to resolve this issue.



Report •

August 18, 2009 at 12:09:30
Mitch, check if you dont have changed anything in the bat. Copy the code again and test it without any change. I do the test and it worked fine. In my test there were 4 folders below the source path and all files were properly copied.

If you dont get it working, send me the structure of the source path in a text file (private message if necessary).

dir *.* /b /s > SoucePath.txt

send me this sourcePath.txt if the script still failing after a new test.

Report •

August 19, 2009 at 04:49:53
Mitch, I found the bug: the initial width (89) stated in the bat was insufficient. Change the code, replacing 89 with 250, and 88 with 249. Your source path name is too long.

Report •

August 19, 2009 at 06:13:42

Thank you very much for your diligence with this request. The batch file is working as expected.

I have one last question. If a file that already exists is updated, would this overwrite that file, create a new version of it or is there some other outcome? Please advise.


Report •

August 19, 2009 at 07:47:35
The bat, as it is, will never overwrite any file. Updates in the contents of a file that was previously copied will take no effect in the target path when you run the bat.

Report •

August 19, 2009 at 21:44:33

Thanks for the clarification and your wonderful assistance with this batch file.


Report •

Ask Question