Windows:访问目录而不知道其名称

时间:2017-06-12 08:18:45

标签: windows batch-file directory

我正在编写一个Windows批处理脚本,我希望在其中访问.\build\whatetherthenameis\debug。但是,我收到的错误是:

The system can not find the given path

这个地方只能有一个目录,没有别的。这是一条生成的路径,遗憾的是中间的名称各不相同。

到目前为止,我已尝试过以下命令:

  • cd build\*\debug
  • build\~1\debug

2 个答案:

答案 0 :(得分:2)

for /f "delims=" %%a in ('dir /s/b/ad ".\debug"') do cd "%%a"

应该将您带到所需的目录。

以子目录的基本形式执行仅/ad目录/s/b的目录列表,查找debug。将找到的每个目录分配给%%a(您声称只有一个)并使用"delims="确保分配整个令牌。

答案 1 :(得分:2)

如果你知道目录只有1级深度,那么直接查找它可能会更快,而不是执行完全递归的树搜索。

@For /D %%A In (*) Do @If Exist "%%A\debug\" CD "%%A\debug"