darksimoon Posted September 16, 2016 Posted September 16, 2016 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
jaclaz Posted September 16, 2016 Posted September 16, 2016 (edited) 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 September 16, 2016 by jaclaz 1
darksimoon Posted September 16, 2016 Author Posted September 16, 2016 Dear Jaclaz, I thank you a million times for this. You helped me again as before. thanks to information you provide me, I will be able to improve my batch file much more efficiently. thank you again. Best Regards
darksimoon Posted September 20, 2016 Author Posted September 20, 2016 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 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" extra space.cmd
jaclaz Posted September 20, 2016 Posted September 20, 2016 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
darksimoon Posted September 21, 2016 Author Posted September 21, 2016 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
jaclaz Posted September 21, 2016 Posted September 21, 2016 I didn't add anything, I simply quoted the example on the page I pointed you to: http://www.robvanderwoude.com/datetimentparse.php (which is something that you should have already found ) Of course you should adapt the example to your specific usage . jaclaz
darksimoon Posted September 21, 2016 Author Posted September 21, 2016 (edited) Thank you very much Jaklaz. I adapted my batch file accordingly thanks to your kind help. And it works like charm Edited September 21, 2016 by darksimoon
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now