Jump to content

Run command - passing it a variable


Recommended Posts

My script is pretty basic, I am running an application and have all my controlclicks working properly, etc, but then I found out that the application I'm going to run might not always be in the same directory.

Before I discovered that, I was doing a simple statement to start things off:

run("c:\Program Files\2020\catmanager.exe") <-- that worked just fine, started my "catmanager.exe" program up and away we go. But since that .exe might be in different locations on different machines, I used another method to find the app, write its location to a file (c:\kworking\catmanager-location.txt), now I'm trying to read in that file, load a variable with the full path to the executable, and then Run using that Variable. I used the fileopen chunk from an example script, I get the Popup that shows my $line variable has the right path and executable in it, but nothing happens. I know I must be just missing something on the Run command, but I'm not sure what.

I've tried putting Quotes around my Variable name in the run command, that didn't get it... I added the "Global $line" part as I thought my variable might not be making it out of the loop.

Here's my script:

; Script Start - Add your code below here

Opt("TrayAutoPause",0)

Global $line

$file = FileOpen("c:\kworking\catmanager-location.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open c:\kworking\catamanager-location.txt.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

MsgBox(0, "Line:", $line)

Wend

FileClose($file)

Run($line)

WinWaitActive("20-20 Catalog Tools")

Link to comment
Share on other sites

Is there any chance that there's more in the text file than just the one line with the file path? If there is, you might be reading the wrong line just before you exit the loop. You should have your MsgBox command outside the While...Wend loop to see what the variable $line contains before executing the run command. The only time your MsgBox will execute is when the variable $line ISN'T the last line of the file.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

If you KNOW the file is going to start with the path to the program, read one line from the file and close it. No need to iterate through it until EOF.

Script Start - Add your code below here
Opt("TrayAutoPause",0)

Global $line

$file = FileOpen("c:\kworking\catmanager-location.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open c:\kworking\catamanager-location.txt.")
Exit
EndIf

$line = FileReadLine($file)
MsgBox(0, "Line:", $line)
FileClose($file)
Run($line)
WinWaitActive("20-20 Catalog Tools")

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I should have added an error check in that in case the file didn't start with the file path, something along the lines of this:

While 1
    $line = FileReadLine($file)
    If @error > -1 Then
        If StringLeft($Line, 2) = "C:" Then ExitLoop
    Else
        MsgBox(0,"Error", "Path not found in the file")
        Exit
    Endif
Wend
MsgBox(0, "Line:", $line)

This is untested code but should do the trick to make it more foolproof.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...