Not possible creating a variable comma in batch if using %time%

436 Views Asked by At

I was working to a script that I needed to read the time from the system with seconds but not milliseconds. I noticed that some computers have a different region settings that did caused me a few problems like the date from one computer is shown as 01/01/2020, the other one with 01.01.2020 using the command %date% etc.

Same thing if I use the command %time%, some computers is shown a time like 12:13:14,15, some like 12:13:14.16.

Now, to solve this problem I have to create a variable to read the separator symbol and output the correct one. For the command date It was simple.

For time I have this command:

FOR /F "tokens=3 delims=0123456789" %%A IN ('echo %time%') DO set timeSeparatorSymbol=%%A

If the computer have the symbol for separator for milliseconds as . then the output is:

timeSeparatorSymbol=.

Which is good.

If the computer have the symbol for separator for milliseconds as , then the output is:

timeSeparatorSymbol=

As you see, the variable is not created and I'm struggling figure it out why the comma doesn't accept it as a variable.

I need this for the following script to works to all computers no matter the regional settings:

for /f "tokens=3 delims=:%timeSeparatorSymbol%" %%A in ('echo %time%') do set second=%%A

Thank you in advance.

3

There are 3 best solutions below

4
Compo On

Had you searched this site, you'd have found a huge amount of questions and answers requring a method of returning dates and/or times in a consistent format.

The method below, shows one such method of getting the current time in hh:mm:ss format, it uses robocopy.exe as linked by @Mofi in their recent comment:

@Set "TimeStamp="
@For /F "Tokens=2" %%A In (
    '^""%__AppDir__%Robocopy.exe" /NJH /L "\|" Null^"'
) Do @If Not Defined TimeStamp Set "TimeStamp=%%A"
@Echo=%TimeStamp%
@Timeout 5

If you don't want the 'complication' of being robust, try this:

For /F "Tokens=2" %%A In ('RoboCopy /NJH /L "\|" Null^|Find ":"') Do Set "TimeStamp=%%A"


If you only wanted to retrieve the seconds, i.e ss, (it is unclear from your question):

@Set "Seconds="
@For /F "Tokens=4Delims=: " %%A In (
    '^""%__AppDir__%Robocopy.exe" /NJH /L "\|" Null^"'
) Do @If Not Defined Seconds Set "Seconds=%%A"
@Echo=%Seconds%
@Timeout 5

…and once again without the 'complication':

For /F "Tokens=4Delims=: " %%A In ('RoboCopy /NJH /L "\|" Null^|Find ":"') Do Set "Seconds=%%A"
3
Compo On

After re-reading your question, and based upon my interpretation that your only issue is the difference in separator types between seconds and milliseconds, and that they are always either , or ., I would offer this alternative:

For /F "Tokens=3 Delims=:" %%A In ("%TIME:,=.%") Do Set "second=%%~nA"

If you were wanting the timestamp as hms, then perhaps this will suffice:

For %%A In ("%TIME:,=.%") Do Set "TimeStamp=%%~nA"

If you just want to ensure that %TIME% always uses a . instead of , just use the variable directly like this: %TIME:,=.%

0
Stephan On

Your question has much room for speculation and interpretation.

As I understand, you need the date/time separators to build a valid date/time string.

You claim the format is always hh:mm:ss?xx so you can easily extract the desired separator (?) with:

set "timeSeparatorSymbol=%time:~8,1%"

same for date you claim it's "always DD?MM?YYYY):

set "dateSeparatorSymbol=%date:~2,1%"

Of course this doesn't work when the date format is different from your claim (like for example Mon, 27.01.2020. %date:~-5,1% works as long as the date-string ends with a 4 digit Year and there is a separator before)