If Else Statement for Program-files vs x86 variant Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
fohrums
Posts: 8
Joined: 07 Sep 2018, 05:39

If Else Statement for Program-files vs x86 variant

11 Apr 2021, 00:17

Hello AHK Community,

I am having trouble with running programs that may arbitrarily either be located in either of the following locations:

* Location 01 (Run):

C:/Program Files/Foo/Bar/2000.exe

* Location 02 (Run, If the above fails to launch with AHK error Message-box):

C:/Program Files (x68)/Foo/Bar/2000.exe

Notice the difference is the first is from Program-files 64-bit and the other is in Program-files 32-bit directory path. How to run other variant if one doesn't exist? This is because I get an error if the first path doesn't exist so that means it is then assumed it existst on the next variant.

I am thinking this is resolved with an "If statement" or "Else statement". Any ideas?
fohrums
Posts: 8
Joined: 07 Sep 2018, 05:39

Re: If Else Statement for Program-files vs x86 variant

25 Apr 2021, 02:35

Xtra wrote:
11 Apr 2021, 02:48
Use: FileExist()
This simply solved it (see below). Thank you.

Code: Select all

;;; Shows a message box if a file does not exist.

if !FileExist("C:\Temp\FlagFile.txt")
    MsgBox, The target file does not exist.
fohrums
Posts: 8
Joined: 07 Sep 2018, 05:39

Re: If Else Statement for Program-files vs x86 variant

17 Nov 2021, 16:07

I apologize for coming back to this thread. The problem is I don't have the way I was able to make this work using FileExist(). I haven't returned to it until the time came and was left stumped :think:. What ways could I use FileExist() to begin a run command and execute the next run command afterwards in case the first wasn't found?

I should've put the example down when I had it the first time. I let it slip by and now I don't have a reference to it. So, now I need help with getting this resolved once again.
User avatar
Bugz000
Posts: 93
Joined: 30 Sep 2013, 10:01

Re: If Else Statement for Program-files vs x86 variant

17 Nov 2021, 18:50

fohrums wrote:
17 Nov 2021, 16:07
I apologize for coming back to this thread. The problem is I don't have the way I was able to make this work using FileExist(). I haven't returned to it until the time came and was left stumped :think:
perhaps some context on the nature of the problem?
fohrums wrote:
17 Nov 2021, 16:07
What ways could I use FileExist() to begin a run command and execute the next run command afterwards in case the first wasn't found?

because the solution is above, you answered your own thread and came back to it, use fileexist to check if the file exists
Image
||-------[-HP-ML350E-G8-]-------||-[-32-core-xeon-]-||--[-48gb-ECC-]--||
||----[-Dell-Poweredge-r610-]---||-[-16-core-xeon-]-||--[-16gb-ECC-]--||
||-[-Lenovo-ThinkPad-x201-tab-]-||---[-4-core-i7-]--||-[-8gb-nonECC-]-||
||---------------------------[-shack--img-]---------------------------||
fohrums
Posts: 8
Joined: 07 Sep 2018, 05:39

Re: If Else Statement for Program-files vs x86 variant

19 Nov 2021, 18:00

flyingDman wrote:
11 Apr 2021, 00:56
Try "Try ...Catch": https://www.autohotkey.com/docs/commands/Try.htm
This inital reply worked the best. It is one command after another plain and simple just formatted in Braces and not a one-liner type code, so there's workspace using "Try-catch". Unfortunately I am unable to explain what made me say the response afterwards is the clear answer when I simply couldn't figure it out (seriously) :wtf: (idk, past is the past) :cookie: .

The simple Brace style code to answer (see below on) - How to run an alternative location of the same program (or another in case of an error) in a scenario where the first doesn't exist is using the "Try-catch" syntax when using Autohotkey :thumbup:.

Note: The error message for which the first brace doesn't exist will not display the error and skip to the next brace as you go.

Code: Select all

try
{
  Run "C:\Program Files (x86)\foo\bar.exe"
}
catch
{
  Run "C:\Program Files\foo\bar.exe"
}

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: If Else Statement for Program-files vs x86 variant

19 Nov 2021, 19:04

With a topic title like If Else Statement you had the answer right there.

Examples:

Code: Select all

if FileExist("C:\Program Files (x86)\foo\bar.exe")
    Run "C:\Program Files (x86)\foo\bar.exe"
else
    Run "C:\Program Files\foo\bar.exe"
or

Code: Select all

if FileExist("C:\Program Files (x86)\foo\bar.exe")
    Run "C:\Program Files (x86)\foo\bar.exe"
else if FileExist("C:\Program Files\foo\bar.exe")
    Run "C:\Program Files\foo\bar.exe"
else
    MsgBox, 4112, Error, bar.exe not found!
fohrums
Posts: 8
Joined: 07 Sep 2018, 05:39

Re: If Else Statement for Program-files vs x86 variant

21 Nov 2021, 07:58

Thank you very much Xtra. This code (previous post) seems a whole lot cleaner :clap:. I probably did that then, but didn't explain it :geek:.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, scriptor2016 and 354 guests