Solved

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

Posted on 2012-03-25
4
682 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
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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 68

Expert Comment

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

Author Closing Comment

by:defecta
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now