Friday 8 January 2016

Creating timestamped folders in Windows using batch scripting



In this blog, I am going to show you how to create a date/timestamp folder in Windows using batch scripting… This is particularly useful when you need to take periodic backups of Hyperion applications (generally LCM backups/level zero data export of Planning applications before application refresh) as all system administrators have to do sometime or the other. 

In Linux/Unix like systems, manipulating date values and time stamp values is pretty straight forward…However, in Windows, it requires some hard work since batch scripting gives the users a very basic set of features and functionalities to manipulate and work with…

The script that will create it is as shown in the below snippet..I will explain the working later…

set datevalue=temp
set timevalue=temp

echo "Displaying the temporary values here"
echo %datevalue%

echo Displaying the values of date and time here
date /t
time /t

for /f "tokens=2-4 delims=/ " %%a in ('date /t') do set datevalue=%%a_%%b_%%c
for /f "tokens=1-3 delims=: " %%a in ('time /t') do set timevalue=%%a_%%b_%%c


echo %datevalue%_%timevalue%

mkdir .\%datevalue%_%timevalue%
pause




I save this script as a batch file as shown in the below snapshot.

The set commands in batch are equivalent to assignment statements in programming languages. 

The date command will display the system date as shown in the below snapshot.

The time command will display the system time as shown in the below snapshot.



The default behavior of the date and time command is to output the system date and time respectively and then ask the user to enter a new date and time...which is weird since I would not want to do it every time…In order to overcome this, we use the /t flag which notifies the command prompt that the system is to just output the system date and time.

All the action takes place in the two for statements… The first creates a datevalue variable and the second creates a timevalue variable

The first for statement in above snapshot on line number 11 of the script can be explained as follows:-
For the second, third, fourth token (tokens=2-4) that is found in the output of ‘date /t’ command(date /t'), where delimiters are forward slash and space (delims=/ ), assign the tokens to positional parameters starting as %a, %b and so on. Set datevalue basically creates an environment variable called datevalue which now has the tokens %a, %b and %c ( Month, date and year respectively ) separated by an underscore character.

The timevalue is created similarly the only difference being that for time we take the first, second and third token and this is separated by a colon and space.

An important point to note in the above scenario is that %a and %A positional parameters are different in Windows batch scripting. This is the only exception that I have found where batch scripting is case sensitive as of now.

In the line number 17 of the script I created a date timestamp folder in the current directory from where I am running the script.

The output of running the batch file is as shown in the below snapshot.


The timestamp folder that I created is as shown below:-





1 comment: