Computing.Net > Forums > Windows XP > Batch file to copy and rename files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to copy and rename files

Reply to Message Icon

Name: Mitch
Date: August 4, 2009 at 13:19:58 Pacific
OS: Windows XP
Subcategory: General
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Johnw
Date: August 4, 2009 at 16:45:44 Pacific
Reply:

Using the search key words > copy batch rename ( change to suit ) gets 2 pages here.

http://primewares.com/links.php?sea...


0

Response Number 2
Name: Mitch
Date: August 5, 2009 at 20:37:11 Pacific
Reply:

Thanks for the response. Unfortunately none of the apps offer the flexibility I need for this specific situation.


0

Response Number 3
Name: ricardo647
Date: August 5, 2009 at 21:34:34 Pacific
Reply:


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!"
)
cls
echo *** Job Done ***
pause
::-------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).


0

Response Number 4
Name: Mitch
Date: August 17, 2009 at 13:07:46 Pacific
Reply:

Ricardo,

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.

Mitch


0

Response Number 5
Name: ricardo647
Date: August 17, 2009 at 23:42:13 Pacific
Reply:

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
)
)


0

Related Posts

See More



Response Number 6
Name: Mitch
Date: August 18, 2009 at 06:03:27 Pacific
Reply:

Ricardo,

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.

Thanks,

Mitch


0

Response Number 7
Name: ricardo647
Date: August 18, 2009 at 12:09:30 Pacific
Reply:

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.


0

Response Number 8
Name: ricardo647
Date: August 19, 2009 at 04:49:53 Pacific
Reply:

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.


0

Response Number 9
Name: Mitch
Date: August 19, 2009 at 06:13:42 Pacific
Reply:

Ricardo,

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.

Mitch


0

Response Number 10
Name: ricardo647
Date: August 19, 2009 at 07:47:35 Pacific
Reply:

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.


0

Response Number 11
Name: Mitch
Date: August 19, 2009 at 21:44:33 Pacific
Reply:

Ricaardo,

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

Mitch


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: Batch file to copy and rename files

Mass Copy And Rename www.computing.net/answers/windows-xp/mass-copy-and-rename/158680.html

copy and rename www.computing.net/answers/windows-xp/copy-and-rename/166392.html

files unable to copy AND mmc unable to l www.computing.net/answers/windows-xp/files-unable-to-copy-and-mmc-unable-to-l/28757.html