Solved

Replace blank with 0

Posted on 2005-05-17
16
227 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 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