批处理文件查找包含子字符串的文件名

4
我想写一个批处理文件来查找所有扩展名为.vsdm的文件,并且文件名必须包含子字符串"2.4"。但是我的代码告诉我,所有的.vsdm文件都包含了子字符串"2.4",这是错误的。
FOR /R %completepath% %%G IN (*.vsdm) DO (
set file=%%~nG

If not "%file%"=="%file:2.4=%" (
    echo Filename contains 2.4
) else (
    echo Filename does NOT contains 2.4
)
)

有人能告诉我哪里错了吗?谢谢。


2
请参考 For /?setlocal /? 中的 delayedexpansion。同时,set /? 也有相关讨论。基本上使用 !file! 而不是 %file% - user6017774
3
dir /a /s "c:\*2.4*.vsdm" 可以快速实现你想要的功能。 - user6017774
请参考 forfiles 命令。它有点像 Unix 系统的 find 命令。 - ths
2个回答

0
If "%file%"=="%file:2.4=%" (
    echo Filename "%file%" does NOT contain 2.4
) else (
    echo Filename "%file%" contains 2.4
)

echo中包含文件名可能会揭示更多信息。我看不出双重否定方法的理由。代码的操作方式可能取决于指令在代码中的位置,例如,如果这些行包含在任何类型的循环或代码块中,则操作可能取决于其他元素,因此在上下文中呈现代码并提供预期和实际发生情况的示例非常重要。

正确的格式使一切清晰明了。

有一两篇关于延迟扩展的SO文章,OP应该熟悉一下。

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %completepath% %%G IN (*.vsdm) DO (
set "file=%%~nG"

If not "!file!"=="!file:2.4=!" (
    echo Filename contains 2.4
) else (
    echo Filename does NOT contains 2.4
)
)
ENDLOCAL

1
代码的第一行(和最后一行)没有被格式化为代码。它在一个 FOR 循环中。 - user6017774
他要求找到而不是检查。 - Hassan Uddin
@HassanUddin:没错,但原始代码显然旨在检查for循环找到的文件名,并且叙述表明现有代码的问题似乎是检查字符串失败了。 - Magoo

0
您可以使用命令Where /?,该命令允许您使用通配符(?*)和UNC路径。
@echo off
Title Find the location of a file with substring by Hackoo
Color 0A
Call :inputbox "Enter the file name to search :" "Enter the file name to search"
If  "%input%" == ""  Color 0C & (
    echo(
    echo       You must enter a filename to continue with this program
    pause>nul & exit
) else (
    Call :Browse4Folder "Select the source folder to scan %input%" "c:\scripts"
)
Set "ROOT=%Location%"
::We check whether the input string has an anti-Slach in the end or no ? if yes, we remove it !
IF %ROOT:~-1%==\ SET ROOT=%ROOT:~0,-1%

set whereCmd=where.exe /r %ROOT% %input%
for /f %%a in ('%whereCmd%') do echo %%~nxa --^> %%a
pause & exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^) 
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::***************************************************************************
:InputBox
set "input="
set "heading=%~2"
set "message=%~1"
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do ( 
    set "input=%%a"
)
exit /b
::***************************************************************************

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接