1

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

:END

Is this possible, how do I achieve my goal by declaring a specific file type or extension?

7
  • Try IF EXIST "*.ini". Try also with the folder path.
    – harrymc
    Commented Feb 1, 2019 at 13:12
  • I already tried wrapping the extension definiton in quotes @harrymc, no dice. Commented Feb 1, 2019 at 13:14
  • 1
    Your above lines do work here, without seeing your compplete code it's difficult to help. Without a path it checks the current folder, not neccessarily the one the batch resides in.
    – LotPings
    Commented Feb 1, 2019 at 13:25
  • I'm checking the working directory @LotPings (`.`). I tried using the path variable as well but it changed nothing. This is just an initial check I'm running in my script. Commented Feb 1, 2019 at 13:51
  • 1
    It's IMO still unclear where you want to check for the *.ini files, in case you want to refer to the same folder where the batch file is, use If exist "%~dp0*.ini" GOTO :IMPORT
    – LotPings
    Commented Feb 8, 2019 at 22:30

1 Answer 1

1

I think that you can use following command

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

and 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
  • 1
    instead of looping through all the files you can just set the variable and break early, or jump to a place that contains the "exist" branch
    – phuclv
    Commented Feb 1, 2019 at 14:02
  • 1
    Yes, but breaking a FOR loop in DOS is tricky
    – schlebe
    Commented Feb 1, 2019 at 14:20
  • 1
    the OP is using Windows, not DOS, and in cmd there are many ways to exit a for loop. It's often used to get the first result in a list or the first line in a text filefile. Exiting out of a FOR loop in a batch file?
    – phuclv
    Commented Feb 1, 2019 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .