DOS script to rename and copy files
Moderator: DosItHelp
-
- Posts: 8
- Joined: 19 Feb 2013 10:32
DOS script to rename and copy files
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
@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
Re: DOS script to rename and copy files
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"
-
- Posts: 8
- Joined: 19 Feb 2013 10:32
Re: DOS script to rename and copy files
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.
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.
Re: DOS script to rename and copy files
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 ).
-
- Posts: 8
- Joined: 19 Feb 2013 10:32
Re: DOS script to rename and copy files
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.
Re: DOS script to rename and copy files
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
then change the ren command to:
try it, i didn't test it but it should work
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
Re: DOS script to rename and copy files
set "file=%~nx1"
-
- Posts: 8
- Joined: 19 Feb 2013 10:32
Re: DOS script to rename and copy files
Squashman, your response did the trick. It worked just as expected. Guys thank you so much for your help on this important matter.
-
- Posts: 8
- Joined: 19 Feb 2013 10:32
Re: DOS script to rename and copy files
Sorry can't forget Foxydrive you corrected the biggest portion of the coding.
-
- Posts: 8
- Joined: 19 Feb 2013 10:32
Re: DOS script to rename and copy files
Sorry for the bad spelling, foxidrive.