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.
Was looking to do this myself and saw complaints about blank lines:
I am in the US. I can run this code in Windows 7, Windows 2008 R2, Windows Server 2003, Windows XP (All OS's are current with Windows Updates and patches). All with short date setting without ddd (or dddd) (day of week).
If today is Thursday, it would output "The Day Is: Thu".
This returns the day on all 4 Windows versions I have tested on. And only the day. When I changed my short date setup to be "ddd, M/d/yyyy", my output would show the day with a comma (e.g. Thu,) which tells me this code does use the short date format. But what also may be happening is that if the short date does not contain the day of week, it may look to the long date format which on all 4 machines I tested on, have dddd in the format.
This turned out way more complex then I first suspected, and I guess that's what intrigued me, I searched every where and all the methods given wouldnt work on Windows 7.
So I have an alternate solution which uses a Visual Basic Script.
The batch creates and executes the script(DayOfWeek.vbs), assigns the scripts output (Monday, Tuesday etc) to a variable (dow), the variable is then checked and another variable (dpwnum) assigned with the days number, afterwards the VBS is deleted hope it helps:
EDIT:
Though I turned to VBS, It can be done in pure batch, took me a while to get it working and a lot of searching lol, but this seems to work:
The only caveat for you on the above batch is that its day of weeks are from 1-7 and not 0-6
Locale-dependent version: In some environments, the following will extract the day name from the date:
It assumes that the day name is the first part of
%date%
. Depending on the machine settings, though, the substring part (~0,3
) would need to change.A statement such as this would dump to a file with a three character day name:
Locale-independent version: If you need it less dependent on the current machine's day format, another way of doing it would be to write a tiny application that prints the day of the week. Then use the output of that program from the batch file. For example, the following C application prints
dayN
where N=0..6.If the above were compiled and linked as
myday.exe
, then you could use it from a batch file like this:few more ways:
1.Robocopy not available in XP but can be downloaded form with win 2003 resource tool kit .Also might depend on localization:
2.MAKECAB - works on every windows machine (but creates a small temp file).Function provided by carlos:
3.W32TM - uses command switches introduced in Vista so will not work on windows 2003/XP:
4..bat/jscript hybrid (must be saved as .bat):
5..bat/vbscript hybrid (must be saved as .bat)
6.powershell can be downloaded from microsoft.Available by default in everything form win7 and above:
7.WMIC already used as an answer but just want to have a full reference.And with cleared
<CR>
:9.Selfcompiled jscript.net (must be saved as
.bat
):A version using MSHTA and javascript. Change %jsfunc% to whateve jscript function you want to call