Solved

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

Posted on 2012-03-25
4
683 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 83

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 68

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

932 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

8 Experts available now in Live!

Get 1:1 Help Now