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

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

问题描述

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


解决方案

 为/ R C:如果\\文件夹%%一中(*)做%%〜NXA== file.txt的集p = %%〜dpnxa
如果定义P(
回声%P%
)其他(
回声未找到文件

如果您搜索发现,将变量%P%设置为文件,包括名和扩展名的完整路径的文件。

如果你只是想的路径(如无该文件的文件夹路径),然后使用设置P = %%〜DPA 代替。

注意:如果有超过1个文件具有相同名称,则变量将被设置为找到的最后一个。另外,环线后脚本是不是真的有必要,只是为了告诉你,如果发现任何东西:)

如果你想使用 DIR 命令然后用这个做,规则同样适用

  FOR / F令牌= *%%一个在('DIR的acad.exe / B / S')不设置P = %%一

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
)

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.

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

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 :)

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天全站免登陆