0

I found the base code for this here already and was using it to move and then rename a file where I knew the file name. However, now I would like to move a file where the file name is a parameter.

When I run this code it renames the folder with the current date and time. How do I get it to rename the variable file name in the folder? I added the pause to try and troubleshoot what was happening.

Echo Off
SET SiteName=%~1
SET FileDate=%~2
SET File=%SiteName%%FileDate%  


IF Not EXIST C:\Users\Administrator\Documents\~Withdrawal_Report\%File%  GOTO DoNothing

copy C:\Users\Administrator\Documents\~Withdrawal_Report\%File%    C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File% 


ren C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File% %SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.csv
ren C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File% %SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.csv


del C:\Users\Administrator\Documents\~Withdrawal_Report\%File%
pause

:DoNothing
Exit
  • You are trying to rename the same file twice, which of course cannot work (upon the first renaming, the source path does no longer exist); so what do you actually want to do? – aschipfl Nov 25 '15 at 21:10

1 Answers1

0

Try to use double quotes, like this:

Echo Off
SET "SiteName=%~1"
SET "FileDate=%~2"
SET "File=%SiteName%%FileDate%"


IF Not EXIST "C:\Users\Administrator\Documents\~Withdrawal_Report\%File%" GOTO DoNothing

copy "C:\Users\Administrator\Documents\~Withdrawal_Report\%File%" "C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File%" 

ren "C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File%" "%SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.csv"
ren "C:\Users\Administrator\Documents\~Withdrawal_Report_Archive\%File%" "%SiteName%-%date:~4,2%-%date:~7,2%-%date:~10,4%_@_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.csv"


del "C:\Users\Administrator\Documents\~Withdrawal_Report\%File%"
pause

:DoNothing
Exit

Also, you're trying to rename the same file twice, as far as I can tell. Did that work before?

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • You should also use `""` around the `set` expressions like `set "SiteName=%~1"`, because otherwise, several special characters in the arguments may cause trouble... – aschipfl Nov 25 '15 at 21:11
  • Thank you Dennis and aschipfl! Adding the quotes around the entire file path worked. The whole renaming of the file twice was for the single digit/double digit (Hour) timestamp. One of the renames will fail it is is a single digit hour, and vice versa for a double digit hour. This is the base code, where it is explained. http://stackoverflow.com/questions/4984391/cmd-line-rename-file-with-date-and-time – Jordan Reed Dec 01 '15 at 13:51