Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

Make the Time Variable Produce the Same Result Inside Batch


Recommended Posts

Hello friends,

I have a basic level at batch files. In one of my bacth file I use below variable for naming my backup file whose name is "2016.08.16-(08.30)-FLAMENT.rar"

set HM=%time:~0,2%.%time:~3,2%

echo (%HM%)

--produces--> (08.30)

The problem is that I want to use the same time (08.30) for the other file which related to the previous one. But since the time changes throughout the process in the same batch file,

%HM% produces (08.32), not (08.30) for the time changed. how can I use same time for all processes thoughout the batch file ?

I mean, I want %HM% to produce same result once the batch file runs until it finishes although the time changes. How can I do this ?

Best Regards
 

Link to post
Share on other sites

SET Now=%TIME%

set HM=%Now:~0,2%.%now:~3,2%

SET SomeTimeAgo=%HM%

echo (%HM%) --produces--> (08.30)

imagine here that a few minutes elapse

echo (%SomeTimeAgo%) --STILL produces--> (08.30)

as a general rule, DO NOT use . (dots) or () (brackets) in file names and - possibly - avoid also using spaces in them

2016_08_16-0830-FLAMENT.rar

is perfectly human readable as well and easier to parse programmatically, particurarly in batch.

jaclaz



 

Edited by jaclaz
  • Upvote 1
Link to post
Share on other sites

Hello Friends,

I have following variable inside a batch file, but somehow there is an extra space in the output as seen in the attached file. (I also added sample batch file)

I checked the possible spaces inside batch file but everything seems OK.

how can I prevent this space to happen ?

Edited 1:  I've just realized that sometimes it doesn't make any space and works well. So I don't understand when it works and if it works, how it works :wacko:

Edited 2: This time I've realized that it is about time format. when the hour is before 10:00, the time format doesn't show zero like 09.15. Instead it shows only 9.15 and puts a space instead of zero and this space which is put instead of zero causes the problem. So how can I solve this interesting problem ?

Best Regards

Note : I know that I shouldn't use dots and parenthesis for naming files when using batch file but I think this space isn't caused by dots or parenthesis.

set YMD=%date:~-4%.%date:~3,2%.%date:~0,2%
SET Now=%TIME%
set HM=%Now:~0,2%.%now:~3,2%
SET "BackUpTime=%HM%"

SET file=%YMD%-(%BackUpTime%)-FLAMENT.rar

echo %file%

2016.09.20-( 9.30)-FLAMENT.rar

space takes place between "(" and "9"

emptyvariable.png

extra space.cmd

Link to post
Share on other sites

Sure :), you are parsing incorrectly the result of time.

Managing dates and time in batch needs some attention, and - like always - you shouldn't invent things before having fully appreciated "previous art", go through this:
http://www.robvanderwoude.com/datetime.php

http://www.robvanderwoude.com/datetimentparse.php

If you are using 24h clock but your local does not provide a leading 0, but rather a space:
 

Quote


Parse the time, ensure leading zeroes:
SET Now=%Time: =0%
SET Hours=%Now:~0,2%
SET Minutes=%Now:~3,2%


and - AGAIN - DO NOT use brackets in the names of files, before or later you will be bitten by that, if you really want to use brackets, use square ones [] or curly ones {} (which are not a  "special" character in batch).

jaclaz


 

Link to post
Share on other sites

Dear Jaclaz, :)

Thank you again. These subjects are a little complicated for me, so sometimes I prefer to use template solutions and for such cases I ask my questions here.

this time I chanced my template as follows:

set YMD=%date:~-4%.%date:~3,2%.%date:~0,2%
SET Now=%Time: =0%
set HM=%Now:~0,2%.%now:~3,2%
SET BackUpTime=%HM%

SET file=%YMD%-[%BackUpTime%]-FLAMENT.rar

echo %file%

--> 2016.09.21-[07.53]-FLAMENT.rar

I think it is OK! but what I don't understand is why did you add followings after SET Now=%Time: =0% ?

I wonder this because I couldn't see any %Hours% or %Minutes% variables in the afterwards usage. This is why I excluded.

SET Hours=%Now:~0,2%
SET Minutes=%Now:~3,2%

Best Regards

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...