I have a windows batch file that runs daily. Wish to log data into a file and want to rotate it (i.e. having at most the last 7 days worth of data).
Looked into the commands DATE
and DELIMS
- Cannot figure out a solution.
Is there a simple solution to create a file name that contains the day of the week i.e. 0 for monday etc.
Or do I have to resort to some better shell script.
First - Copy CON SETUPDAY.001 SET WORKDAY=^Z (very important - no cr/lf)
USE %WEEKDAY% IN YOUR SCRIPT
I thought that my first answer gives the correct day of week as a number between 0 and 6. However, because you had not indicated why this answer does not give the result you want, I can only guess the reason.
The Batch file below create a log file each day with a digit in the name, 0=Sunday, 1=Monday, etc... The program assume that
echo %date%
show the date in MM/DD/YYYY format; if this is not the case, just change the position of mm and dd variables in thefor
command.If this is not what you want, please indicate the problem so I can fix it.
EDIT: The version below get date parts independent of locale settings:
EDIT: The version below insert day of week as 3-letter short name:
Regards,
Antonio
If you can change format of short date in the PC to "ddd yyyy-MM-dd" (only first parameter 'ddd' is compulsory), then following command returns-
Then you can write you batch file -
Another spin on this topic. The below script displays a few days around the current, with day-of-week prefix.
At the core is the standalone :dpack routine that encodes the date into a value whose modulo 7 reveals the day-of-week per ISO 8601 standards (Mon == 0). Also provided is :dunpk which is the inverse function:
%DATE%
is not your friend. Because the%DATE%
environment variable (and theDATE
command) returns the current date using the Windows short date format that is fully and endlessly customizable. One user may configure the system to return 07/06/2012 while another might choose Fri060712. Using%DATE%
is a complete nightmare for a BAT programmer.There are two possible approaches to solve this problem:
You may be tempted to temporarily change the short date format, by changing the locale settings in the registry value
HKCU\Control Panel\International\sShortDate
, to your recognizable format. Then access%DATE%
to get the date in the format you want; and finally restore the format back to the original user format. Something like thisbut this method has two problems:
it tampers with a global registry value for its local particular purpouses, so it may interfere with other processes or user tasks that at the very same time query the date in short date format, including itself if run simultaneously.
and it returns the three letter day of the week in the local language that may be different in different systems or different users.
use WMIC Win32_LocalTime, that returns the date in a convenient way to directly parse it with a
FOR
command.this is the method I recommend.
I have this solution working for me:
Create a file named dayOfWeek.vbs in the same dir where the cmd file will go.
dayOfWeek.vbs contains a single line:
or, if you want day number instead of name:
The cmd file will have this line:
Now you can use variable dayName like: