Simon Biddle
Simon Biddle

Reputation: 43

Batch file to copy file, if file exists rename file with timestamp

I have a batch file which copies a file from the network to local, and if the file exists, adds a timestamp. Actually I'm renaming the file first and then copying the file.

Echo Off
REN "C:\Project\3d_Model.dgn" "* %Date:/=% %Time::=.%.dgn"
xcopy /Y /S /i "Z:\Project\3d_Model.dgn" "C:\Project\"

The problem I have is the date is added after the file extension and looks like this:

3d_Model.dgn Sat 07122019  9.05.46.36.dgn

How can I remove the file extension and then rename the file?

Upvotes: 3

Views: 2451

Answers (3)

Mostafa Khorasani
Mostafa Khorasani

Reputation: 1

why not working %time% in

for %I in ("C:\Project\3d_Model.dgn") do ren "%~I" "%~nI %DATE:/=% %TIME::=.%%~xI"

Upvotes: 0

aschipfl
aschipfl

Reputation: 34909

Use a for loop to split the original file name, using the ~-modifiers of its meta-variable:

for %%I in ("C:\Project\3d_Model.dgn") do ren "%%~I" "%%~nI %DATE:/=% %TIME::=.%%%~xI"

To do this directly in a Command Prompt window, it looks like this:

for %I in ("C:\Project\3d_Model.dgn") do ren "%~I" "%~nI %DATE:/=% %TIME::=.%%~xI"

N. B.:
Regard that %DATE% and %TIME% return region-dependent values!

Upvotes: 0

leorleor
leorleor

Reputation: 582

How about

REN "C:\Project\3d_Model.dgn" "3d_Model%Date:/=% %Time::=.%.dgn"

Upvotes: 1

Related Questions