Saturday, June 13, 2020

// // Leave a Comment

Automation: Batch Script to delete Hyperion backup folders older than N months/days

Hi Friends,

Topic: How to delete Oracle Hyperion backup folders older than N months/days

Maintaining a backup of various applications is an important part of any Oracle Hyperion Production system. In most of the cases, we have a network drive connected and accessible from all the servers of a Hyperion Production environment where we save the backup of Hyperion application data, metadata, artifacts, LCM exports, etc. either manually or through automated scripts like Lifecycle Management (LCM) automation, FDMEE data load jobs, Maxl scripts, etc. on a daily or weekly basis.

Over the course of time, your network drive is filled with past many months’ backup folders consuming a lot of space and thus causing a requirement for network drive housekeeping. In most of the cases, maintaining last 3-6 months application backup folders actually serve the purpose.   

We too faced the similar case where we had many Hyperion application LCM backup folders over a network shared drive starting from the year 2017, which were consuming a huge amount of space raising critical space alerts. To housekeep the network drive when we tried to delete some of these LCM backup folders manually, we encountered the notorious ‘Source Path Too Long’ error to make matters worse.

Therefore, to fix this issue permanently, we have created a batch script called “Network_Housekeeper” which works perfectly fine and is based on the following logic:

1- We have a LCM automated scheduled job which adds two huge sized LCM backup folders every day to the network drive path: \\NetworkServerAddress\HyperionBackupFolder\AutomatedLCMExport with the following folder naming convention:

HYP_PROD_YYYYMMDDhhmm

Automation: Batch Script to delete Network drive folders older than N months/days

Note: To learn how you can add current date and time in your backup folders' name, kindly refer this link: Batch script to add today's date and time in file/folder name

2- The script maps the network drive path as a local drive on the server from where we trigger this script.

3- It identifies the date 4 months (120 days) before today’s date.

4- It lists out all the LCM backup folders with the name HYP_PROD_* having the oldest date folder first and compares the date of each folder with the date 4 months before the current date.

5- If the folder date is less than the date 4 months before the current date, it passes that folder name to ROBOCOPY purge command section as an input to delete that folder.

Note: ROBOCOPY purge command is used to troubleshoot ‘Source Path Too Long’ error while deleting the folder.

6- This loop continues until all the folders older than 4 months (120 days) are deleted.

7- The script saves the names of the deleted folders in a log file for your record: D:\Data\Scripts\NETWORK_HOUSEKEEPER\Deleted_Folder.log 

Network_Housekeeper Batch Script:

@echo off

REM Connect to the network drive path

pushd \\NetworkServerAddress\HyperionBackupFolder\AutomatedLCMExport

REM Set the number of days to retain the folders for. You need to change the day=-120 to the relevant number of days you want. 

set day=-120
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\*%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%yyyy%%mm%%dd%"

setlocal enabledelayedexpansion

REM List out all the folders with name HYP_PROD_* having the oldest folder first and then extract the date from each folder name 

for /f "delims=" %%a in ('dir "HYP_PROD_*" /a:d /o:d /b') do (
set "folder=%%a"
set folddate=!folder:~9,8!

REM If folder date is less than the date 4 months before the current date, trigger the 'work' section of the script

if !folddate! LSS !final! call :work
)
goto :EOF

REM Delete the folder using ROBOCOPY command route 

:work
echo !folder! >> D:\Data\Scripts\NETWORK_HOUSEKEEPER\Deleted_Folder.log
rmdir emptyfolder
mkdir emptyfolder
robocopy emptyfolder "!folder!" /purge                  
rmdir !folder!
rmdir emptyfolder

You can schedule this batch script through Windows Task Scheduler to run at any time of the day. I have tested this script on my system and it takes 80 seconds to delete a 2 GB LCM backup folder. That is quite ok! 

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!!!
Read More

Saturday, June 6, 2020

// // Leave a Comment

LCM Import timeout for HPCM artifacts:EPMLCM-13000:Service currently not available

Hi Friends,

Topic: How to fix LCM timeout issue while importing HPCM (Hyperion Profitability and Cost Management) artifacts

In this post, we will see LCM (Lifecycle Management) timeout error (EPMLCM-13000: Service currently not available) and how to fix it while we import HPCM (Hyperion Profitability and Cost Management) artifacts into any Oracle Hyperion environment.

Important Note: 
  • This post has been written and associated activities have been demonstrated on Oracle HPCM (Hyperion Profitability and Cost Management) version 11.1.2.4.127.
  • Different paths and folders' names mentioned in this blog may slightly vary for different Oracle Hyperion setups but at large it should be the same. 
  • It's very important to test any workaround/fix first in a non-Prod environment, and move/apply it to PROD only when you are fully sure about its workability. 
  • Never forget to take a complete backup of EVERYTHING before deleting or changing anything.
Issue:

In our Production environment, when we were trying to import HPCM application models, POVs etc. components using LCM, it was getting aborted after 5 minutes throwing following error in migration details page:

EPMLCM-13000: Service currently not available.  

LCM timeout issue while importing HPCM artifacts:EPMLCM-13000:Service currently not available

We tried to run the LCM import two more times, but every time it was getting failed after 5 mins.

Analysis:

When we checked SharedService_LCM.log (MIDDLEWARE_HOME/user_projects/domains/EPMSystem/servers/FoundationServices0/logs), we found following error message recorded over there:

2020-05-16T09:04:44.587-06:00] [FoundationServices2] [NOTIFICATION] [EPMLCM-13000] [oracle.EPMLCM] [tid: 815] [userId: <anonymous>] [ecid: 00j15bZ53w2EoItDwfV4CW0003po001Y3p,0:1:4:3:4:3:3:3:3:4:3:3:3:3:4:3:4:4:4:3:3:3:3:3:4:3:4:3:3:3:3:3:3:3:3:3:3:3:3:3:3:4:4:4:3:4:3:3:3:3:3:4:3:3:3:3:3:3:4:3:3:3:4:3:4:4:4:3:3:4:4:4:3:4:3:3:3:4:3:3:3:4:3:3:3:3:3:3:3:4:4:4:3:3:3:3:3:4:3:3:4:4:4:4:3:3:4:4:3:4:4:3:4:4:3:4:4:3:3:3:4:3:3:4:3:4:4:3:4:4:4:4:4:4:3:4:4:3:3:3:3:3:3:3:3:4:4:3:4:3:3:3:3:3:3:3:3:3:3:4:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:4:4:3:3:3:4:3:3:3:3:4:3:3:3:3:3:3:3:4:4:4:4:4:4:4:4:4:4:3:4:4:4:4:3:4:3:3:3:3:4:4:3:4:3:4:3:3:3:4:3:4:3:3:3:3:3:3:3:3:4:3:1:1:1] [APP: SHAREDSERVICES#11.1.2.0] Service currently not available.
[2020-05-16T09:04:44.587-06:00] [FoundationServices2] [ERROR] [EPMLCM-30052] [oracle.EPMLCM] [tid: 815] [userId: <anonymous>] [ecid: 00j15bZ53w2EoItDwfV4CW0003po001Y3p,0:1:4:3:4:3:3:3:3:4:3:3:3:3:4:3:4:4:4:3:3:3:3:3:4:3:4:3:3:3:3:3:3:3:3:3:3:3:3:3:3:4:4:4:3:4:3:3:3:3:3:4:3:3:3:3:3:3:4:3:3:3:4:3:4:4:4:3:3:4:4:4:3:4:3:3:3:4:3:3:3:4:3:3:3:3:3:3:3:4:4:4:3:3:3:3:3:4:3:3:4:4:4:4:3:3:4:4:3:4:4:3:4:4:3:4:4:3:3:3:4:3:3:4:3:4:4:3:4:4:4:4:4:4:3:4:4:3:3:3:3:3:3:3:3:4:4:3:4:3:3:3:3:3:3:3:3:3:3:4:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:3:4:4:3:3:3:4:3:3:3:3:4:3:3:3:3:3:3:3:4:4:4:4:4:4:4:4:4:4:3:4:4:4:4:3:4:3:3:3:3:4:4:3:4:3:4:3:3:3:4:3:4:3:3:3:3:3:3:3:3:4:3:1:1:1] [APP: SHAREDSERVICES#11.1.2.0] [SRC_CLASS: ?] [SRC_METHOD: ?:?] Failed to connect to "HPCMPRD" while performing import for application. Received status code - "503" with error message - "Weblogic Bridge Message Failure of server APACHE bridge: No backend server available for connection: timed out after 10 seconds or idempotent set to OFF or method not idempotent. ". Possible cause of error no backend server available for connection.

We first tried to restart Hyperion Foundation and HPCM related services but no luck. As per the SharedService_LCM.log, there seems to be an issue while connecting to the HPCM web application server during the LCM import process. As we noticed the LCM import process is getting aborted every time exactly after five mins. So we thought it to be a time out issue probably at the OHS layer. 

Solution:

As LCM import was getting aborted after 5 mins so we thought to check and modify (increase or add) the timeout setting for profitability application in the OHS (Oracle HTTP Server) configuration file i.e. mod_wl_ohs.conf. The Oracle HTTP Web server can be configured to time out if a job takes longer than a predefined period. 

Note: If your OHS server has not been configured to a specific shared location and has been kept as default then your mod_wl_ohs.conf file will be stored at <EPM_ORACLE_INSTANCE\httpConfig\ohs\config\OHS\ohs_component\mod_wl_ohs.conf. But If your OHS server is load balanced and has been configured to a shared location and you don't know that location then you can run a registry report and search for that shared location. 

How to modify LCM timeout default settings for HPCM (Profitability) application in Oracle HTTP Server server configuration file (mod_wl_ohs.conf ):

If you are using Hyperion LCM to import large HPCM models or POVs, the LCM import process may take longer to process than the time specified in the default timeout settings on the Oracle WebLogic Server. To fix this issue, you need to increase or add a timeout setting for Profitability application in OHS configuration file i.e. mod_wl_ohs.conf

Steps:

1- Stop your Oracle Hyperion services (Foundation and OHS service must be stopped).

2- On your OHS Web server, navigate to the path:

%Middleware_HOME%\user_projects\epmsystem1\httpConfig\ohs\config\OHS\ohs_component\mod_wl_ohs.conf.

3- First take a backup of mod_wl_ohs.conf file. Then open it and locate the below section: 

<LocationMatch ^/profitability>

4- Add the following lines within the <LocationMatch ^/profitability> section (modify or add the WLIOTimeoutSecs property with a value that will encompass the duration of typical HPCM migration tasks):

WLIOTimeoutSecs 3000
Idempotent OFF

LCM timeout issue while importing HPCM artifacts:EPMLCM-13000:Service currently not available

5- Now on OHS web server, navigate to the path:

%Middleware_HOME%\user_projects\epmsystem1\httpConfig\ohs\config\OHS\ohs_component\htppd.conf

6- First take a backup of htppd.conf file. Then open it and locate the below section:

# Timeout: The number of seconds before receives and sends time out. 

7- Add or modify the Timeout value to 3000, as shown below:

# Timeout: The number of seconds before receives and sends time out. 
Timeout 3000

LCM timeout issue while importing HPCM artifacts:EPMLCM-13000:Service currently not available

Note: The server timeout shown above is a suggested limit, and may be modified to suit the specific timeout settings for your application server.

8- Start your Oracle Hyperion services and try to re-import your HPCM application artifacts. It should work fine now without any timeout issue.

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!!!
Read More

Tuesday, June 2, 2020

// // Leave a Comment

Shell Command/Script to extract Oracle EPM/Hyperion already applied patches in Linux/Unix server

Hi Friends,

Topic: How to extract Oracle Hyperion patches currently applied in your Hyperion environment using shell/bash script

In this post, we will see how you can extract all the Oracle Hyperion patches currently applied in your Hyperion environment Linux servers. I am sure you would have seen Oracle Hyperion Essbase application installed and configured on Linux/Unix server in many Hyperion setups. 

When you plan to apply any newly released Oracle patches to upgrade your Hyperion applications patch level, you may need to first know the currently applied Oracle patches for that particular Hyperion application. 

For the Windows Servers, to know how to extract Oracle Hyperion patches currently applied in your Hyperion environment using a batch script, you may like to read this post: Batch script to extract Oracle Hyperion patches currently applied

Note: 
  • This post has been written and associated activities have been demonstrated on Oracle Hyperion version 11.1.2.4.
  • The demonstrating Hyperion Essbase application server has the 'Red Hat Enterprise Linux Server release 6.10' operating system.
Concept:

As you know, we all use ./opatch lsinventory command in order to see details of all the patches that are currently installed on a Hyperion Linux/Unix server.

The lsinventory command reports what has been installed on the system for a particular Oracle home directory, or for all installations. 

The following syntax is used for this command (on Linux/Unix machine):

./opatch lsinventory -oh /apps/OracleEPM/Middleware/EPMSystem11R1

where -oh option is your Oracle Home location. 


Shell script to extract Oracle Hyperion patches currently applied

The result of ./opatch lsinventory command contains a lot of data like various versions, paths, bugs fixed, overlaying patch details, etc as shown in the below image. So in order to list out only the Oracle patch numbers and when they were applied you need to go through all the details generated as a result of ./opatch lsinventory command then extract the required details manually.

Shell script to extract Oracle Hyperion patches currently applied

The below-mentioned shell/bash script does all these things for you automatically by extracting out only the Oracle patch numbers with the date when they were applied. It saves the output into a log file for your record along with saving your manual effort. 

On Command prompt:

If you want to see all the applied patch list directly on your EPM/Hyperion Linux/Unix server's command prompt, run the below commands:

To see all installed patches:
----------------------------------------
cd /apps/oracle/epm/Middleware/EPMSystem11R1/OPatch

For EPMSystem11R1 patches:
./opatch lsinventory -oh /apps/oracle/epm/Middleware/EPMSystem11R1 -jdk /apps/oracle/epm/Middleware/jdk160_35 -invPtrLoc /apps/oracle/epm/Middleware/EPMSystem11R1/oraInst.loc | grep -i applied

For oracle_common patches:
./opatch lsinventory -oh /apps/oracle/epm/Middleware/oracle_common -jdk /apps/oracle/epm/Middleware/jdk160_35 -invPtrLoc /apps/oracle/epm/Middleware/oracle_common/oraInst.loc | grep -i applied

How to extract Oracle EPM/Hyperion already applied patches in Linux/Unix server

To see a particular installed patch:
---------------------------------------------
cd /apps/oracle/epm/Middleware/EPMSystem11R1/OPatch

For EPMSystem11R1 patches:
./opatch lsinventory -oh /apps/oracle/epm/Middleware/EPMSystem11R1 -jdk /apps/oracle/epm/Middleware/jdk160_35 -invPtrLoc /apps/oracle/epm/Middleware/EPMSystem11R1/oraInst.loc | grep -i "applied" | grep -i "28314691"

For oracle_common patches:
./opatch lsinventory -oh /apps/oracle/epm/Middleware/oracle_common -jdk /apps/oracle/epm/Middleware/jdk160_35 -invPtrLoc /apps/oracle/epm/Middleware/oracle_common/oraInst.loc | grep -i "applied" | grep -i "18514458"

Shell/Bash Script:

Create a folder (/apps/Admin) in the Hyperion Linux/Unix server where you want to extract the installed Oracle patch list and then create a shell/bash script (/apps/Admin/test.shwith the below codes under that folder.

#!/bin/bash

cd /apps/OracleEPM/Middleware/EPMSystem11R1/OPatch

./opatch lsinventory -oh /apps/OracleEPM/Middleware/EPMSystem11R1 > /apps/Admin/Patch_History.txt

cd /apps/Admin

truncate -s 0 /apps/Admin/Installed_Patches_$HOSTNAME.txt

grep -w "Interim patches" /apps/Admin/Patch_History.txt >>/apps/Admin/Installed_Patches_$HOSTNAME.txt

grep -w "applied" /apps/Admin/Patch_History.txt >>/apps/Admin/Installed_Patches_$HOSTNAME.txt

When you run this test.sh script via bash command line, it will extract out the following things for you:
  1. Total number of Oracle patches
  2. Patch numbers of all the currently applied Oracle Hyperion patches
  3. Date of applying these patches
It will also save the output in a log file with your Linux/Unix server HOSTNAME appended in the log file name.

Below is the directory structure and the output format of the above-given script:

Shell script to extract Oracle Hyperion patches currently applied

You can run this script across all the Linux/Unix servers of your Hyperion environment to extract the list of applied Oracle Hyperion patches. 

In order to see which Oracle Hyperion applications your extracted patch numbers belong to, simply do the following:  
  • Go to https://support.oracle.com.
  • Select the Patches & Updates tab.
  • In Patch Search, enter the patch number. 
  • Make sure you are searching for a Patch Name or Number and select Search.
The corresponding Hyperion application name with patch details will be displayed. 

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!!!
Read More

Monday, June 1, 2020

// // 1 comment

Batch Command/Script to extract Oracle EPM/Hyperion already applied patches in Windows server

Hi Friends,

Topic: How to extract Oracle Hyperion patches currently applied in your Hyperion environment using a batch script

In this post, we will see how you can extract all the Oracle Hyperion patches currently applied in your Hyperion environment Windows servers. 

When you plan to apply any newly released Oracle Hyperion patches to upgrade your Hyperion applications patch level, you may need to first know the currently applied Oracle patches for that particular Hyperion application. 

For the Linux/Unix Servers, to know how to extract Oracle Hyperion patches currently applied in your Hyperion environment using a shell/bash script, you may like to read this post: Shell script to extract Oracle Hyperion patches currently applied

Note: 
  • This post has been written and associated activities have been demonstrated on Oracle Hyperion version 11.1.2.4.
  • The demonstrating Hyperion environment has the 'Windows Server 2012 R2 Standard' operating system. 
Concept:

As you know, we all use opatch lsinventory command in order to see all the details of the patches that are currently installed on a particular Oracle Hyperion server.

The lsinventory command reports what has been installed on the system for a particular Oracle home directory, or for all installations. 

The following syntax is used for this command (on Windows machine):

opatch.bat lsinventory -oh E:\apps\OracleEPM\Middleware\EPMSystem11R1

where -oh option is your Oracle Home location. 


Batch script to extract all Oracle Hyperion patches currently applied

The result of opatch lsinventory command contains a lot of data like various versions, paths, bugs fixed, overlaying patch details, etc as shown in the below image. So in order to list out only the Oracle patch numbers and when they were applied you need to go through all the details generated as a result of opatch lsinventory command then extract the required details manually.

Batch script to extract all Oracle Hyperion patches currently applied

The below-mentioned batch script does all these things for you automatically by extracting out only the Oracle patch numbers with the date when they were applied. It saves the output into a log file for your record along with saving your manual effort. 

On Command prompt:

If you want to see all the applied patch list directly on your EPM/Hyperion Windows server's command prompt, run the below commands:

To see all installed patches:
----------------------------------------
cd E:\apps\OracleEPM\Middleware\EPMSystem11R1\OPatch

For EPMSystem11R1 patches:
opatch.bat lsinventory -oh E:\apps\OracleEPM\Middleware\EPMSystem11R1 -jdk E:\apps\OracleEPM\Middleware\jdk160_35 | findstr -i applied

For oracle_common patches:
opatch.bat lsinventory -oh E:\apps\OracleEPM\Middleware\oracle_common -jdk E:\apps\OracleEPM\Middleware\jdk160_35 | findstr -i applied

How to extract Oracle EPM/Hyperion already applied patches in Windows server

To see a particular installed patch:
---------------------------------------------
cd E:\apps\OracleEPM\Middleware\EPMSystem11R1\OPatch

For EPMSystem11R1 patches:
opatch.bat lsinventory -oh E:\apps\OracleEPM\Middleware\EPMSystem11R1 -jdk E:\apps\OracleEPM\Middleware\jdk160_35 | findstr "applied" | findstr "24738130"

For oracle_common patches:
opatch.bat lsinventory -oh E:\apps\OracleEPM\Middleware\oracle_common -jdk E:\apps\OracleEPM\Middleware\jdk160_35 | findstr "applied" | findstr "16964825"

Through Batch Script:

Create a folder (E:\Admin) in the server where you want to extract the installed Oracle patch list and then create a batch script (E:\Admin\test.batwith the below codes under that folder.

@echo off

cd E:\apps\OracleEPM\Middleware\EPMSystem11R1\OPatch

call opatch.bat lsinventory -oh E:\apps\OracleEPM\Middleware\EPMSystem11R1 >"E:\Admin\Patch_History.txt"

cd E:\Admin

rem. > "E:\Admin\Installed_Patches_%COMPUTERNAME%.txt"

findstr /L /C:"Interim patches" "E:\Admin\Patch_History.txt" >>"E:\Admin\Installed_Patches_%COMPUTERNAME%.txt"

echo. >>"E:\Admin\Installed_Patches_%COMPUTERNAME%.txt"

FOR /F "tokens=*" %%A IN (
  'findstr /L "applied" "E:\Admin\Patch_History.txt"'
) DO (
  echo %%A >>"E:\Admin\Installed_Patches_%COMPUTERNAME%.txt"
)

When you run this test.bat script via command prompt, it will extract out the following things for you:
  1. Total number of Oracle patches
  2. Patch numbers of all the currently applied Oracle Hyperion patches
  3. Date of applying these patches
It will also save the output in a log file with your SERVERNAME appended in the log file name.

Below is the folder structure and the output format of the above-given script:


Batch script to extract all Oracle Hyperion patches currently applied

You can run this script across all the Windows servers of your Hyperion environment to extract the list of applied Oracle Hyperion patches. 

In order to see which Oracle Hyperion applications your extracted patch numbers belong to, simply do the following:  
  • Go to https://support.oracle.com.
  • Select the Patches & Updates tab.
  • In Patch Search, enter the patch number. 
  • Make sure you are searching for a Patch Name or Number and select Search.
The corresponding Hyperion application name with patch details will be displayed. 

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!!!
Read More

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!!!
Read More