Jump to content

RightPath - always run your script in the same directory


Luigi
 Share

Recommended Posts

This script has the following functions: 
* define or set a working directory to the script; 
* includes the function of _Singleton, preventing concurrent execution; 
* if you try to run the script from another directory other than the default directory is the default directory does not exist, it creates the default directory, copies itself to there, and runs; 
* if there is already the default directory and a file with the same name, it checks the versions of files: if you are trying to run is newer, it kills the current process (if running), copies the most current file to the default directory, and runs, if the same version or earlier, just run the file.
*you can call the script from any directory, but only the default executable directory is to be executed; 
* when the executable is out of your default directory, it is just a shortcut;
* update you script/app easily; 
* downgrad is not allowed;
 
obs: how to write English contains some error, suggest me a new way, I'll be happy to fix (I'm using Google Translator).
 
Best Regards,
Detefon
 
 
Example:
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=try.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Res_Fileversion=0.0.0.1
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_Language=1046
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;###############################################################
;#OBS: Always compile, use file version and singleton's string
;###############################################################

#include <RightPath.au3>
Global $SINGLETON = "RightPath"

Local $TRY = _RightPath("C:\Program Files (x86)\alfa\try.exe", $SINGLETON)

Local $hGui = GUICreate("v->" & FileGetVersion(@ScriptFullPath), 200, 100, -1, -1)
GUISetState(@SW_SHOW)

Local $hMsg

While Sleep(100)
    $hMsg = GUIGetMsg()
    If $hMsg == -3 Then Exit
WEnd

RightPath.au3

Edited by Detefon

Visit my repository

Link to comment
Share on other sites

RightPath.au3

#include-once
#RequireAdmin
#include <Misc.au3>
#include <Array.au3>
#include <File.au3>

; #FUNCTION# ====================================================================================================================
; Name...........: _RightPath
; Description ...: Performs version comparison between two files
; Syntax.........: _RightPath($sFullPath = 0, $sSingleton = 0)
; Parameters ....: $sFullPath       - full qualified path's file
;                  $sSingleton      - singleton string's file
; Return values .: Success          - Execute the script/app
;                  Failure          - 1 at least one or more input terms is empty
;                                   | 2 script is not compiled
;                                   | 3 input drive is not real or error
;                                   | 4 error on close proccess
; Author ........: Detefon
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: _RightPath("C:", "Program Files (x86)\alfa", "try.exe", 1)
; ===============================================================================================================================
Func _RightPath($sFullPath = 0, $sSingleton = 0)
    If Not @Compiled Then Return SetError(2, 0, 0)
    If Not $sFullPath Or Not $sSingleton Then Return SetError(1, 0, 0)

    Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
    Local $aPathSplit = _PathSplit($sFullPath, $sDrive, $sDir, $sFilename, $sExtension)
    Local $iSingleton = _Singleton($sSingleton, 1)

    If Not IsDeclared("__FULL_PATH") Then Global $__FULL_PATH = $sDrive & $sDir
    If Not IsDeclared("__APP") Then Global $__APP = $sFilename & $sExtension

    If (@ScriptFullPath == $__FULL_PATH & $__APP) Then
        Switch $iSingleton
            Case 0
                Exit
            Case Else
                Return 1
        EndSwitch
    EndIf

    Local $aDrives = DriveGetDrive("FIXED")
    If _ArraySearch($aDrives, $sDrive) == -1 Then Return SetError(3, 0, 0)

    Local $iCompare = __FileVersionCompare($__FULL_PATH & $__APP, @ScriptFullPath)
    Local $iAttempts = 0
    If $iCompare == 3 Then
        If $iSingleton == 0 Then
            Local $PID = ProcessExists($__APP)
            ProcessClose($PID)
            Do
                Sleep(1000)
                $iAttempts += 1
                If $iAttempts > 20 Then Return SetError(4, 0, 0)
            Until Not ProcessExists($PID)
        EndIf
        FileDelete($__FULL_PATH & $__APP)
        DirCreate($__FULL_PATH)
        FileCopy(@ScriptFullPath, $__FULL_PATH & $__APP, 1 + 8)
        OnAutoItExitRegister("__RightPath_ExecOnExit")
    EndIf
    Exit
EndFunc   ;==>_RightPath


; #FUNCTION# ====================================================================================================================
; Name...........: __FileVersionCompare
; Description ...: Performs version comparison between two files
; Syntax.........: __FileVersionCompare($file1, $file2)
; Parameters ....: $file1           - path's file1
;                  $file2           - path's file2
; Return values .: Success          - Integer: 1, 2 Or 3
;                                   | 1 $file1 is greater
;                                   | 2 $file1 and $file2 is equal
;                                   | 3 $file2 is greater
;                  Failure          - None
; Author ........: Detefon
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __FileVersionCompare($file1 = 0, $file2 = 0)
    If FileExists($file1) Then
        $file1 = FileGetVersion($file1)
    Else
        $file1 = "0.0.0.0"
    EndIf

    If FileExists($file2) Then
        $file2 = FileGetVersion($file2)
    Else
        $file2 = "0.0.0.0"
    EndIf

    $file1 = StringSplit($file1, ".", 2)
    $file2 = StringSplit($file2, ".", 2)

    For $ii = 0 To 3
        $file1[$ii] = Number($file1[$ii])
        $file2[$ii] = Number($file2[$ii])
    Next

    If $file1[0] == $file2[0] And $file1[1] == $file2[1] And $file1[2] == $file2[2] And $file1[3] == $file2[3] Then Return 2
    If $file1[0] > $file2[0] Then Return 1
    If $file1[0] >= $file2[0] And $file1[1] > $file2[1] Then Return 1
    If $file1[0] >= $file2[0] And $file1[1] >= $file2[1] And $file1[2] > $file2[2] Then Return 1
    If $file1[0] >= $file2[0] And $file1[1] >= $file2[1] And $file1[2] >= $file2[2] And $file1[3] > $file2[3] Then Return 1
    Return 3
EndFunc   ;==>__FileVersionCompare

; #FUNCTION# ====================================================================================================================
; Name...........: __FileVersionCompare
; Description ...: Performs version comparison between two files
; Syntax.........: __FileVersionCompare($file1, $file2)
; Parameters ....: $file1           - path's file1
;                  $file2           - path's file2
; Return values .: Success          - Integer: 1, 2 Or 3
;                                   | 1 $file1 is greater
;                                   | 2 $file1 and $file2 is equal
;                                   | 3 $file2 is greater
;                  Failure          - None
; Author ........: Detefon
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __RightPath_ExecOnExit()
    ShellExecute($__APP, "", $__FULL_PATH)
EndFunc   ;==>__RightPath_ExecOnExit

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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