gisvpn
asked on
Batch File Date
Hello,
I am trying to get the currnet date so that when I create a file it has the date in it, however this has not been easy !..
I am trying to use this to create the date (see code below). However this this runs it seems to create the Pdate as _03_2009 which is strange as I was expecting 2009_03_30 ?
Can someone tell me what is wrong with the below. I am using this batch files on Windows XP Compuiters only.
I am trying to get the currnet date so that when I create a file it has the date in it, however this has not been easy !..
I am trying to use this to create the date (see code below). However this this runs it seems to create the Pdate as _03_2009 which is strange as I was expecting 2009_03_30 ?
Can someone tell me what is wrong with the below. I am using this batch files on Windows XP Compuiters only.
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ('DATE /T') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('DATE /T') DO SET dd=%%B
FOR /F "TOKENS=3* DELIMS=/ " %%A IN ('DATE /T') DO SET yyyy=%%B
set /A dd=%dd%
set Pdate=%yyyy%_%mm%_%dd%
echo %Pdate%
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some display the day as well (mine does) - apparently yours does not...try this:
set yyyy=%date:~-4%
set mm=%date:~0,2%
set dd=%date:~3,2%
set Pdate=%yyyy%_%mm%_%dd%
try this
set yyyy=%date:~-4%
set mm=%date:~5,2%
set dd=%date:~8,2%
set Pdate=%yyyy%_%mm%_%dd%
Please try the following:
set Pdate=%date:~-4%_%date:~3, 2%_%date:~ 0,2%
set Pdate=%date:~-4%_%date:~3,
ASKER
Excellent thank you.
ASKER
I just noticed that the month and day appear to be swapped around as in DD = 03 (when it should be 30)..
Can I just change it to as below :
Could I also ask what the end bits mean (i.e. ~5,2%) ?
Thanks,
GISVPN
Can I just change it to as below :
Could I also ask what the end bits mean (i.e. ~5,2%) ?
Thanks,
GISVPN
set yyyy=%date:~-4%
set dd=%date:~5,2%
set mm=%date:~8,2%
set Pdate=%yyyy%_%mm%_%dd%
ASKER
what are the difference between all the posts ?
Glad I could help - thanx for the grade! :^)
I'm glad also that my correct answer was not graded :)
this is because the date is in the format: 2009-03-30
counting index from 0 is also applied to "-" sign
0 1 2 3 4 5 6 7 8 9
----------------------
2 0 0 9 - 0 3 - 3 0
so, 0 to 4 is a year 5 to 6 is month 8 to 9 is day
this is because the date is in the format: 2009-03-30
counting index from 0 is also applied to "-" sign
0 1 2 3 4 5 6 7 8 9
----------------------
2 0 0 9 - 0 3 - 3 0
so, 0 to 4 is a year 5 to 6 is month 8 to 9 is day
The date bits start with position number 0. So, the first two bits (on the left) are 0,2 - (that's interpretted as "the substring starting at character position 0, and which contains 2")
So, in the case of
dd/mm/yyyy
'dd' will be 0,2 (or -10,2)
'mm' will be 3,2 (or -7,2)
'yyyy' will be 6,4 (or just -4)
If your date format is:
dd/mm/yyyy
then to get:
%yyyy%_%mm%_%dd%
you'll need the following code:
set Pdate=%date:~-4%_%date:~3, 2%_%date:~ 0,2%
So, in the case of
dd/mm/yyyy
'dd' will be 0,2 (or -10,2)
'mm' will be 3,2 (or -7,2)
'yyyy' will be 6,4 (or just -4)
If your date format is:
dd/mm/yyyy
then to get:
%yyyy%_%mm%_%dd%
you'll need the following code:
set Pdate=%date:~-4%_%date:~3,
If your date format is:
yyyy/mm/dd
then to get:
%yyyy%_%mm%_%dd%
you simply need:
set Pdate=%date:/=_%
yyyy/mm/dd
then to get:
%yyyy%_%mm%_%dd%
you simply need:
set Pdate=%date:/=_%
ASKER
Great thank you for the help ;)
"I'm glad also that my correct answer was not graded :)"
Roads_Roads, just so you are aware, the question was answered before you even posted your incorrect answer.
The format of the date variable is most certainly 'not' as you indicated:
0 1 2 3 4 5 6 7 8 9
----------------------
2 0 0 9 - 0 3 - 3 0
so, 0 to 4 is a year 5 to 6 is month 8 to 9 is day
In fact, if you interpret the author's original code, you'll be able to determine that their regional settings are MMDDYYYY (not YYYY-MM-DD).
t0t0 has correctly displayed the interpretation(s) presented by the working code.
If you type
echo %date%
at a command prompt, you'll see the format that the date is laid out as - mine is DDD MM/DD/YYYY
So, in order for me to set a variable to YYYY_MM_DD, I would need to start my new variable at position 4 and grab that and the next position (5) using:
Set MM=%date:~4,2%
DDD MM/DD/YYYY
0123456789012
The day is acquired by using the same format, beginning at character 7
Set DD=%date:~7,2%
Finally, the year can be acheived by beginning at position 10 (the second 0 above), and since we know it's 4 characters, it could be retrieved using:
Set YYYY=%date:~10,4%
However, it's simpler to just grab the last 4 characters using
Set YYYY=%date:~-4%
The negative sign (-) states to start 4 characters from the right side and retrieves all characters to the end, since we didn't specify how many to pulll...
Roads_Roads, just so you are aware, the question was answered before you even posted your incorrect answer.
The format of the date variable is most certainly 'not' as you indicated:
0 1 2 3 4 5 6 7 8 9
----------------------
2 0 0 9 - 0 3 - 3 0
so, 0 to 4 is a year 5 to 6 is month 8 to 9 is day
In fact, if you interpret the author's original code, you'll be able to determine that their regional settings are MMDDYYYY (not YYYY-MM-DD).
t0t0 has correctly displayed the interpretation(s) presented by the working code.
If you type
echo %date%
at a command prompt, you'll see the format that the date is laid out as - mine is DDD MM/DD/YYYY
So, in order for me to set a variable to YYYY_MM_DD, I would need to start my new variable at position 4 and grab that and the next position (5) using:
Set MM=%date:~4,2%
DDD MM/DD/YYYY
0123456789012
The day is acquired by using the same format, beginning at character 7
Set DD=%date:~7,2%
Finally, the year can be acheived by beginning at position 10 (the second 0 above), and since we know it's 4 characters, it could be retrieved using:
Set YYYY=%date:~10,4%
However, it's simpler to just grab the last 4 characters using
Set YYYY=%date:~-4%
The negative sign (-) states to start 4 characters from the right side and retrieves all characters to the end, since we didn't specify how many to pulll...
I didn't realise you had already awarded points for this question... never mind. You can also force the system to format the date to dd/mm/yyyy, do your stuff, then return the date format back to what it was, as in the following:
@echo off
rem specify date format
set datestring=dd/MM/yyyy
rem save current date format
for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v sshortdate') do (
set sfmt=%%a
)
for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v slongdate') do (
set lfmt=%%a
)
rem replace current date format with new date format
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %datestring% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %datestring% /f >nul
rem get the date
set datestring=%date%
rem return original date format
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %sfmt% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %lfmt% /f >nul
rem unset variables
set sfmt=
set lfmt=
rem date format as dd/mm/yyyy
echo %datestring%
@echo off
rem specify date format
set datestring=dd/MM/yyyy
rem save current date format
for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v sshortdate') do (
set sfmt=%%a
)
for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v slongdate') do (
set lfmt=%%a
)
rem replace current date format with new date format
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %datestring% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %datestring% /f >nul
rem get the date
set datestring=%date%
rem return original date format
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %sfmt% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %lfmt% /f >nul
rem unset variables
set sfmt=
set lfmt=
rem date format as dd/mm/yyyy
echo %datestring%
ASKER
Thanks for the post, I think its there however I get this now :
2009_3/_00
I am trying to get 2009_03_30
;)