循环批处理文件似乎正在运行一次额外的时间/迭代 [英] Batch file for loop appears to be running one extra time/iteration
问题描述
我正在尝试将批处理文件放在一起,以在系统上除光驱之外的每个逻辑驱动器上运行程序.它可以很好地滤除光盘驱动器,但是由于某些原因,我正在测试的已建立命令的回显会额外运行一段时间,但变量没有值.就像循环在空值上迭代额外的时间一样.想法?
I'm trying to put a batch file together to run a program on each logical drive on a system except for optical drives. It filters out the optical drives just fine but for some reason my echo of the built up command I am testing with runs one extra time except the variables have no values. It's like the loop is iterating an extra time on a null value. Ideas?
(IF NOT!drive!==%safe%只是确保我不为脚本所在的驱动器运行命令,而"safe"只是首字母缩写)
(The IF NOT !drive! == %safe% is just making sure I don't run the command for the drive the script lives on and "safe" is just an acronym)
for /F "usebackq skip=1 tokens=1,2" %%a IN (`wmic logicaldisk get DeviceID^,DriveType`) do (
SET drive=%%a
SET driveType=%%b
IF NOT !drive! == %safe% (
IF NOT !driveType! == 5 (
ECHO test commands
)
)
)
推荐答案
wmic
在其结果报告的末尾附加一个回车符.尝试这样的事情:
wmic
puts an additional carriage return at the end of its resulting report. Try something like this:
for /f "skip=1 tokens=1,2" %%a IN ('wmic logicaldisk get DeviceID^,DriveType ^| findstr /v /r /c:"^$"') do (
SET drive=%%a
SET driveType=%%b
IF NOT !drive! == %safe% (
IF NOT !driveType! == 5 (
ECHO test commands
)
)
)
注意:由于您的命令不包含正向滴答声,因此不需要usebackq
.此处的findstr
只是搜索空白行(即,正则表达式是行的开头,然后是行的末尾").
Note: usebackq
isn't required since your command doesn't include forward-ticks. The findstr
there just searches for a blank line (that is, the regex is "beginning of line followed by end of line").
这篇关于循环批处理文件似乎正在运行一次额外的时间/迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!