使用通配符路径搜索文件 [英] Search file with wildcard path

查看:393
本文介绍了使用通配符路径搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个脚本来提示用户输入文件路径并列出所有找到的文件.文件路径可以包含通配符.类似于.但是它的批处理脚本版本.例如:

I want to write a script to prompt user for file path and list all files found. The file path can contain wildcards. Something similar to this. But the batch script version of it. For example:

C:\Somewhere\user*\app\version-*.*\start.exe

文件的位置可能是这样的:

The files might be located like this:

C:\Somewhere\user345\app\version-1.0\start.exe
C:\Somewhere\user898\app\version-1.2\start.exe
C:\Somewhere\user898\app\version-1.3\start.exe

我尝试使用FOR,但事实证明这比预期的要难得多,因为FOR在路径中间不支持通配符.
有没有办法列出这些文件? (也许不使用for?)

I tried to use FOR and it turns out to be so much harder than expected because FOR does not support wildcards in the middle of a path.
Is there a way to list these files? (Maybe without using for?)

推荐答案

我认为此递归解决方案效果很好;您可以将其命名为 WCDIR.bat :

I think this recursive solution works pretty well; you may name it WCDIR.bat:

@echo off
setlocal

if "%~1" neq "" set "next=%~1" & goto next
echo Show files selected by several wild-cards
echo/
echo WCDIR wildcardPath
echo/
echo Each folder in the path may contain wild-cards
echo the last part must be a file wild-card
goto :EOF

:next
for /F "tokens=1* delims=\" %%a in ("%next%") do set "this=%%a" & set "next=%%b"
if defined next (
   for /D %%a in ("%this::=:\%") do (
      setlocal
      cd /D "%%~a" 2>NUL
      if not errorlevel 1 call :next
      endlocal
   )
) else (
   for /F "delims=" %%a in ('dir /B /A:-D "%this%" 2^>NUL') do echo %%~Fa
)
exit /B

编辑:我在上一个for /F命令中修复了一个小错误.

EDIT: I fixed a small bug in the last for /F command.

例如,在Windows 8.1 64位计算机上,WCDIR.bat C:\Windows\Sys*\find*.exe命令的输出为:

For example, the output of WCDIR.bat C:\Windows\Sys*\find*.exe command in my Windows 8.1 64-bits computer is:

C:\Windows\System32\find.exe
C:\Windows\System32\findstr.exe
C:\Windows\SysWOW64\find.exe
C:\Windows\SysWOW64\findstr.exe

这篇关于使用通配符路径搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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