Solved

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

Posted on 2012-03-25
4
685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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 70

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
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 viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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