How can I use IF EXIST with a specific file type?
I'm trying to make a batch script that will perfotm one of two actions based on if a specified filetype is or is not present. I'm using If EXIST and IF NOT EXIST, but I can't get these to actually detect if the file type exists or not. Here's my code:
@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
:ENDIs this possible, how do I achieve my goal by declaring a specific file type or extension?
71 Answer
I think that you can use following command
SET IS-PROFILE=NO
FOR %%f IN (*.ini) do SET IS-PROFILE=YESand then test IS-PROFILE
As proposed in comment, you can break the loop using following command
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
) I have tested on my PC on Windows 10 and this work fine.
3