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

时间:2016-09-30 02:44:20

标签: batch-file

我想编写一个批处理文件来查找所有.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 个答案:

答案 0 :(得分:0)

if(userChoice.defeats(computerChoice)){
    //handle user win here
}else if(computerChoice.defeats(userChoice){
   //handle computer win here
}else{
   //handle tie here
}

If "%file%"=="%file:2.4=%" ( echo Filename "%file%" does NOT contain 2.4 ) else ( echo Filename "%file%" contains 2.4 ) 中包含文件名可能会显示更多信息。我认为双重否定方法没有理由。代码操作的方式可能取决于指令在代码中的精确位置,例如,如果这些行包含在任何类型的循环或代码块中,则操作可能取决于其他元素,因此它很重要上下文中的代码,以及预期和实际发生的内容的示例。

正确的fomatting使一切都清楚。

有一两篇关于echo的SO文章,OP应该熟悉这些文章。

delayed expansion

答案 1 :(得分: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
::***************************************************************************