Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-25
4
Medium Priority
?
688 Views
Last Modified: 2012-06-27
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
Comment
Question by:defecta
  • 2
4 Comments
 

Author Comment

by:defecta
ID: 37763884
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
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 37764538
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 37840455
Does   time /t   show the same format? If not, use that instead of %time%.
0
 

Author Closing Comment

by:defecta
ID: 37858809
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Loops Section Overview
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question