• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Replace blank with 0

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
bpyeo
Asked:
bpyeo
  • 8
  • 8
1 Solution
 
sirbountyCommented:
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
 
bpyeoAuthor Commented:
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
 
sirbountyCommented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
bpyeoAuthor Commented:
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
 
sirbountyCommented:
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
 
bpyeoAuthor Commented:
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
 
sirbountyCommented:
That's really weird...
I'm running xp - give me a bit to test this on the 2k3 server behind me...
0
 
bpyeoAuthor Commented:
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
 
bpyeoAuthor Commented:
Oops, delims=/ should change to delims=:
0
 
sirbountyCommented:
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
 
bpyeoAuthor Commented:
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
 
sirbountyCommented:
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
 
bpyeoAuthor Commented:
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
 
bpyeoAuthor Commented:
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
 
sirbountyCommented:
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
 
sirbountyCommented:
happy to help. :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now