Scripts / Batch Files

Create a Variable With the Current Date & Time (MS-DOS)

: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:: Create a variable with the current date and time (24Hr clock)
: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
set StrDate=%d:~6,4%-%d:~0,2%-%d:~3,2%
:: Date format: YYYY-MM-DD
set StrTime=%time:~0,2%-%time:~3,2%
:: Time format: HH-MM (as : cannot be used in a file or directory name.)
set StrTime=%StrTime: =0%
:: Replace space with leading zero for single digits
set StrDateTime=%StrDate% %StrTime%
:: Combine the Date and time: e.g. 2018-04-01 09-32
: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Set a Variable to the Current Directory (MS-DOS)

: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:: Set Source Directory, Destination Drive and Destination Directory
: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for %%* in (.) do set CurrDirName=%%~nx*
:: Determine the current directory
:: nx means filename and extension only

set SrcDir=%~dp0
:: %~dp0 returns the the full path to the batch file's directory (static). ( With the trailing \ )
:: %~dp0 is the path of the batch file, which might not be the same as %cd%
:: ~dp gives you the drive and path of the specified
:: %0 is the command entered on the command line. argument.
:: %cd% returns the current working directory (variable). ( Without the trailing \ )
set DestFN=%CurrDirName% - %StrDateTime%
set DestDrv=X:
set DestDir=\Dev.backup\Projects\ProjectName
: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Leave a Comment