0

I have my batch file which consists of the following command.

REN "H:\April2012\A04\mr_sudheendra_holla_vaderhobli.pdf.1335780379203.ver1" "mr_sudheendra_holla_vaderhobli.pdf"

But if duplicate files exist, the command will not execute. I would like my command to rename the file name to *(1).pdf and *(2).pdf etc. if there are duplicates. How can I do that?

2

1 Answer 1

0

## We can also use TIMESTAMP:##

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
Xcopy /s "D:\folder1\test.xls" "D:\folder2\test_%fullstamp%.xls"
pause

This is a simple backup method. For example, if you are modifying a file again and again (say: D:\folder1\test.xls) and it is required to save a copy of it after a number of edits (in folder : D:\folder2\test.xls). Then we can use time stamp in the file name (D:\folder2\test_%fullstamp%.xls) which will not replace the older backups.

2
  • Please supplement your answer with some explanation...
    – Attie
    May 31, 2018 at 12:26
  • This is a simple backup method. For example, if you are modifing a file again and again (say: D:\folder1\test.xls) and it is required to save a copy of it after a number of edits (in folder : D:\folder2\test.xls). Then we can use time stamp in the file name (D:\folder2\test_%fullstamp%.xls) which will not replace the older backups. Jun 4, 2018 at 8:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .