|
Answer» GuruGary, every time you assist someone with a date related issue (ie. adding a date to a file name) you always say "depending on your date setup" it is either mmddyyyy or ddmmyyyy.
Is there a way to detect which date format you have?
I want to automate the process and not have to alter the code for every machine. My desktop is set up as mmddyyyy and my server is set up as ddmmyy. I would like to know which way it is set up and code around that.
KenHi, Ken. The date format of the computer is controlled by the "Regional Settings" of the computer (in the Windows CONTROL Panel). Different countries use different SEPARATORS and different formats for date, time, currency, etc. So you could change them to be the same on each computer. Or if you want something that will work in any scenario, the script below will work. It is a generic script that will set environment variables to YY MM and DD for Year Month and Day. So if you were going to add the date to a filename, you could do something like Code: [Select]ren filename.ext filename%yy%%mm%%dd%.extHere is the universal date program. This should work for any regional settings: Code: [Select]@echo off set $tok=1-3 for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u if "%$d1:~0,1%" GTR "9" set $tok=2-4 for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do ( for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do ( set %%x=%%u set %%y=%%v set %%z=%%w set $d1= set $tok=))Ah... can I just say WOW. There seems to be an error though and I am not good ENOUGH to see this. Thanks Again, Ken
Microsoft Windows XP [Version 5.1.2600] (C) COPYRIGHT 1985-2001 Microsoft Corp.
>set $tok=1-3 >for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u %%u was unexpected at this time.
>if "%$d1:~0,1%" GTR "9" set $tok=2-4
>for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do ( %%u was unexpected at this time.
>for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do ( %%x was unexpected at this time.
>set %%x=%%u
>set %%y=%%v
>set %%z=%%w
>set $d1=
>set $tok=))
> Worked great for me.
Try this at the DOS prompt: date /t
What do you get? I get a date in WindowsNT
If what you posted was taken directly from your BAT file, I can't imagine what is wrong. I was looking for an error in copying.
Nice script, GG
Mac I was actually testing this in DOS directly. I hadn't put it into a script yet. Now that I have put this in a script...it WORKS. Thanks
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
>date /t 06/11/2006
I don't mean to be a pain GuruGary but any chance you could translate this code you gave me into english? I would like to understand what it is doing, but I just can't seem to figure it out.
Ken Sorry for the delay. The code I gave you is meant to run in a batch file. If you try to run it from a command line you will need to change the %% to a single %, and you will also have to combine commands between () all on one line. So from a command line you would need to do: Code: [Select]set $tok=1-3 for /f "tokens=1 delims=.:/-, " %u in ('date /t') do set $d1=%u if "%$d1:~0,1%" GTR "9" set $tok=2-4 for /f "tokens=%$tok% delims=.:/-, " %u in ('date /t') do (for /f "skip=1 tokens=2-4 delims=/-,()." %x in ('echo.^|date') do (set %x=%u&set %y=%v&set %z=%w&set $d1=&set $tok=)) The batch file basically uses the date command (without any parameters) to see what format the date is in, since it prompts for the date including the format. It uses that information with the date /t (print date) command to fill in those variables.Here is a line-by-line translation of what the script does:
rem Turn off echo @echo off rem Set the initial tokens to be 1 through 3 (to be used later) set $tok=1-3 rem Take the first part of the string returned by the "date /t" command pad put it in the $d1 variable for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u rem If the first character of the variable $d1 is greater than 9, it must be a letter, therefore the day of week is given, so we adjust our $tok variable to skip the day of week if "%$d1:~0,1%" GTR "9" set $tok=2-4 rem Get each part of the date in 3 steps using either parts 1-3 or 2-4 depending on if day of week is given for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do ( rem get the yy mm and dd from the date [Enter] command to assign the parts in the previous step for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do ( rem Set the first part of the date set %%x=%%u rem Set the second part of the date set %%y=%%v rem Set the third part of the date set %%z=%%w rem Clear the $d1 variable since we are done with it set $d1= rem clear the $tok variable since we are done with it and close both loops set $tok=))Ok I can start to see how this works. What I don't see is how the variables MM and DD are set in here? I'm not even sure I see how the YY is set.
I know this works, but I cannot see how. It gets them from the DATE command, where it prompts for DD MM and YY ... or in other languages it may be different. Type in DATE without any parameters and press [Enter] and you will see the prompt. That is where it gets them from.
|