Add Date-Time Stamp To Copied File using Windows Batch

0

I thought this would be simple but it seems more of a challenge than I thought.

I want a file to be copied and pasted with a time stamp appended. I get the time stamp using this code:

set hournum=%time:~0,2%

if %hournum% gtr 9 (set timeback=_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%h%time:~3,2%m%time:~6,2%s) else (set timeback=_%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%h%time:~3,2%m%time:~6,2%s)

Set FileAffected=%1
echo %FileAffected:~0,-1%%timeback%

This would make the file, FILE.TXT be FILE.TXT_2015-03-05_11h56m32s

But I obviously want it to be FILE_20150305_11h56m32s.TXT

The file extension could be any extension or any length. Thanks for any help.

EDIT: FileAffected is passed with quotes, so I just stripped them with the ~0,-1.

I did find that if I do this:

FOR /f %%i IN (%FileAffected%) DO (
ECHO filename=%%~fi
ECHO fileextension=%%~xi
)

If I echo FileAffected I get the full path name with spaces. But if I use the FOR /F command, the filename comes back truncating anything after a space. Even adding extra quotes to the "%FileAffected" variable in the "IN" command, still doesn't fix it, nor does adding quotes around the fi or xi.

HTWingNut

Posted 2015-03-15T16:03:54.883

Reputation: 1

Answers

1

How do I add the date and time as a suffix to a filename?

Use the following batch file:

@echo off
set hournum=%time:~0,2%
if %hournum% gtr 9 (set timeback=_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%h%time:~3,2%m%time:~6,2%s) else (set timeback=_%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%h%time:~3,2%m%time:~6,2%s)
echo %~n1%timeback%%~x1

Output:

F:\test>test file.txt
file_3/01_18h07m40s.txt

If your filename contains spaces, for example file with space.txt then you need to call the batch file with "s around the filename, as follows:

F:\test>test "file with space.txt"
file with space_3/01_18h07m56s.txt

Parameter Extensions

%~n1 Expand %1 to a file Name without file extension C:\utils\MyFile or if only a path is present (with no trailing backslash) - the last folder in that path.

%~x1 Expand %1 to a file eXtension only - .txt

Source Command Line arguments (Parameters)


Using "Double Quotes

If a single parameter contains spaces, you can still pass it as one item by surrounding in "quotes" - this works well for long filenames.

If a parameter is used to supply a filename like this:

MyBatch.cmd "C:\Program Files\My Data File.txt"

This parameters will be:

%0 = MyBatch

%1 = "C:\Program Files\My Data File.txt"

Source Syntax : Escape Characters, Delimiters and Quotes


Further Reading

DavidPostill

Posted 2015-03-15T16:03:54.883

Reputation: 118 938

Note: %timeback% does not include the year for me. I get _3/01_18h31m52s on my system, where %date% is 15/03/2015 – DavidPostill – 2015-03-15T17:33:48.163

Thank you for taking the time to answer. I will have to trial this later when I get a chance. Weird because I have this same "%timeback%" variable with another batch giving me year. I'll have to look deeper. – HTWingNut – 2015-03-26T00:18:51.667

Thank you for taking the time to answer. I will have to trial this later when I get a chance. I guess your date format is dd/mm/yyyy where in good ol' USA where we do everything backwards it's mm/dd/yyyy. Will have to ponder that one for a bit. http://i.imgur.com/WIKf6TE.jpg

– HTWingNut – 2015-03-26T00:26:21.813