2016-09-30 79 views
2

我想编写一个批处理文件来查找所有.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%'。 – 2016-09-30 03:08:38

+3

'dir/a/s“c:\ * 2.4 * .vsdm”'会尽快完成您想要的操作。 – 2016-09-30 03:09:54

+0

另请参阅'forfiles'命令。 (这有点像unix'find') – ths

回答

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

包括在echo文件名可能会发现更多。我没有看到双重否定方法的理由。代码运行的方式可以精确地取决于代码中指令所在的位置,例如,如果这些行包含在各种循环或代码块中,则操作可能取决于其他元素,因此,上下文以及预期和实际发生的例子。


正确的排版使所有的清楚。

关于delayed expansion有一两个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'循环中。 – 2016-09-30 07:19:28

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 
::*************************************************************************** 
相关问题