?
Solved

Replace blank with 0

Posted on 2005-05-17
16
Medium Priority
?
239 Views
Last Modified: 2011-09-20
Hi Experts, currently I am writing a batch file to capture the time using %time%. If it is in the morning say 7am, then I want to have a return value of 07 instead of currently it returns me <blank>7. However if it is in the afternoon and evening, then it is OK as it is a double digit hour.

Please advice me on how this could be done.

TIA.
0
Comment
Question by:bpyeo
[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
  • 8
  • 8
16 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 14017483
If I run the following, it records the leading 0...

for /f %a in ('time/t') do set sTime=%a
(in a batch, replace one % with 2 %%).
0
 

Author Comment

by:bpyeo
ID: 14017530
Hi sirbounty, I have tried to run your script and it does not work. I have an error that says "The syntax of the command is incorrect".

I have tried to run "time /t" from the prompt and I have realized that if it is in the afternoon or evening, it outputs as 07:00 PM. However I need to have a 24 hour clock value (00-23).

Please advice.

TIA.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 14017543
That would be set in your control panel, regional options.
Set it there and try again.
Please post back with the exact statement you're using.
Thanx.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:bpyeo
ID: 14017578
Sorry that I miss another % in the script. The script runs but it does not produce the output that I want. I want to be able to extract a 24-hour time in a 2-digit hour. I have used %time% and it returns me a 24-hour clock but when it is in the morning say 7am, it returns me <blank>7 and I want 07 instead. I have went to the regional setting and the time is being set to hh:mm:ss tt and when I inspect the value using %time%, it still returns me <blank>7.

Please advice.

TIA.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 14017626
Go to Regional options again.
Customize
Time tab
Time format should be HH:mm:ss (note the case of the hour).

Did you want the minutes as well?
to only pull the hour:

for /f "delims=:" %%a in ('time/t') do set sTime=%%a
0
 

Author Comment

by:bpyeo
ID: 14017685
Hi Sirbounty, I have configured the regional setting and when I run time /t in command prompt, it gives me the correct output as 19:00. However when I run the script, it still gives me 07:00 when I type in echo %sTime% in the command prompt.

What went wrong? Please advice.

TIA.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 14017834
That's really weird...
I'm running xp - give me a bit to test this on the 2k3 server behind me...
0
 

Author Comment

by:bpyeo
ID: 14017887
Hi Sirbounty, I have discovered that ('time/t') is not executed. I have typed in set sTime= in the command prompt, run the script and when I type echo %sTime%, it returns me the value %sTime%. But I have managed to extract the hours using the following:

for /f "tokens=1-3 delims=/" %%a in ("%time%") do (
  set sTime=%%a
)

and the problem is the <blank>7 instead of 07. That is all I need to solve, ie to replace the <blank> with 0. Is there any way to do it in batch file?

TIA.
0
 

Author Comment

by:bpyeo
ID: 14017892
Oops, delims=/ should change to delims=:
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 14017922
Do this first...(tested fine on my 2k3).

Start->Run->CMD
At the prompt type:

TIME /T <Enter>

Please let me know the results.

If I use echo %time% I don't get the leading 0 either...

I'll work on another option, but feel this should be working this way...
0
 

Author Comment

by:bpyeo
ID: 14017938
Hi Sirbounty, when I type time /t in the command prompt, I obtain 07:00 or 19:00 which is correct output. It seems that the script is not able to obtain this information.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 1000 total points
ID: 14017969
I'm not sure how you're getting 19:00 or are you temporarily changing the time?
Try this from a command line:

for /f %a in ('time/t') do echo %a <Enter>
It should return
08:45 (my time)

If that works (not clear if you want the minutes seperate?) then try
for /f %a in ('time/t') do set sTime=%a <Enter>
echo  %sTime%

Post back with the results...
0
 

Author Comment

by:bpyeo
ID: 14018055
Hi Sirbounty, it works in the command prompt but using the same statement and place it in the script (I have placed another %), it does not work.

As for the time, I have manually set it for testing purpose. My current time is 21:00.

TIA.
0
 

Author Comment

by:bpyeo
ID: 14018083
Hi Sirbounty, thanks for your help. I have found my mistake. I have typed in } instead of ) in my script, and how it works perfectly. Thanks for your prompt response and appreciate your assistance. Here are all your points for your good effort.

Thanks.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 14018105
So your batch file has this:

for /f "delims=:" %%a in ('time/t') do set sTime=%%a

If it works from the command line, it should certainly work in the batch file.

Try altering it a bit for testing:
(only use one % for the variable)

for /f "delims=:" %%a in ('time/t') do (
echo %%a
set sTime=%%a
echo sTime is now %sTime%
)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 14018110
happy to help. :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Learn about cloud computing and its benefits for small business owners.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

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