简体   繁体   中英

Batch %time% env variable - Issue

I have issue with printing of environment variable %time% inside of FOR loop. I'm no sure if it is related to setlocal enabledelayedexpansion but it seems to be.

I am trying to figure this out in CMD.exe not in .bat script.

Without setlocal enabledelayedexpansion it looks like:

C:\Users\dabaran\Desktop\cs50\src\C>for /L %i in (1,1,3) do echo [%time%]

C:\Users\dabaran\Desktop\cs50\src\C>echo [21:47:27,19]
[21:47:27,19]

C:\Users\dabaran\Desktop\cs50\src\C>echo [21:47:27,19]
[21:47:27,19]

C:\Users\dabaran\Desktop\cs50\src\C>echo [21:47:27,19]
[21:47:27,19]

C:\Users\dabaran\Desktop\cs50\src\C>

If I try to use setlocal enabledelayedexpansion it looks like below:

C:\Users\dabaran\Desktop\cs50\src\C>Setlocal EnableDelayedExpansion

C:\Users\dabaran\Desktop\cs50\src\C>for /L %i in (1,1,3) do echo !time!

C:\Users\dabaran\Desktop\cs50\src\C>echo !time!
!time!

C:\Users\dabaran\Desktop\cs50\src\C>echo !time!
!time!

C:\Users\dabaran\Desktop\cs50\src\C>echo !time!
!time!

C:\Users\dabaran\Desktop\cs50\src\C>

I've tried to use timeout as well in order to create some delayed between different iterations of for loop but time is still not updated.

Don't understand why time variable is not being expanded !time! but instead stored as string.

Thank you.

Best Regards, Damian Baran

As pointed above, by default setlocal applies only in batch file. But if you want to use it in command prompt, you could create & modify the registry entry and use it with cmd.exe. See below.

EnableDelayedExpansion can also be set in the registry under HKLM or HKCU:

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"DelayedExpansion"= (REG_DWORD)
1=enabled 0=disabled (default)

Example -

C:\>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor" /V Delayedexpansion

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
    Delayedexpansion    REG_DWORD    0x1

C:\>for /L %i in (1,1,5) do echo !time!

C:\>echo !time!
54:23.9

C:\>echo !time!
54:23.9

C:\>echo !time!
54:23.9

C:\>echo !time!
54:23.9

C:\>echo !time!
54:23.9

With default reg setting

C:\>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor" /V Delayedexpansion

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
    Delayedexpansion    REG_DWORD    0x0


C:\>for /L %i in (1,1,5) do echo !time!

C:\>echo !time!
!time!

C:\>echo !time!
!time!

C:\>echo !time!
!time!

C:\>echo !time!
!time!

C:\>echo !time!
!time!

As in setlocal /? :

Begins localization of environment changes in a batch file ...

So, setlocal command has no effect directly on command prompt, hence exclamation marks aren't nothing but a String in your case.

If you enable the delayed expansion and use exclamation marks within a batch-file it will work as well.

Okay, I think I found an answer to my comment question here: Expanding environment variables for command prompt

Thank you for your great and prompt help/reply Rafael.

To summarize:

  1. setlocal work locally for batch files only (as it is in docu setlocal /?)
  2. if you want to test delayed expansion in command prompt directly you need to start cmd.exe with correct argument as described on above URL.

How you should proceed?

  1. Open cmd.exe with setlocal enabledelayedexpansion with following command (Windows Run): cmd.exe /V:ON
  2. Test your code which uses delayed expansion

My output looks good now:

C:\Windows\system32>for /L %i in (1,1,5) do echo !time!

C:\Windows\system32>echo !time!
22:56:37,71

C:\Windows\system32>echo !time!
22:56:37,73

C:\Windows\system32>echo !time!
22:56:37,77

C:\Windows\system32>echo !time!
22:56:37,78

C:\Windows\system32>echo !time!
22:56:37,79

C:\Windows\system32>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM