Solved

Replace blank with 0

Posted on 2005-05-17
16
191 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:bpyeo
Comment Utility
Oops, delims=/ should change to delims=:
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
happy to help. :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

by Batuhan Cetin Within the dynamic life of an IT administrator, we hold many information in our minds like user names, passwords, IDs, phone numbers, incomes, service tags, bills and the order from our wives to buy milk when coming back to home.…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

12 Experts available now in Live!

Get 1:1 Help Now