如何将 IF EXIST 与特定文件类型一起使用?

如何将 IF EXIST 与特定文件类型一起使用?

我正在尝试编写一个批处理脚本,该脚本将根据指定的文件类型是否存在来执行两个操作之一。我正在使用If EXISTIF NOT EXIST,但我无法让它们真正检测文件类型是否存在。这是我的代码:

@ECHO OFF
IF NOT EXIST *.ini GOTO :NOPROFILE
IF EXIST *.ini GOTO :IMPORT

:INSTALL
COLOR 0B
TITLE Profile Installer
MODE CON COLS=43 LINES=2
cls
SET /P MENU="Do you want to install this profile (Y/N)? "
IF /I "%MENU%" EQU "Y" GOTO :IMPORT
IF /I "%MENU%" EQU "N" GOTO :CANCELLED
IF /I "%MENU%" EQU "Y" GOTO :INVALID
IF /I "%MENU%" NEQ "N" GOTO :INVALID

:IMPORT
MKDIR "%USERPROFILE%\Documents\Dolphin Emulator\Config\Profiles\Wiimote"
COPY "*.ini" "%USERPROFILE%\Documents\Dolphin Emulator\Config\Profiles\Wiimote"
COPY "*.txt" "%USERPROFILE%\Documents\Dolphin Emulator\Config\Profiles\Wiimote"
GOTO :DONE

:INVALID
CLS
SET msgboxTitle=Error!
SET msgboxBody=Please enter one of the above menu options...
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
Error.exe "%tmpmsgbox%"
GOTO :INSTALL

:DONE
CLS
SET msgboxTitle=Notice
SET msgboxBody=The profile has been successfully installed.
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
Error.exe "%tmpmsgbox%"
GOTO :OPEN

:OPEN
%SYSTEMROOT%\explorer.exe "%USERPROFILE%\Documents\Dolphin Emulator\Config\Profiles\Wiimote"
GOTO :END

:CANCELLED
CLS
SET msgboxTitle=Notice
SET msgboxBody=Installation cancelled, closing...
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
Error.exe "%tmpmsgbox%"
GOTO :END

:NOPROFILE
CLS
SET msgboxTitle=Error!
SET msgboxBody=No INI profile detected, closing...
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
Error.exe "%tmpmsgbox%"
GOTO :END

:END

这可能吗,我如何通过声明特定的文件类型或扩展名来实现我的目标?

答案1

我认为你可以使用以下命令

SET IS-PROFILE=NO
FOR %%f IN (*.ini) do SET IS-PROFILE=YES

然后测试 IS-PROFILE

正如评论中所建议的,您可以使用以下命令中断循环

SET IS-PROFILE==NO
FOR %%f IN (*.ini) do (
set IS-PROFILE=YES
echo %%f - File containing INI exists
exit /b
)

IF '%IS-PROFILE%'=='NO' (
echo NO File contains INI type 
)       

我已经在我的 Windows 10 电脑上测试过并且运行良好。

相关内容