如何在批处理中找到特定文件名并更改到该目录? [英] How do I find a Specific File Name in Batch and change to that directory?
问题描述
我编写一个批处理文件,它将获取特定文件(curl_for_64bit.exe)的目录。我尝试使用find命令,但它不工作。
以下脚本搜索文件的目录,在根目录树 C:\ROOT\
中的文件 curl_for_64bit.exe
,并更改为父目录找到的文件实际位于:
for / Fdelims =%% F in('dir / B / A:-DC:\ROOT\curl_for_64bit.exe')do(
/ pre>
cd / D%%〜dpF
)
或直接输入到
cmd
中的此命令行:for / Fdelims =%F in('dir / B / S / A:-DC:\ROOT\curl_for_64bit.exe )do @cd / D%〜dpF
要了解
〜dp
修饰符代表变量
%% F
一个新的命令提示符窗口,键入for /?
并阅读帮助文本(特别是最后一节)。I am coding a batch file that will get the directory of a specific file(curl_for_64bit.exe). I tried using the find command but it does not work. It basically gets the directory of the file, changes to that directory so that it can be copied.
解决方案The following script searches the file
curl_for_64bit.exe
in the directory tree rooted atC:\ROOT\
and changes to the parent directory where the found file is actually located:for /F "delims=" %%F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do ( cd /D "%%~dpF" )
Or this command line to be typed directly into
cmd
:for /F "delims=" %F in ('dir /B /S /A:-D "C:\ROOT\curl_for_64bit.exe"') do @cd /D "%~dpF"
To learn what the
~dp
modifier of thefor
variable%%F
means and how it works, open a new command prompt window, typefor /?
and read the help text (see the last section in particular).这篇关于如何在批处理中找到特定文件名并更改到该目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!