Solved

Replace blank with 0

Posted on 2005-05-17
16
201 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
  • 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 250 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

I guess it is not common knowledge to most Wintel engineers/administrators: If you have an SNMP-based monitoring system in your environment (and it's common to have SNMP or Syslog) it's reasonably easy to enable monitoring of the Windows Event logs,…
Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

20 Experts available now in Live!

Get 1:1 Help Now