Here is a concrete example application for your question, it might be also interesting for Windows/cygwin users. I have once created a .bat
batch script to automatically backup files by creating a subfolder named with the current datetime.
Firstly, variables are fetched from the %time%
environment, and then formatted to evantually have a leading 0
. The strings are then concatenated to a new variable %datetimef%
and passed to mkdir
:
set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
set year=%date:~-4%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
set datetimef=%year%%month%%day%-%hour%%min%%secs%
mkdir version%datetimef%
cp *.pdf version%datetimef%/
%Y-%m-%d
or%F
if it's available. That format will display chronologically in a directory listing. – glenn jackman Mar 26 '16 at 15:33