3

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.

LMacs
  • 57
  • 3
  • 10
  • Just tried it there, same issue persisting. Thanks! – LMacs Oct 31 '17 at 12:54
  • 1
    Open up a cmd prompt and type: **if /?** You will then see the proper syntax for a comparison and how to use the else clause correctly. It will also show you how to use **if errorlevel** correctly – Squashman Oct 31 '17 at 12:55
  • 2
    `if exist "C:\SomeDirectory\SomeProgram.exe" (echo found) else (echo not found)` – Stephan Oct 31 '17 at 13:01
  • @Squashman Just went through that there, thanks I was using ErrorLevel wrong. – LMacs Oct 31 '17 at 13:09
  • @Stephan That actually works a charm, and eliminates the .txt file usage. Thank you! – LMacs Oct 31 '17 at 13:10
  • 1
    BTW the proper way to fix your code is `if %ErrorLevel% == 0 (` and the ELSE line should be `) else (` – Aacini Oct 31 '17 at 13:12
  • @Aacini Its working now that i've changed that, was the way I had my else structured causing the interpreter to not see it as an else statement and just execute it after the if regardless? – LMacs Oct 31 '17 at 13:25
  • Exactly! A line that start in `else` command imply an isolated `else` command, in the same way of all commands. Is the right paren placed in the same line the link to a previous left paren that belongs to an `if` command. This is true even if the whole `if-then-else` is placed in a single long line: `if %c% equ true (echo True) else echo False` – Aacini Oct 31 '17 at 13:30
  • Okay okay, I can see exactly why the batch file was behaving like it was in that case. Thank you! – LMacs Oct 31 '17 at 13:34

1 Answers1

1

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")

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks for the reply! The code you showed above was how I ended up re-writing my script yesterday, all running smoothly now! Also thank you for the clarification on the use of '@' I wasnt fully sure on when/when not to use it. – LMacs Nov 02 '17 at 12:43