Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

Windows Server 2003: batch file troubleshooting. %time% returning the same time at every query.

Hi guys,

I am trying to write a script that will automatically insert today's date and time into a file name without illegal characters for a filename. I am using the advice and code borrowed from here. http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us

Here is the code I am using in a batch file being run on a Windows Server 2003 STD 32 bit.

@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%b%%a)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
echo %mydate%_%mytime%

Open in new window


The problem I am having is the line in the code that is supposed to return the time in 24 hour format is returning the exact same time in 12 hour format every time. This really weird.

C:\Program Files\Windows Resource Kits\Tools>For /F "tokens=1-2 delims=/:" %a in
 ("0955 AM") do (set mytime=%a%b )

C:\Program Files\Windows Resource Kits\Tools>(set mytime=0955 AM )

C:\Program Files\Windows Resource Kits\Tools>echo 0955 AM
0955 AM

The exact same code run on my Windows 7 box returns the current time in 24 hour format as expected.
Has anyone else heard of or encountered this problem before?
0
defecta
Asked:
defecta
  • 2
1 Solution
 
defectaAuthor Commented:
I have a second Windows 2003 server, which when running the same batch file returns the proper 24 hour time. So it would appear to be something with the server config. hmmm.

both have the same patches and updates applied AFAIK. Maybe I should double check that.
0
 
oBdACommented:
Try the code below; it uses WMI to query the time, which is not independent of profile specific settings:
@echo off
setlocal enabledelayedexpansion
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
REM *** At this point, the variables Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
REM *** Month, Day, Hour, Minute, Second have leading zeros if less than 10.
set MyDate=%Year%%Month%%Day%
set MyTime=%Hour%%Minute%
echo My time stamp: %MyDate% %MyTime%

Open in new window

0
 
QlemoC++ DeveloperCommented:
Does   time /t   show the same format? If not, use that instead of %time%.
0
 
defectaAuthor Commented:
I have just come back to this problem and now since daylight savings has finished the problem no longer exists with the original code I supplied. =/

The code that oDBa supplied also works fine so. If that works independent of profile specific settings that seems like a solution to use.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now