【问题标题】:Skip a part of a for loop跳过 for 循环的一部分
【发布时间】:2014-04-27 08:52:49
【问题描述】:

我正在尝试创建一个批处理脚本来记录找到的hello.txt 实例的位置。问题是我可以%workingroot% 和/或%startingloc% 中都有一份hello.txt

在脚本中,我试图让它检查它是否在%workingroot%%startingloc% 中。但是当它被触发并转到标签:skip 时,它似乎忘记了它仍在for 循环中,而是继续执行脚本。

for /f "delims=" %%f in ('dir /s /b /a-d "%searchloc%\hello.txt"') do (
if "%%f"=="%workingroot%" goto skip
if "%%f"=="%startingloc%" goto skip
echo Instance found.
set /a foundd=!foundd!+1
echo %%f>> "%templl%\wefound.txt"
:skip
)

有没有办法解决这个问题?

【问题讨论】:

  • 您不想计算%workingroot%%startingloc% 上的hello.txt 实例?
  • 不,我已经知道它们存在。
  • 会不会是 %%f 实际上从来没有 %workingroot%%startingpoint% 值?你有回音%%ft 吗?
  • 你说得有道理——我现在就去检查一下。
  • 原来他们不一样。我已经纠正了它,它现在可以正常工作了。谢谢! :)

标签: windows batch-file for-loop goto


【解决方案1】:
for /f "delims=" %%f in ('dir /s /b /a-d "%searchloc%\hello.txt"') do (
 if not "%%f"=="%workingroot%" if not "%%f"=="%startingloc%" (
  echo Instance found.
  set /a foundd=!foundd!+1
  echo %%f>> "%templl%\wefound.txt"
 )
)

应该适合你。

【讨论】:

  • 我不知道为什么,但我仍然在wefound.txt 中获得%workingroot%%startingloc% 的位置...
  • if not "%%~dpf"=="%workingroot%" if not "%%~dpf"=="%startingloc%" (
  • 排除是文字 - exclude-me 变量中的值必须与 %%f 中的值完全匹配。您还没有显示这些变量的实际内容 - 它可能是整个文件名(然后匹配%%f)或者它可能只是一个路径名,在这种情况下您应该匹配%%~dpf,正如 foxidrive 建议的那样。请注意,即使在这里,它也是区分大小写的(可以通过将/i 开关添加到if 来改变它)并且%%~dpf 将包含一个终端``。真的,在不确切知道变量的内容是什么的情况下,有很多可能性。
  • 是的,原来%%f 不匹配%workingroot%%startingloc%。我修复了它,它现在运行良好。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 2021-04-08
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多