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
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:
On Windows 10 system:
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:
On Windows 10 system:
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:
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:
- A log file in current directory with name: %today_date_time%_Output.log
- A folder in current directory with name: %today_date_time%
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):
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.
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!!!
Batch Script Version 3
ReplyDelete:: ================================================================
:: 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
Good approach! but again 'date' command output is dependent on your local system/server 'date and time setting' which is fully customizable.
DeleteIs it possible to use 12-hour format in that script batch?
ReplyDeleteAntalya
ReplyDeleteKonya
Adana
Ankara
Van
XGS328
Mardin
ReplyDeleteistanbul
Çanakkale
Antep
Elazığ
BUDS8
https://titandijital.com.tr/
ReplyDeletemalatya parça eşya taşıma
bilecik parça eşya taşıma
antalya parça eşya taşıma
hakkari parça eşya taşıma
K7RUO0
8D582
ReplyDeleteZonguldak Evden Eve Nakliyat
Kripto Para Nedir
Nevşehir Evden Eve Nakliyat
Bolu Evden Eve Nakliyat
Adana Evden Eve Nakliyat
D567C
ReplyDeleteManisa Şehirler Arası Nakliyat
Ünye Televizyon Tamircisi
Kırklareli Lojistik
Kütahya Parça Eşya Taşıma
Kripto Para Borsaları
Ankara Parke Ustası
Eryaman Parke Ustası
Bybit Güvenilir mi
Yalova Evden Eve Nakliyat
773FD
ReplyDeleteKars Lojistik
Ordu Evden Eve Nakliyat
Altındağ Fayans Ustası
Sinop Parça Eşya Taşıma
Samsun Şehirler Arası Nakliyat
Nevşehir Parça Eşya Taşıma
Arbitrum Coin Hangi Borsada
Ağrı Şehir İçi Nakliyat
Konya Evden Eve Nakliyat
B4763
ReplyDeleteEryaman Alkollü Mekanlar
Niğde Evden Eve Nakliyat
Erzincan Lojistik
Bitlis Şehir İçi Nakliyat
Çankaya Fayans Ustası
Ardahan Lojistik
Mersin Şehir İçi Nakliyat
Çerkezköy Marangoz
Van Lojistik
513D2
ReplyDeleteArdahan Evden Eve Nakliyat
Silivri Cam Balkon
Kırıkkale Evden Eve Nakliyat
Muş Evden Eve Nakliyat
trenbolone enanthate for sale
Kripto Para Nedir
buy sarms
Silivri Duşa Kabin Tamiri
boldenone for sale
4EDBB
ReplyDeleterize seslı sohbet sıtelerı
rastgele canlı sohbet
mersin rastgele sohbet
Kilis Ücretsiz Sohbet Uygulaması
ardahan en iyi sesli sohbet uygulamaları
Düzce Ücretsiz Sohbet Sitesi
Rize Görüntülü Sohbet
ığdır Mobil Sohbet
sesli sohbet siteleri
F19AF
ReplyDeletetokat nanytoo sohbet
antep rastgele sohbet odaları
sesli mobil sohbet
Karaman Rastgele Sohbet Siteleri
telefonda rastgele sohbet
Bayburt Rastgele Görüntülü Sohbet Ücretsiz
kocaeli görüntülü sohbet
Ordu Mobil Sohbet Odaları
kilis canli goruntulu sohbet siteleri
D77BB
ReplyDeleteledger wallet
avax wallet web
web dcent wallet
onekey web
ellipal wallet
ellipal
desktop ledger
web trezor suite
trust wallet
FC94E
ReplyDeletebkex
bybit
binance
en güvenilir kripto borsası
canlı sohbet ücretsiz
kredi kartı ile kripto para alma
binance
mexc
canlı sohbet
D0754
ReplyDeletejeneratör fiyatları
Yapay Zeka
jeneratör fiyatları
İçerik Editörü İş İlanları
Blogger SEO
Site Satışı
Anime Önerileri
MMORPG Oyunlar
vds satın al
D978D
ReplyDeleteBacklink Satın Al
takipci satin al
residential proxy satın al
Instagram Para Kazanma
Telegram Kanal Üye Satın Al
abone satin al
beğeni satın al instagram
Sunucu Kiralama
offshore
B1C40
ReplyDeleteIPv6 Proxy Satın Al
Youtube Beğeni Satın Al
instagram beğeni satın al
adwords kupon satışı
Bing SEO
Hosting Firmaları
google yorum satın al
coin kazanma
Amazon SEO
3368F
ReplyDeleteAnkr Coin Yorum
Bitcoin Yorum
Zil Coin Yorum
BTC Son Dakika
Flux Coin Yorum
Rose Coin Yorum
Bitcoin Forum
Algo Coin Yorum
Axl Coin Yorum
87948
ReplyDeleteLink Coin Yorum
Vite Coin Yorum
Eos Coin Yorum
Ada Coin Yorum
Egld Coin Yorum
Lpt Coin Yorum
Zil Coin Yorum
Mxc Coin Yorum
Uni Coin Yorum
C87D2
ReplyDeletecanlı şov
45C47
ReplyDeleteücretli sanal show
BC88B
ReplyDeletesanal canlı şov
2A11A
ReplyDeletewhatsapp görüntülü show