![](/img/trans.png)
[英]How do I store output in a variable from Windows “where” command, in a batch file?
[英]How do I find a directory in and store it into a variable, using a Windows batch file?
我需要找到特定目录的位置,然后将该目录路径存储到Windows批处理脚本中的变量中。
我还希望命令在找到匹配项时返回(避免在找到目录后搜索整个硬盘)。
到目前为止,我已经在命令行上尝试过了:
dir c:\ /s /b /ad | find "DirectoryName"
问题是,即使找到匹配项,它也会搜索整个驱动器。 另外,我仍然不知道如何将结果存储在批处理文件中的变量中。 应该只有一个结果。
基本上,我需要在Linux / bash上实现类似的功能:
export DIRPATH=`find / -name "DirectoryName" -print -quit`
感谢您的光临!
批处理中,您需要FOR /F
以获取命令的输出。
FOR /F "usebackq delims=" %%p IN (`dir c:\ /s /b /ad ^| find "DirectoryName"`) DO (
set "DIRPATH=%%p"
)
echo %DIRPATH%
由于find命令中有引号,因此您需要usebackq-option。 并且有必要转义管道字符一次,因为它应该管道dir命令,而不是for命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.