简体   繁体   English

批次%time%环境变量-问题

[英]Batch %time% env variable - Issue

I have issue with printing of environment variable %time% inside of FOR loop. 我在FOR循环中打印环境变量%time%有问题。 I'm no sure if it is related to setlocal enabledelayedexpansion but it seems to be. 我不确定它是否与setlocal enabledelayedexpansion有关,但似乎与此有关。

I am trying to figure this out in CMD.exe not in .bat script. 我试图在CMD.exe而不是.bat脚本中弄清楚这一点。

Without setlocal enabledelayedexpansion it looks like: 没有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 [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: 如果我尝试使用setlocal enabledelayedexpansion,则如下所示:

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. 我也尝试使用超时,以便在for循环的不同迭代之间创建一些延迟,但是时间仍未更新。

Don't understand why time variable is not being expanded !time! 不明白为什么时间变量没有被扩展! but instead stored as string. 而是存储为字符串。

Thank you. 谢谢。

Best Regards, Damian Baran 最好的问候,达米安·巴兰(Damian Baran)

As pointed above, by default setlocal applies only in batch file. 如上所述,默认情况下,setlocal仅适用于批处理文件。 But if you want to use it in command prompt, you could create & modify the registry entry and use it with cmd.exe. 但是,如果要在命令提示符下使用它,则可以创建和修改注册表项,并将其与cmd.exe结合使用。 See below. 见下文。

EnableDelayedExpansion can also be set in the registry under HKLM or HKCU: 也可以在HKLM或HKCU下的注册表中设置EnableDelayedExpansion:

[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 /? 如在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. 因此,setlocal命令对命令提示符没有直接影响,因此,在您的情况下,感叹号不过是一个字符串而已。

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. 感谢您的及时帮助和答复Rafael。

To summarize: 总结一下:

  1. setlocal work locally for batch files only (as it is in docu setlocal /?) setlocal仅在本地处理批处理文件(就像在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. 如果要直接在命令提示符下测试延迟扩展,则需要使用上述URL中所述的正确参数启动cmd.exe。

How you should proceed? 您应该如何进行?

  1. Open cmd.exe with setlocal enabledelayedexpansion with following command (Windows Run): cmd.exe /V:ON 使用以下命令(Windows运行)使用setlocal enableelayedexpansion打开cmd.exe: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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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