使用批处理文件查找文件并返回完整路径 [英] Find file and return full path using a batch file

查看:26
本文介绍了使用批处理文件查找文件并返回完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个批处理文件来搜索文件名,然后返回其路径以便我可以在变量中使用它?

Is it possible to create a batch file that searches for a file name, then returns its path so I can use it in a variable?

推荐答案

for /r C:folder %%a in (*) do if "%%~nxa"=="file.txt" set p=%%~dpnxa
if defined p (
echo %p%
) else (
echo File not found
)

如果找到了您搜索的文件,它会将变量 %p% 设置为文件的完整路径,包括名称和扩展名.

If the file you searched for was found it will set the variable %p% to the full path of the file including name and extension.

如果您只想要路径(如没有文件的文件夹路径),请改用 set p=%%~dpa.

If you just want the path (as in the folder path without the file) then use set p=%%~dpa instead.

注意:如果同名文件超过 1 个,则变量将设置为最后一个找到的文件.此外,for 循环行之后的脚本并不是真正必要的,只是为了向您展示它是否找到了任何东西:)

Note: If there is more than 1 file with the same name then the variable will be set to the last one found. Also the script after the for loop line isn't really necessary, just to show you if it found anything :)

如果你想使用 dir 命令来做,那么使用这个,同样的规则适用

If you want to do it using the dir command then use this, same rules apply

for /f "tokens=*" %%a in ('dir acad.exe /b /s') do set p=%%a

这篇关于使用批处理文件查找文件并返回完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆