我正在编写一个batch file,将获得一个特定的文件(curl_for_64bit.exe)的目录。 我尝试使用find命令,但它不起作用。 它基本上得到的文件的目录,更改到该目录,以便它可以复制。
您可以使用FOR
命令遍历目录树,检查是否存在所需的文件,直到找到
for /r /d %%a in (*) do ( if exist %%a\curl_for_64bit.exe ( pushd %%a goto :eof ) )
你可以使用像@Ken White这样的命令,然后使用其他代码来获取文件的目录。
这是我的代码
rem you should go to the specific root directory(like cde etc.) cd /dc:\ dir /s /a /b curl_for_64bit.exe >tmp.txt set /P file_path=<tmp.txt del tmp.txt cd /d %file_path%\..
如果你只是想复制这些文件,为什么你要去文件的目录?
以下脚本搜索以C:\ROOT\
根的目录树中的文件curl_for_64bit.exe
,并更改为找到的文件实际位于的父目录:
for /F "delims=" %%F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do ( cd /D "%%~dpF" )
或者这个命令行直接输入到cmd
:
for /F "delims=" %F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do @cd /D "%~dpF"
要了解变量%%F
~dp
修饰符的含义及其工作方式,请打开一个新的命令提示符窗口,键入for /?
并阅读帮助文本(具体见上一节)。