DOS script to rename and copy files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
patron0119
Posts: 8
Joined: 19 Feb 2013 10:32

DOS script to rename and copy files

#1 Post by patron0119 » 20 Feb 2013 11:26

Hi i'm working on the below script to rename files and after renamed will copy the file to a different target directory, but is not working. can some one revised for me? I don't know what i'm doing wrong.

@echo on
setlocal enabledelayedexpansion
for %%a in (%*) do (
set file=%%a
ren "!file!" "!file: =_!"
ren "!file!" "!file:#=_!"
ren "!file!" "!file:&=_!"
ren "!file!" "!file:@=_!"
ren "!file!" "!file:'=_!"
ren "!file!" "!file:(=_!"
ren "!file!" "!file:)=_!"
ren "!file!" "!file:-=_!"
ren "!file!" "!file:{=_!"
ren "!file!" "!file:}=_!"
ren "!file!" "!file:,=_!"
ren "!file!" "!file:;=_!"
)
setlocal disabledelayedexpansion
for %%A in (%*) do if exist %%A copy %%A Q:\Applications\InfoPrint_Manager\IPM_FTP_Process\Edge2\Temp_Folders\Offline_Staple_Temp
Q:
cd \Applications\InfoPrint_Manager\IPM_FTP_Process\Edge2\eppsprta06\Next_Files
call S06_next_Offline_Staple.bat
erase \Applications\InfoPrint_Manager\IPM_FTP_Process\Edge2\Temp_Folders\Offline_Staple_Temp\*.PDF
for %%A in (%*) do if exist %%A move %%A Q:\Applications\InfoPrint_Manager\IPM_FTP_Process\PDF_History

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

Re: DOS script to rename and copy files

#2 Post by foxidrive » 20 Feb 2013 11:59

This is untested: it expects the files given on the command line to be in the current folder.

Code: Select all

@echo on
for %%a in (%*) do call :next "%%a"
echo done...
pause
goto :EOF

:next
set "file=%~1"
set "file=%file: =_%"
set "file=%file:#=_%"
set "file=%file:&=_%"
set "file=%file:@=_%"
set "file=%file:'=_%"
set "file=%file:(=_%"
set "file=%file:)=_%"
set "file=%file:-=_%"
set "file=%file:{=_%"
set "file=%file:}=_%"
set "file=%file:,=_%"
set "file=%file:;=_%"

ren "%~1" "%file%"
copy /b "%file%" "Q:\Applications\InfoPrint_Manager\IPM_FTP_Process\Edge2\Temp_Folders\Offline_Staple_Temp"
pushd "Q:\Applications\InfoPrint_Manager\IPM_FTP_Process\Edge2\eppsprta06\Next_Files"
call S06_next_Offline_Staple.bat
popd
del "Q:\Applications\InfoPrint_Manager\IPM_FTP_Process\Edge2\Temp_Folders\Offline_Staple_Temp\*.PDF"
move "%file%" "Q:\Applications\InfoPrint_Manager\IPM_FTP_Process\PDF_History"

patron0119
Posts: 8
Joined: 19 Feb 2013 10:32

Re: DOS script to rename and copy files

#3 Post by patron0119 » 21 Feb 2013 12:39

I got the following error:

C:\Users\marionh\Desktop\Staging2>ren "C:\Users\marionh\Desktop\Staging2\2D@renametest.pdf" "C:\Users\marionh\Desktop\Staging2\2D_renametest.pdf"
The syntax of the command is incorrect.

I don't understand why, because is executing the rename from 2D@renametest.pdf to 2D_renametest.pdf as expected.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: DOS script to rename and copy files

#4 Post by abc0502 » 21 Feb 2013 12:46

rename or ren commands doesn't take the full path in the second part, it must be just the file name only not the path + the new file name ( try ren /? in the CMD window ).

patron0119
Posts: 8
Joined: 19 Feb 2013 10:32

Re: DOS script to rename and copy files

#5 Post by patron0119 » 21 Feb 2013 13:08

I tried that, but it didn't work. On the run execution of the bat file I'm able to see the rename from the old file name to the new file name. It has to be something on the ren "%~1" "%file%" instruction.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: DOS script to rename and copy files

#6 Post by abc0502 » 21 Feb 2013 13:18

both "%~1" and %file% are a full path (drive letter + folder + file name),
In rename or ren commands, the 2nd part where you enter the new file name must only have new file name.

if you want to keep your code as it is, you can add this for command to extract the file name from the %file% variable after the last "set "file=%file:;=_%"" command

Code: Select all

For /F "delims=" %%A ("%file%") Do SET "FileName=%%~nxA"


then change the ren command to:

Code: Select all

ren "%~1" "%FileName%"


try it, i didn't test it but it should work

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

Re: DOS script to rename and copy files

#7 Post by Squashman » 21 Feb 2013 13:19

set "file=%~nx1"

patron0119
Posts: 8
Joined: 19 Feb 2013 10:32

Re: DOS script to rename and copy files

#8 Post by patron0119 » 21 Feb 2013 15:14

Squashman, your response did the trick. It worked just as expected. Guys thank you so much for your help on this important matter.

patron0119
Posts: 8
Joined: 19 Feb 2013 10:32

Re: DOS script to rename and copy files

#9 Post by patron0119 » 21 Feb 2013 15:24

Sorry can't forget Foxydrive you corrected the biggest portion of the coding.

patron0119
Posts: 8
Joined: 19 Feb 2013 10:32

Re: DOS script to rename and copy files

#10 Post by patron0119 » 21 Feb 2013 15:26

Sorry for the bad spelling, foxidrive.

Post Reply