Reputation: 1397
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
Upvotes: 0
Views: 212
Reputation: 1108
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!
Upvotes: 2
Reputation: 1397
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:
How you should proceed?
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>
Upvotes: 1
Reputation: 3122
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.
Upvotes: 1