0

A part of my batch script involves creating a timestamp in the batch file. I am using the following code to get the date in yyyy-mm-dd format set mydate=!date:~10,4!-!date:~4,2!-!date:~7,2!

This when the setting on my pc is mm-dd-yy and date command returns The current date is: Mon 09/26/2016 and my above command converts it into 2016-09-26

but the problem is when i run my script on another machine which has a dd-mm-yy format where the date command returns this:

The current date is: 26-Sep-16 and my above command gives me this: -ep-16

How can i always get the date in my desired format (yyy-mm-dd) irrespective of the date format settings on the computer?

aschipfl
  • 28,946
  • 10
  • 45
  • 77
irish
  • 147
  • 1
  • 2
  • 13

1 Answers1

1

The command wmic os get localdatetime will give an output like

LocalDateTime
20160926085318.630000+120

you can than place the output in a var or directly split it to multiple variables.

For a single line output add the switch /value to the command above. Output will then look like this:

LocalDateTime=20160926085649.867000+120

To show the concept behind the function idea, I made up this:

@echo off
setlocal EnableDelayedExpansion
REM change this to what you would do usually in your program:
for /l %%m in (1,1,5) do (
timeout /t 1
call:getNewTimestamp
echo !timestamp!
)
pause
goto:eof

:getNewTimestamp
for /f "delims== tokens=1*" %%g in ('wmic os get localdatetime /value') do (
if ".%%g"==".LocalDateTime" (
REM Change this to the usual way to get your timestamp:
set timestamp=%%h
)
)
Goto:eof

So whenever you need the current timestamp, you want to call :getNewTimeStamp. This function will set !timestamp! to the desired value. You can then use the value as usual in your main part of the program.
So my example has a loop that it goes through 5 times, each time waiting a second, then calling getNewTimestamp and then echoing the value of !timestamp!.
The term function might be misleading here. It updates a script variable from within the same script, the goto:eof at the end in combination together with the call <functionName> - command, will result in updating the variable(s) accoring to the "functions script".

geisterfurz007
  • 3,390
  • 5
  • 29
  • 46
  • Cool, thanks but i have hit another issue, the time stamp and date remain constant throughout the log. Rather than an updated time stamp as events occur in the batch file, I only see the time and date that the batch file was started in place of every %time% and %date% instance. I looked up [http://stackoverflow.com/questions/11422182/properly-timestamp-a-log-file-created-by-batch-script] but even this solution isn't working for me. – irish Sep 28 '16 at 03:42
  • Here is my command: `for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j set timestamp=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%T%ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%` `ECHO !timestamp! - SWPath = %SWPATH% >> %SWPATH% ` – irish Sep 28 '16 at 03:45
  • Sample Output: There are few more echo's that i record in my logs but all of them have got the same time Example: `2016-09-28T10:00:36.947 - SWPath = C:\SW 2016-09-28T10:00:36.947 - SW Version = 1 2016-09-28T10:00:36.947 - Deleting 2016-09-28T10:00:36.947 - SW Updated 2016-09-28T10:00:36.947 - Done ` – irish Sep 28 '16 at 03:46
  • If you use the command in a block of paranthesis, you have to use exclamation-marks in the part where you build the timestamp-> `set timestamp=!ldt:~0,4!-!ldt:~4,2!-!ldt:~6,2!T!ldt:~8,2%:!ldt:~‌​10,2!:!ldt:~12,6!` . If you have not done so yet you can place the code you got in a subfunction `getNewTimestamp` or something like that and then `call` it to refresh the var `timestamp` if you do not want to use exclamation-marks. – geisterfurz007 Sep 28 '16 at 05:33
  • I have tried this as well `set timestamp=!ldt:~0,4!-!ldt:~4,2!-!ldt:~6,2!T!ldt:~8,2%:!ldt:~‌​‌​10,2!:!ldt:~12,6!1 but this doesn't seem to work, your sub function idea looks good... will give that a go !! Thanks... – irish Sep 28 '16 at 06:58
  • ok so i am a bit lost here again.... i did try to set the timestamp inside a subfunction "getNewTimestamp" but the problem is when i am trying to print something in my log `ECHO !timeStamp! - SW Path = %SW_PATH% >> %SW_PATH%/SW_Log.Log` how would i call that subfunction from here? I can call it but its a long script and when i would say `GOTO getNewTimestamp` the control would go there and then the script would start executing from that location again? I am printing stuff into the log in multiple location and so i am confused how would i do this using a subfunction? – irish Oct 04 '16 at 00:04
  • I did try `set timestamp=!ldt:~0,4!-!ldt:~4,2!-!ldt:~6,2! !ldt:~8,2!:!ldt:~10,2!:!ldt:~12,6!` but this also didn't work – irish Oct 04 '16 at 00:04
  • After a bit of testing and looking on my own, I put together a few lines and brought up something. See edit above; hope it helps :) – geisterfurz007 Oct 04 '16 at 00:39
  • Thanks for that, will test it out & get back to you. – irish Oct 04 '16 at 06:45
  • I went through your example but my concern remains the same, this was my understanding when your first advised this but my problem is that i do have a pretty long script and if we look at the example given by you how would i ensure that the control would return to the point of call? So for example: ` script line 1, script line 2, script line 3 - Call getnewtimestamp, script line 4 - Echo timestamp, script line 5 further logic. Now in in line 4 when it calls getnewtimestamp how does control returns to line 4 from :getnewtimestamp function and then continues execution of further logic/lines. – irish Oct 05 '16 at 00:26
  • In the above example how does control goes back to the first for loop where it echo timestamp? I don't see any call within getnewtimestamp function to return the control back to the first loop? That's my confusion? I dont understand that part. – irish Oct 05 '16 at 00:29
  • I have got it working but if you could please help me understand how call function works in batch? How does it know where to get back to in a batch script after it gets called? – irish Oct 05 '16 at 00:36
  • I have just put my timestamp logic under `:getNewTimestamp for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j set timestamp=!ldt:~0,4!-!ldt:~4,2!-!ldt:~6,2! !ldt:~8,2!:!ldt:~10,2!:!ldt:~12,6!` – irish Oct 05 '16 at 00:37
  • 2
    There is an important difference between `goto` and `call`. Do you see the `goto:eof` command at the end of my function in the above example? This marks the end of the function basically. In other programming languages, when you use a function you do not have to determine where to go back as well and this is the same for batch. It reaches the end of the file (either because of the command `goto:eof` or because your script ends) and this is the point where it automatically returns to the line where it gets called. Using `goto` instead would not work as it not goes back. – geisterfurz007 Oct 05 '16 at 03:40
  • Cool, got it !! So its the same as in any other prog language. Thanks a lot for the explanation and for your perseverance. – irish Oct 05 '16 at 04:04
  • I am not sure, in how far I can actually help you, as I am using batch around 2 months now (not very experienced as well), but I will try my best. – geisterfurz007 Oct 05 '16 at 08:30
  • ok cool got it :), will be sending an email soon !! Thanks – irish Oct 05 '16 at 09:09
  • @irish Just looked as this randomly and wanted to ask if there should have been a mail... I have not got any. – geisterfurz007 Nov 01 '16 at 13:54
  • hmm funny !! you should have. Let me send another one. – irish Nov 04 '16 at 03:17
  • Nope, nothing. I have set the mail-adress as cleartext on my so-profile. Additionally I added my twitter-profile if that helps.. – geisterfurz007 Nov 04 '16 at 06:41