Jump to content

[Solved] Passing variables from one script to another


Recommended Posts

Hello folks,

I'm new to AutoIt and I love it!  I've used command line scripts for years, but needed something that's a little more robust.  I've been teaching myself AutoIt and have run into a snag that I hope you folks can help me with.

What I'm trying to do is have the script that was just run "Termed Data Copy.exe", check to see if a newer version of the file is available, then if so, update with a second script "Updater".

I'm trying to pass some variables from one script to another.  Here is the TDC script:  

; Declare some variables
Local $js = "\\bakertilly\firmdata\Data\IT\Apps\Jody Scripts\"
Local $ljs = "C:\Users\Public\Jody Scripts\"
Local $AppName = "Termed Data Copy"

; Check to see if this is the latest version of this script
Local $SrvCopy = FileGetVersion ($js & $AppName & "\" & $AppName & ".exe", "FileVersion")
Local $LocCopy = FileGetVersion (@ScriptFullPath)

If $SrvCopy <> $LocCopy Then
    Run ($ljs & "Updater.exe " & '"' & $AppName & '" "' & @ScriptFullPath & '"')
    Exit
EndIf

And here is the code in Updater:

Local $js = "\\bakertilly\firmdata\Data\IT\Apps\Jody Scripts"

FileCopy ('"' & $js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe" & '"' & ", " & '"' & $CmdLine[2] & '"', 1)

This doesn't work for some reason, but if I test without using variables, it works as expected.

FileCopy ("\\bakertilly\firmdata\Data\IT\Apps\Jody Scripts\Termed Data Copy\Termed Data Copy.exe", "C:\Users\jp12440\Documents\_JodyBuilt\AutoIt\Termed Data Copy.exe", 1)

The goal is to use this Updater script to be used for some other scripts, so I don't want to hardcode the location of the scripts.  Any ideas on what I should try?  Could there be a better way of transferring the variables that I'm not seeing?  I've tried many combinations of variables in the Updater script, but still isn't working.

Here is the MsgBox from the CmdLineRaw.  It's showing both $CmdLine[1] and $CmdLine[2]

CmdLineRaw.png

Edited by LeftHandedWave
I spel gud
Link to comment
Share on other sites

1 minute ago, ViciousXUSMC said:

A script can only reference itself for variables.

If you need to hand them off from one script to another (and cant combine the two scripts into one)

You could try something like an .ini file or saving/reading to the registry. 

This works fine to pass the variable to a MsgBox, but FileCopy dosen't work.  I would prefer using the Command Line Parameters from the help file, but for some reason, it's not working as listed.

In other words, why does this work:

MsgBox (0, "test", '"' & $js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe" & '"' & ", " & '"' & $CmdLine[2] & '"')

But this doesn't work:

FileCopy ('"' & $js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe" & '"' & ", " & '"' & $CmdLine[2] & '"', 1)

 

Link to comment
Share on other sites

The method is good, but you have an error in your FileCopy line. Please, try this one :

FileCopy ($js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe", $CmdLine[2], 1)

Also, make sure that the @ScriptFullPath is not running while the updater starts the copy (add a Sleep(200) or more because the FileCopy line)

 

Edit : another way to pass parameters to a script can be to use the environment variables using EnvSet + EnvGet

 

Edited by jguinch
Link to comment
Share on other sites

6 minutes ago, jguinch said:

The method is good, but you have an error in your FileCopy line. Please, try this one :

FileCopy ($js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe", $CmdLine[2], 1)

Also, make sure that the @ScriptFullPath is not running while the updater starts the copy (add a Sleep(200) or more because the FileCopy line)

 

Thank you!  I've been playing around with this for 5 hours.  

So, I don't need double quotes around a path with a space if it comes from a variable?  

Link to comment
Share on other sites

Here is the method using environment vars :

Main script :

; Declare some variables
Local $js = "\\bakertilly\firmdata\Data\IT\Apps\Jody Scripts\"
Local $ljs = "C:\Users\Public\Jody Scripts\"
Local $AppName = "Termed Data Copy"

; Check to see if this is the latest version of this script
Local $SrvCopy = FileGetVersion ($js & $AppName & "\" & $AppName & ".exe", "FileVersion")
Local $LocCopy = FileGetVersion (@ScriptFullPath)

If $SrvCopy <> $LocCopy Then
    EnvSet("_UPDATER_EXE", @ScriptFullPath)
    EnvSet("_UPDATER_NEW", $js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe")
    EnvSet("_UPDATER_PID", @AutoItPID)
    Run ($ljs & "Updater.exe)
    Exit
EndIf

Updater :

$source = EnvGet("_UPDATER_EXE")
$destination = EnvGet("_UPDATER_NEW")
$PID = EnvGet("_UPDATER_PID")

If Not FileExists($source) Or Not FileExists($destination) Or $PID = "" Then Exit

Do
    sleep(10)
Until Not FileExists($PID)

FileCopy($source, $destination, 1)

 

Link to comment
Share on other sites

4 minutes ago, jguinch said:

Here is the method using environment vars :

Main script :

; Declare some variables
Local $js = "\\bakertilly\firmdata\Data\IT\Apps\Jody Scripts\"
Local $ljs = "C:\Users\Public\Jody Scripts\"
Local $AppName = "Termed Data Copy"

; Check to see if this is the latest version of this script
Local $SrvCopy = FileGetVersion ($js & $AppName & "\" & $AppName & ".exe", "FileVersion")
Local $LocCopy = FileGetVersion (@ScriptFullPath)

If $SrvCopy <> $LocCopy Then
    EnvSet("_UPDATER_EXE", @ScriptFullPath)
    EnvSet("_UPDATER_NEW", $js & "\" & $CmdLine[1] & "\" & $CmdLine[1] & ".exe")
    EnvSet("_UPDATER_PID", @AutoItPID)
    Run ($ljs & "Updater.exe)
    Exit
EndIf

Updater :

$source = EnvGet("_UPDATER_EXE")
$destination = EnvGet("_UPDATER_NEW")
$PID = EnvGet("_UPDATER_PID")

If Not FileExists($source) Or Not FileExists($destination) Or $PID = "" Then Exit

Do
    sleep(10)
Until Not FileExists($PID)

FileCopy($source, $destination, 1)

 

Ahh... Thanks for explaining that.  I'll look into using the EnvSet command.

Link to comment
Share on other sites

It's just a way, not a definitive solution. The command line is good, too.

I often use the envget/envset way because the environment variables can store up to 32,767 characters, while the command line max limit is "only" 8 191 characters (sufficient in most cases)

 

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...