I know nothing about batch files and I need to automate an application. The application has two fields
- Today's date
- Yesterday's date
In the batch file I have the application's location and my log in information.
And additionally I need these two dates. How can I get the dates via batch file?
2 Answers
On a Microsoft Windows system, you can obtain the current date using the date /t command (the /t option prevents the command from prompting for a change to the the date) or by using echo %date% to display the contents of the date environment variable. However, both of those display the date in the form DDD mm/dd/yyyy, if the system uses a U.S. date format for displaying dates, where DDD is the day of the week represented by a 3-character abbreviation, e.g., "Thu", mm is a two-digit representation of the month, e.g., "08" for August, dd is the day and yyyy is the year.
C:\>date /t
Thu 08/06/2015
C:\>echo %date%
Thu 08/06/2015
You can reformat the representation of the date that is stored in the %date% environment variable. E.g, if you wanted the date in the form yyyymmdd, you can use a command like the one below where a variable, YYYYMMDD is set to hold the reformatted date; the variable name can be anything you like, e.g., mydate, etc.
C:\>set YYYYMMDD=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%
C:\>echo %YYYYMMDD%
20150806
The substring arguments to extract the elements of the date string are in the format %variable:~startposition,numberofchars%, so if the "T" in Thursday in the string "Thu 08/06/2015" is at position 0, the 10th character is the "2" of 2015 and I want 4 characters, i.e., "2015", so %DATE:10,4% will give me those characters. Or you can also think of the first number as the numer of characters to be skipped, i.e., %variable:~num_chars_to_skip,numberofchars%. I can then append %DATE:~4,2% to get "08" for the month followed by %DATE:~7,2% to extract the day, i.e., "06" if the date is August 6, 2015 represented in the %DATE% variable as "Thu 08/06/2015". Reference with additional links
If your batch file may be used on systems in other countries, though, you may need to make adjustments for a different style of date display other than month/day/year if you are reformatting the date rather than just displaying it in whatever format is the default one for the system. If you issue a reg query command, you can see the default format for the system. E.g.:
C:\>reg query "HKCU\Control Panel\International" /v sShortDate
HKEY_CURRENT_USER\Control Panel\International
sShortDate REG_SZ M/d/yyyy
In the above example, I can see the system is using the month/day/year format. The setting for the country can be seen with reg query "HKCU\Control Panel\International" /v sCountry, but that may not be some thing you have to concern yourself with if you know the systems are local ones using a default date representation.
For ways to get yesterday's date there are several Stack Overflow postings, including the one mentioned by DavidPostill:
How to get yesterday's date in batch file?
Slightly OT, but if you are able to use PowerShell instead of CMD.EXE, it all gets much easier, e.g. you can use the Get-Date expression and a UFormat specifier, as documented here, to do exactly what you want. Powershell also makes it easier to do sensible arithmetic on dates, eg it knows that the day before 1 Jan 2025 is not 0 Jan 2025.
PowerShell is standard on versions of Windows going back to at least Windows 7.
Examples (here, PS > is a prompt in the interactive PowerShell ISE window):
PS > $oldfile = "backup." + (get-date (get-date).addDays(-1) -UFormat "%Y%m%d")
PS > $newfile = "backup." + (get-date -UFormat "%Y%m%d")
PS > echo $oldfile
backup.20171027
PS > echo $newfile
backup.20171028