Reputation: 542
The dir command in Windows displays dates in a format that depends on operating system settings.
For example, when I run 'dir' from the command line on my computer, the date is displayed like '09/04/2015'. When I run the same command on another server, I see '07/Aug/2015'. I presume that many different formats are possible depending on operating system settings.
I need a way to reliably parse the day, month, and year from the date string returned by the 'dir' command, that will run on any Windows operating system since (and including) Windows XP.
Does anyone know how to force the 'dir' command to display dates in a particular format?
Or, does anyone know how to identify (from the command line) the format in which the 'dir' command will display dates?
Upvotes: 2
Views: 2809
Reputation: 5389
The solution is not perfect and I advise, not to implement it unless very necessary.
When you change the format of date, it sets a registry value
HKCU\Control Panel\International
->sShortDate REG_SZ <format_Of_date>
where format_of_date is the format chosen by user.
Now, if you edit this value to your convenient format and save it, the dir
command will give you the expected format in result.
I said, it's not advisable because, changing custom settings on user machine is never good. To avoid this, what you can do is, store the initial value of sShortDate
prior to changing and set that initial value after your implementation of dir
cmd.
You can verify using this batch file::
reg export "HKEY_CURRENT_USER\Control Panel\International" C:\DateFormat.reg
reg add "HKEY_CURRENT_USER\Control Panel\International" /f /v "sShortDate" /t
REG_SZ /d "yyyy-MM-dd"
;; dir output always with "yyyy-MM-dd" date format
dir > C:\dir.txt
reg import C:\DateFormat.reg
del C:\DateFormat.reg
pause
Upvotes: 3