Batch script or set of commands to extract the fields of date

0

I am trying to create a batch file to backup my data at regular intervals in Windows using a batch script.

I need date parameter and the fields from the displayed format (Month, day and year). everything works fine when I am using delimiters initially. But when I change the date format from dd-mm-yyyy to yyyy-mm-dd everything fails.

So is there any generic format to extract the fields(mm,dd,yyyy) even-though the format varies.

Thanks.

Avinash Reddy

Posted 2013-03-29T20:16:46.107

Reputation: 101

2This is hard to follow as-is, Can you post the batch code you have so far? – Ƭᴇcʜιᴇ007 – 2013-03-29T20:21:08.127

I do believe there is a similar question on [so], I just can't find it yet. – BDM – 2013-03-30T09:12:41.200

While it might be possible to cook something up (which will no doubt make your batch files even more complex unnecessarily), how often do you need to change your system's date format in Control Panel anyway? – Karan – 2013-03-31T15:12:58.757

Answers

1

The only safe way to get a kind of generic date is the following (hopefully independent of local settings):

@echo off &setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find "REG_SZ"') do set "ssShortDate=%%b"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "dd MM yyyy" >nul
set "cdate=%date%"
reg add "HKCU\Control Panel\International" /f /v sShortDate /d "%ssShortDate%" >nul
for /f "tokens=1-3" %%i in ("%cdate%") do set "day=0%%i"&set "month=0%%j"&set "year=%%k"
set "day=%day:~-2%"
set "month=%month:~-2%"
echo.%year%-%month%-%day%
endlocal

Endoro

Posted 2013-03-29T20:16:46.107

Reputation: 2 036