Batch file 使用通配符路径搜索文件
我想写一个脚本来提示用户输入文件路径并列出找到的所有文件。文件路径可以包含通配符。类似于。但是它的批处理脚本版本。例如:Batch file 使用通配符路径搜索文件,batch-file,path,Batch File,Path,我想写一个脚本来提示用户输入文件路径并列出找到的所有文件。文件路径可以包含通配符。类似于。但是它的批处理脚本版本。例如: C:\Somewhere\user*\app\version-*.*\start.exe 文件的位置可能如下所示: 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
C:\Somewhere\user*\app\version-*.*\start.exe
文件的位置可能如下所示:
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
我尝试使用<代码> < /COD>,结果比预期的要困难得多,因为 <代码>不支持路径中间的通配符。有没有办法列出这些文件?(可能不使用
for
?)您可以尝试使用该命令
WHERE
命令大致相当于UNIX的“which”命令。默认情况下,搜索在当前目录和路径中完成
@echo off
Where /R "%programfiles%" *winrar.exe
pause
我认为这个递归解决方案非常有效;您可以将其命名为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
EDIT:我修复了上一个for/F
命令中的一个小错误
例如,在我的Windows 8.1 64位计算机中,WCDIR.bat C:\Windows\Sys*\find*.exe
命令的输出为:
C:\Windows\System32\find.exe
C:\Windows\System32\findstr.exe
C:\Windows\SysWOW64\find.exe
C:\Windows\SysWOW64\findstr.exe
您可能希望使提供的示例更具体。你说的是列出所有找到的文件,但我可以告诉你,不用看你的计算机,它们都将被称为start.exe。您提供的路径等是否正确,或者您只是为了回答问题而编写的,希望以后自己修改它们?示例@Compo的哪一部分?是的,我让他们兴奋起来:这不管用。/R开关中给定的路径不能包含通配符。我尝试了
where/R C:\Windows\Sys*find*.exe
,得到了类似“错误:无效目录”的内容。这里是Windows8.1。为您使用此命令?@Aaciniwhere/R“C:\Windows\System32”*查找*.exe
此命令有效,此命令也有效where/R“C:\Windows\System32”*f*d*.exe
请问?本主题是关于“带有通配符路径的搜索文件”,也就是说,这里的问题是“<代码> < /COD>不支持路径中间的通配符”(如问题描述所指定的)。解决方案应该找到具有如下路径的文件:C:\Windows\Sys*\find.exe
。。。。。。(就像这个问题中的第一个例子:C:\where\user*\app\version-*.\start.exe
)
C:\Windows\System32\find.exe
C:\Windows\System32\findstr.exe
C:\Windows\SysWOW64\find.exe
C:\Windows\SysWOW64\findstr.exe