I have a process I run from a batch file, and i only want to run it on a certain day of the week.
Is it possible to get the day of week?
All the example I found, somehow rely on "date /t" to return "Friday, 12/11/2009", however, in my machine, "date /t" returns "12/11/2009". No weekday there.
I've already checked the "regional settings" for my machine, and the long date format does include the weekday. The short date format doesn't, but i'd really rather not change that, since it'll affect a bunch of stuff I do.
I'm a little late to this party, but this will give you the day of week:
Use a batch for loop to obtain the output, and you have a testable condition for the day of the week.
Rob
Here's the correct answer for a BAT script on a modern system:
Outputs:
Usage:
I used this for PC's which auto-boot daily from BIOS, which don't have an option to exclude weekends.
Aside: ACPI is a more reliable way of doing this, and allows for a dummy BAT script such as
exit
to be used in a Windows Scheduled Task with specific days selected, using the Wake the computer to run this task option.Here's a batch file that will get all the date and time info into variables but it does depend on you having an appropriate date format set on your machine. Also, the order might need adjusting depending on the local date format, I'm not sure.
The bit you want is in %WD%.
Batch can't do this easily. It requires a lot of command-line fu with the date command. There are a few ways to achieve what you're after, but most of all I'm wondering why the built-in Task Scheduler isn't an option? It allows you specify the days you want it to run and what time:
You could also use VBScript:
Then run it:
You can easily use if/else logic in VBScript then have your VBScript code execute batch files depending on the day, and vice-versa (have batch call a VBScript).
Now all that remains is to pre-pend zeros to the day and month if either of them are less than 10.
My answer is based on RobW's answer, but he simply echoes the weekday, while the question was how to change the course of the script depending on the weekday (exactly what I wanted to do too). Well, it turned out to be less simple than it sounds, because somehow the
FOR
loop gets two lines to process instead of one, and the second one (which seems to be empty) breaks the script execution.The only
FOR
loop that actually worked for me without breaking execution is the following:The
2>NUL
part suppresses the error that would otherwise be shown during the second assignment, while/a
deals with the one space that gets added because of the redirection.Incase anyone needs it, this works for for me on Windows 7 and XP...
For Windows Server 2008 I'd go with one of the Powershell methods suggested above.
To avoid having to redirect to a text file, there is a variation of the FOR which takes a command string. Here it is:
@For /F "tokens=4 delims=/- " %i In ( '@Echo.^|Command /C Date^|Find /i "current"' ) Do @Echo %i
Remember to use double percent signs if you put this command in a dos script. In my example, I checked for the common date delimiter character of forward slash or dash.
I prefer this solution as it is solely DOS without having to rely on whether WMI, PowerShell, etc being present or not.
How about this one for Windows 2008, much more elegant. Also will work anywhere powershell is installed.
Thanks to Phil Swiss previous posting - this works a treat :
echo. | cmd /C date | find "current"
Examples usage:
echo. | cmd /C date | find /i "Fri" && echo today is Friday
Or to reverse:
echo. | cmd /C date | find /i "Fri" || echo today is NOT Friday