简体   繁体   English

Windows批处理:循环内设置的变量将被忽略

[英]Windows batch: variables set inside a loop are ignored

I'm using the following code inside a Windows batch (*.bat) file to run several SQL queries: 我在Windows批处理(* .bat)文件中使用以下代码来运行多个SQL查询:

set PSTR=QUERY1,QUERY2

for %%a in (%PSTR%) do (

  set START=%date%:%time%
  "C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -f "D:\SQL\%%a.sql" -h localhost -p 5432 -U postgres db_tom
  set END=%date%:%time%
  echo %%a  DEB %DEB%  FIN %FIN% >> "D:\LOGS\log.txt"

The echo writes the following string inside the log.txt file: 回声在log.txt文件中写入以下字符串:

QUERY1 START END
QUERY2 START END

The variables set inside the loop are ignored. 循环内设置的变量将被忽略。 Is there another way to write this batch in order to get start and end datetime written down? 还有另一种写此批处理的方法以便记下开始和结束日期时间的方法吗?

setlocal enabledelayedexpansion
for %%a in ...(
 set start=!date!:!time!
 ...
 set end=!date!:!time!
 echo !start! !end!
)

please consult any of the hundreds of SO items about delayed expansion - the setlocal enabledelayedexpansion invokes a mode where !var! 请咨询数百篇有关delayed expansion的SO文章setlocal enabledelayedexpansion调用!var! retrieves the run-time value not the parse-time value of the variable. 检索运行时值而不是变量的解析时值。

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

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