0

I want to copy a file to another file that contains the time and date in its name.

I use the statement below but the problem is for time values earlier than 10 AM (for which the hour value is only a single digit) there is a blank character instead of a leading zero, which I want.

copy  "M:\Production Schedule.xlsm" m:\gsdBackups\ProductionSchedule%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,0%.xlsm

In the example above when I run it at 9:50 I get the resulting name:

GSDProductionSchedule20180509_ 950.xlsm

I do not understand all of the formatting that is going on in the above copy command. Rather than the " 950" below I'd like to have "0950"

1 Answers1

0
  • As a recurring task this should be a batch file hiding the details.
  • use wmic or PowerShell to get date/time in a user settings /locale independent format
  • name the batch file to any name with the extension .bat or .cmd and place it in a folder which is in the path

:: Q:\Test\2018\05\09\SO_50256566.cmd
@echo off
Set "Src=M:\Production Schedule.xlsm"
Set "Dst=M:\gsdBackups"

:: Get date and time in a user settinhs/locale independent format
For /f %%Y in ('
powershell -NoP -C "(get-date).AddDays(0).ToString('yyyyMMdd_HHmm')"
') Do Set _DT=%%Y

:: get source and use for variable modifiers to get name extension separated
For %%F in ("%Src%") Do echo copy "%%~F" "%Dst%\%%~nF_%_DT%%%~xF"

Sample output:

> SO_50256566.cmd
copy "M:\Production Schedule.xlsm" "M:\gsdBackups\Production Schedule_20180509_1743.xlsm"

If it looks OK to you remove the echo in front of copy.