Using current date as part of the file or a folder (the directory) name is very useful. We can use, for example, the dates to distinguish the log files by the days.
We can use the value of current date (and also time) in the batch files, whenever we generating new files and folders. The batch files can use special variable named DATE and which returns the current date in the local regional format.
As we knew, the regional settings are different. Therefore, our batch file will work fine with one settings and return useless information with other. That’s the reason why we need to check this settings before we can use any batch file that utilize the date value.
(Update 28.03.2019) You can find the alternate way to read the current date and time values in this post. With this method you will always have the same format of the current day or time, regardless the local regional settings.
I used all those scripts on the Windows XP, 2003 Server and later versions of the Microsoft Windows operating systems.
Checking the date format
I made a simple batch script that will just return the current date and under it write some numbers. Those numbers will be used to mark a position of every number or the letter in the date string.
Here is source of the check_date_format.bat file:
@echo off cls echo %date% echo 012345678901234567890 echo 11111111112 echo.
When we run this batch file from the command prompt, we will see current date. Here is English US date format:
Here is another example of the changed local settings. I changed short date format to display the date on the way more common to the Serbian local settings.
Those numbers under the date string will be the position counter. We don’t need to count manually positions. First character in this string have a position of 0. This is the reason why we begin with 0 in the counter.
We will continue with first example, the US date format. As we can see, we have three letter abbreviation of the name of the day, then we have space, the number of the month in the year, the number of the day in the month and finally the year by the Gregorian calendar. We can see that we have also slash sign (/) as the separator.
We need a day, a month and a year. We will then repack that information to one string for a today date. That string should be in format YYYYMMDD. Then we can use it to create the directory per day or we can add it to the end of the file, like some log or the output result files.
When we add the date string in such order, we can later sort the files and folders in ascending order, the day after day, in the chronological order. Here is an example of the folders (the ClamAV quarantine folder) sorted by date in their name:
We can now see when we caught some viruses in an e-mail. Furthermore, we can easily find any e-mail held in the quarantine and check it further.
When we analyze this date string, we will see that we need to extract:
- Four characters between positions 10 and 13 for a year value
- Two characters between positions 4 and 5 for a month
- And two characters between positions 7 and 8 for a day
Extracting interesting parts
Now that we know what we need to extract, we can use this to write our batch file. I made another batch file, named get_today_date.bat, which will demonstrate this operation. Here is the content of this file:
@echo off cls set day=%date:~7,2% set month=%date:~4,2% set year=%date:~10,4% set today=%year%%month%%day% echo Today day = %day% echo Today month = %month% echo Today year = %year% echo. echo Today date =%today% echo.
Main part of this batch is done in the first three lines beginning with the command SET. We will analyze further first line:
set day=%date:~7,2%
In our first example we saw that we can display a current day with command
ECHO %date%
Now we need just part of this output (the date string) for out variable named DAY. Also, we counted position of that information. It’s on 8th character in the string and that’s position 7. We will now do the trick.
We can add :~x,y behind variable name, inside % signs, to tell the batch interpreter that we want just the part of the value, not whole variable value. Part :~ is mandatory. The x and y are number position and length.
We will indicate position of first character with value of X. In our example, this is 7. With second value we will indicate length of the substring. In our example it’s 2, as we need to extract just 7th and 8th character in the string.
When we collect all this together, we will have :~7,2 as the marker for a beginning and the end of the selection. In our example that will return 27.
We do the same for a month and the year. Eventually we have three new variables for every element of the today date.
Collecting all this together
We will now collect all three separated values together and then we can form forth variable TODAY. We can do that with following command:
set today=%year%%month%%day%
We just need to write other variables one behind other and the batch interpreter will collect (concatenate) their values in one unique string. In our example, we collected 27th day of the 5th month of the year 2016 in the string with value of 20160527.
Our demo batch file is just printed this on the screen with ECHO command. However, we can use it to create a directory (the folder) of the day, like with the command:
MD %ClamVirDir%\%today%
Or we can use it in the file name:
set LogFileDir=D:\XMail\Logs\ set LogFile=%LogFileDir%ClamAV_%today%.log
As the result, we will have the daily separated log files for the ClamAV operations.
These files will be smaller and easier for searching and analyzing. On top of that, we can delete information older than X days, depending on our security policy.
One more trick for the end. We don’t need to set three new variables for the day, month or a year. We can do all that in just single command with
set today=%date:~10,4%%date:~4,2%%date:~7,2%
(Update 28.03.2019.) Alternatively, you can always read the date value in the same format regardless of the regional settings using the WMIC command.
Stay tuned.
How to find today’s file using the FINDSTR command in batch script?
LikeLike
Hi,
The FINDSTR command is intended to search for text (strings) inside files. It’s more advanced version of the FIND command.
If you want to search for files, you should consider the FORFILES command. It can search for files in the different ways. One of the parameters is /D for date. Now, if you use:
forfiles /d +0 /c “cmd /c dir @fname”
you can enlist all files created today.
Cheers,
Srdjan
LikeLike
[…] we will use two variables (%date% and %time%) in the batch file to return the current date or time values. This method is fine, although the returned values depend on the regional […]
LikeLike