sorry in advance if theres a thread on here to answer my question, I've been stuck on this since friday and was searching around. I did find some similar-ish problems but none I could apply or adapt to this (to my knowledge).
In short, I have a batch script, that creates a text file of a directory, the script then searches for a file within the directory. If the files theres it closes out, if the file isnt there it runs an installer.
My script atm is looking like this:
@echo off
dir "C:\SomeDirectory" > DIRECTORY.txt
timeout 5 >Nul
findstr "SomeProgram.exe" DIRECTORY.txt
if ErrorLevel = 0 (
@echo Program Found! > SCN.txt
)
else (
@echo Program Not Found! >SCN.txt
start ahkbin.exe
)
Im getting two issues when i'm running this.
(1) The else condition always prints to the .txt file specified, even when the directory exists, and the string in the directory.txt is there. If I switch the conditions around the same occurs in vice versa.
(2) No matter where "start ahkbin.exe" is placed, it executes. if placed in both IF and ELSE it executes twice.
I feel like I'm doing something clearly incorrect in how i've structured the IF\ELSE loop or how i'm using information from the text files, or else i'm just incorrectly using a batch file.
Any input would be greatly appreciated, Thanks in advance, LMacs.
there is a better way to do it:
if exist "C:\SomeDirectory\SomeProgram.exe" (
echo Program Found!> SCN.txt
) else (
echo Program Not Found!>SCN.txt
start ahkbin.exe
)
Notes:
if
- else
syntax is picky: the first (
has to be on the same physical line as if
.
) else (
has to be on the very same physical line.
The spaces are mandatory.
The command is echo
, not @echo
. The @
just supresses command repetition for that line. Not needed here, as echo off
completely turns off command repetition (The first line of a batch script is usually @echo off
: "turn off command repetition for following lines and also don't do it for this line")