Monday, May 25, 2020

// // 24 comments

Automation: Batch script to add today's date and time in file/folder name

Topic: Batch script to create files/folders with current (today) date and time in their name

Hi Friends,

In this post, we will see a batch script to add the current date and time in Windows files or folders' names.

This batch script is very useful especially when you need to take periodic backups of Hyperion applications, LCM exports, Essbase Level 0 data exports, Oracle database schemas, creating log files, etc. This backup process is part and parcel of any administration work including Hyperion Administration.

In Windows system, manipulating date and time value is not that straight-forward compared to Linux/Unix systems.

Concept:

As you know, most of the time following commands are used to display the current date and time in Windows system when you quickly want to check the same:

date /t
time /t

But when it comes to batch scripting where you want to use date and time values, there is a problem with these commands. 

date /t command returns the current date using the windows local system settings for the "short date format" so it is Windows-version and region-dependent. This local system 'date and time setting' is fully customizable. Each language/region has its own settings and the users can change this too. One user may configure its system to show the short date as Tue060719; while another user (even in the same Windows system) may choose 07/06/2019. So it is not really a reliable way to get the date. It becomes difficult for you to script using date /t command as the output of this command varies with Windows machines with a different regional/country settings.

To check the above, I randomly tested these commands on two different Windows systems.

On Windows Server 2012 R2 server:

Automation: Batch script to add today's date and time in file/folder name

On Windows 10 system:

Automation: Batch script to add today's date and time in file/folder name

Alright! here is the solution.

In order to overcome this issue, you should use the WMIC localdatetime command to get the date and time as this will work independent of the region setting in your Windows system/server. WMIC is the WMI command-line interface to WMI. WMIC localdatetime command provides you regionally independent date-time parsing.

I tested WMIC localdatetime command on the same set of Windows systems and got the output as shown below where we can see the output format is exactly the same in both:

On Windows Server 2012 R2 server:

Automation: Batch script to add today's date and time in file/folder name

On Windows 10 system:

Automation: Batch script to add today's date and time in file/folder name

From the above output, you can easily see that this WMIC command output needs some formatting to put the current date and time in more readable format.

Below is the batch script that formats the WMIC command output for you. You can directly use this script to add current/today date and time in your Windows files/folders.

Batch Script: Version-1

@echo off

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"

:: Format the WMIC command output in YY_MM_DD_hr_mn format
set "YY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "hr=%dt:~8,2%"
set "mn=%dt:~10,2%"

set "today_date_time=%YY%_%MM%_%DD%_%hr%_%mn%"
echo %today_date_time%

:: Create a folder in the current directory with name as today’s date and time as shown below
mkdir .\%today_date_time%

:: Append today’s date and time in your log file name created in the current directory as shown below
echo This information will be recorded in the following log file >> %today_date_time%_Output.log

Save the above code in a batch file (test.bat) as shown below:

Automation: Batch script to add today's date and time in file/folder name

Now let’s run this batch script (either from the command line or directly from the folder itself by double-clicking on test.bat).

Running this batch script will create the following two things:
  1. A log file in current directory with name: %today_date_time%_Output.log
  2. A folder in current directory with name: %today_date_time%

Automation: Batch script to add today's date and time in file/folder name

Batch Script: Version-2

If you want to display the month-name (with the first 3 characters like 'Feb') and not the month-number, use the below batch script code:

@echo off

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"

:: Format the WMIC command output in YY_MM_DD_hr_mn format
set "YY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "hr=%dt:~8,2%"
set "mn=%dt:~10,2%"

:: Format the MM (month-number) to display the month-name
if %MM%==01 set MM=Jan
if %MM%==02 set MM=Feb
if %MM%==03 set MM=Mar
if %MM%==04 set MM=Apr
if %MM%==05 set MM=May
if %MM%==06 set MM=Jun
if %MM%==07 set MM=Jul
if %MM%==08 set MM=Aug
if %MM%==09 set MM=Sep
if %MM%==10 set MM=Oct
if %MM%==11 set MM=Nov
if %MM%==12 set MM=Dec

set "today_date_time=%YY%_%MM%_%DD%_%hr%_%mn%"
echo %today_date_time%

:: Create a folder in the current directory with name as today’s date and time as shown below
mkdir .\%today_date_time%

:: Append today’s date and time in your log file name created in the current directory as shown below
echo This information will be recorded in the following log file >> %today_date_time%_Output.log

Running above batch script will generate your output in below format (with month-name in place of month-number compared to the first batch script):

Automation: Batch script to add today's date and time in file/folder name

The purpose here is that you can use the above two batch script codes in any of your batch files where you either want to create a backup folder or a log file with the current date and timestamp added in the folder or file name.

WMIC localdatetime command can be run on any Windows platform and the result will be the same. You don’t need to worry about Windows system regional settings and the variation in date and time formats. The returned value of WMIC localdatetime is always in the same format and with time in the 24-hour format.

WMIC localdatetime command works universally on all locales or date-time formats that's why it is the most reliable way to get the date in batch scripting.

That's all for this post.

I hope this article has helped you. 
Your suggestions/feedback are most welcome.
Keep learning and Have a great day!!!

24 comments:

  1. Batch Script Version 3
    :: ================================================================
    :: Execute MaxL script to export level 0 data for CAPBudDB DataBase
    :: Copy all Calc Scripts, Load Rules, Outline, etc.
    :: The code "%date:~-4,4%-%date:~-10,2%-%date:~7,2%" creates a date directory
    :: based on the system's current date.
    :: Delete backups older than 15 days.
    :: Created 04-21-2015
    :: ===============================================================
    @ECHO OFF
    SET LOGFILE=E:\Automation\Scripts\Logs\Logfile.log
    SET Path1=E:\Automation\Backups
    SET Path2=E:\Automation\Backups\CAPBud
    SET Path3=E:\Automation\bin
    SET Path4=E:\Automation\Scripts\MaxL
    SET Path5=E:\Oracle\Middleware\user_projects\epmsystem1\EssbaseServer\essbaseserver1\app\PMCOth\CAPBud
    SET Path6=E:\Automation\Backups\CAPBud\%date:~-4,4%-%date:~-10,2%-%date:~7,2% /e /c /y
    :: ------------------------------------------------------------
    ECHO Backing up the CAPBudDB %date% %time% >> %LOGFILE%
    ECHO.
    CALL %Path3%\startMaxl.cmd %Path4%\Backup_CAPBud_DB_Export.mxl
    MKDIR %Path2%\%date:~-4,4%-%date:~-10,2%-%date:~7,2%
    MOVE %Path2%\CAPBudDB.txt %Path2%\%date:~-4,4%-%date:~-10,2%-%date:~7,2%\CAPBudDB.txt
    XCOPY %Path5% %Path6%
    :: ========= Delete Backups Older than 15 Days ==========
    ECHO Deleting CAPBud Backups Older than 15 Days %date% %time% >> %LOGFILE%
    ECHO ---------------------------------------------- >> %LOGFILE%
    FORFILES -P %Path2% -D -15 -C "CMD /C IF @ISDIR == TRUE RMDIR /S /Q @path"
    ECHO.
    EXIT

    ReplyDelete
    Replies
    1. Good approach! but again 'date' command output is dependent on your local system/server 'date and time setting' which is fully customizable.

      Delete
  2. Is it possible to use 12-hour format in that script batch?

    ReplyDelete