Jump to content

_GetAppVer [like FileGetVersion to work with au3 file too]


Recommended Posts

The Scite4Autoit3 AutoITWrapper uses a directive

#AutoIt3Wrapper_Res_Fileversion=<Version Number>
to set your application version.

if your application is compiled you can get it from file get version, if not? this will help you...

Func _GetAPPVersion()
    ; author rajesh v r 
    Local $retVal
    If @Compiled Then
        $retVal = FileGetVersion(@ScriptFullPath, "FileVersion")
    Else
        Local $ScriptFileHandle, $Line 
        $ScriptFileHandle = FileOpen(@ScriptFullPath, 0)
        If $ScriptFileHandle = -1 Then
            SetError(@error, "File Could not be opened for reading")
            $retVal = ""
        Elseif StringInStr(FileReadLine($ScriptFileHandle, 1), "#Region ;**** Directives created by AutoIt3Wrapper_GUI ****") Then 
    
            While 1
                $Line = FileReadLine($ScriptFileHandle)
                    If StringInStr($Line,"#AutoIt3Wrapper_Res_Fileversion=" )Then 
                        If Not StringInStr($Line,"StringInStr($Line,") Then
                            $retVal = StringTrimLeft(StringStripWS($Line,3),32)
                            ExitLoop
                        Endif 
                        If StringInSTr($Line, "#EndRegion") then Exitloop
                        If @error = -1 Then ExitLoop ; may never be used, coz we dont loop till end of script!!!
                    EndIf
            WEnd
        EndIf
    EndIf
    Return $retVal
EndFunc   ;==>_GetAPPVersion

> Wont work if u are not using the autoit wrapper gui directives.

> wont work if u strip off all comment texts

> <Edit> sorry, forgot to mention, this needs the autoitwrapper directives to be at the top of the script. will soon update it.. thx for reminding me, storme.

Edited by rajeshontheweb
Link to comment
Share on other sites

How about with a RegExp? :D

#AutoIt3Wrapper_Res_Fileversion=0.002;comment allowed? Yes, so don't want it returned as part of version
Func _GetAPPVersion()
    Local $aVersion, $retVal
    If @Compiled Then
        $retVal = FileGetVersion(@ScriptFullPath, "FileVersion")
    Else
        Local $ScriptFile = FileRead(@ScriptFullPath)
        $aVersion = StringRegExp($ScriptFile, "(?mi)(?:^\s*\#AutoIt3Wrapper_Res_Fileversion\s*?=\s*)(.*)(?:;.*\r)", 3)
        If IsArray($aVersion) then $retVal = $aVersion[0]
        $ScriptFile = ""
        $aVersion = ""
    EndIf
    Return $retVal
EndFunc ;==>_GetAPPVersion

Admittedly, a FileReadLine() approach is probably faster on really long scripts...

Edit: Watch out for line-wrapping

Edited by ResNullius
Link to comment
Share on other sites

G'day rajeshontheweb

Great concept and I will be using it in future scripts to check version numbers. Currently I ignore version control if it's not compiled, which has masked some logic errors.

A couple of things to think on.

- "#Region ;**** Directives created by AutoIt3Wrapper_GUI ***" may not be the first line in the script. I'd suggest puting it inside the while loop so it can be detected where ever it is.

- There maybe spaces/tabs infront of "#AutoIt3Wrapper_Res_Fileversion=" so it maybe a good idea to trim these before checking. The Same with the version number it may have trailing spaces to that need to be removed.

Well Done

John Morrison

Link to comment
Share on other sites

strome,

2)

StringStripWS($Line,3)
does the stripping?? and thats the reason i didnt use stringleft, i use string in str instead.

1) one trouble was, for a basic start off, i had to let the user keep it at top. there is one bug i havent understood yet. when looping through full file contents of an active script, i didnt get to the end of file for a while !!! i tried to use file readline and post all to console, but after posting full file contents it still persists! so i have changed my mind to use some thing a little different. i am trying to use fileread to read full file contents if the first line doesnt contain the directives. but the reason i didnt use fileread in the first place was that if the script is big with too many lines, it will just waste the memory space. now that we have a regex check syntax (i am a big zero in regex) i will try to employ it.

Link to comment
Share on other sites

2)

StringStripWS($Line,3)
does the stripping?? and thats the reason i didnt use stringleft, i use string in str instead.
DOH! Sorry I missed that :-(

1) one trouble was, for a basic start off, i had to let the user keep it at top.

I'm playing with your code to see if there is an easy way to handle this. If I come up with something I'll let you know. :D

there is one bug i havent understood yet. when looping through full file contents of an active script, i didnt get to the end of file for a while !!!

I think this could be your problem.

You included

If StringInStr($Line, "#EndRegion") Then ExitLoop
If @error = -1 Then ExitLoop; may never be used, coz we dont loop till end of script!!!

INSIDE the

If StringInStr($Line,"#AutoIt3Wrapper_Res_Fileversion=" )Then

So the WHILE 1 would never have exited.

Anyway here is a modified verison of your script with the IF statements moved. See if this fixes your problem.

Func _GetAPPVersion()
    ; author rajesh v r 
    Local $retVal
    If @Compiled Then
        $retVal = FileGetVersion(@ScriptFullPath, "FileVersion")
    Else
        Local $ScriptFileHandle, $Line 
        $ScriptFileHandle = FileOpen(@ScriptFullPath, 0)
        If $ScriptFileHandle = -1 Then
            SetError(@error, "File Could not be opened for reading")
            $retVal = ""
        Elseif StringInStr(FileReadLine($ScriptFileHandle, 1), "#Region ;**** Directives created by AutoIt3Wrapper_GUI ****") Then 
    
            While 1
                $Line = FileReadLine($ScriptFileHandle)
                    If @error = -1 Then ExitLoop ; may never be used, coz we dont loop till end of script!!!
                    If StringInStr($Line,"#AutoIt3Wrapper_Res_Fileversion=" )Then 
                        If Not StringInStr($Line,"StringInStr($Line,") Then
                            $retVal = StringTrimLeft(StringStripWS($Line,3),32)
                            ExitLoop
                        Endif 
                    EndIf
                    If StringInSTr($Line, "#EndRegion") then Exitloop
            WEnd
        EndIf
    EndIf
    Return $retVal
EndFunc   ;==>_GetAPPVersion

but the reason i didnt use fileread in the first place was that if the script is big with too many lines, it will just waste the memory space.

I think the way you did it was fine. Reading an entire file into memory is a waste of time and memory. imho

now that we have a regex check syntax (i am a big zero in regex) i will try to employ it.

I'm Zero on regex as well.

But IMHO I think the regex version (though elegant) would be a waste of resources for the reasons stated above. IE it reads in the entire file for something that maybe found in he first few lines. OF course it all depends on the size of the file.

Hope this helps

John Morrison

Link to comment
Share on other sites

G'day rajeshontheweb

I couldn't help myself I did a bit of editing on your script to handle a few situations I thought it may encounter.

Improvements (I hope!) to script :

Removes ALL "white space" characters in the "AutoIt3Wrapper_Res_Fileversion" line

"#Region ;**** Directives created by AutoIt3Wrapper_GUI ****" can now be anywhere in script not just the first line

Files that don't have AutoIt3Wrapper_Res_Fileversion are handled

Ignores "#AutoIt3Wrapper_Res_Fileversion" anywhere except the start of a line

Here is the edited version of your script

Func _GetAPPVersion()
    ; author rajesh v r
    ; http://www.autoitscript.com/forum/index.php?showtopic=96654
    Local $retVal
    If @Compiled Then
        $retVal = FileGetVersion(@ScriptFullPath, "FileVersion")
    Else
        Local $ScriptFileHandle, $Line, $DirectivesNotFound
        $ScriptFileHandle = FileOpen(@ScriptFullPath, 0)
        If $ScriptFileHandle = -1 Then
            SetError(@error, "File Could not be opened for reading")
            $retVal = ""
        Else
            $DirectivesNotFound = True
            While 1
                $Line = FileReadLine($ScriptFileHandle)
                If @error Then ExitLoop ; may never be used, coz we dont loop till end of script!!!

                If $DirectivesNotFound Then
                    If StringInStr(StringStripWS($Line, 3), "#Region ;**** Directives created by AutoIt3Wrapper_GUI ****") Then
                        $DirectivesNotFound = False
                    EndIf
                Else
                    $Line = StringStripWS($Line, 8) ; Stripe ALL spaces from line
                    If StringLeft($Line, 32) = "#AutoIt3Wrapper_Res_Fileversion=" Then
                        $retVal = StringTrimLeft($Line, 32)
                        ExitLoop
                    EndIf
                    If StringInStr($Line, "#EndRegion") Then ExitLoop
                EndIf
            WEnd
        EndIf
    EndIf
    Return $retVal
EndFunc   ;==>_GetAPPVersion

Hope you don't mind. :D

Have fun!

John Morrison

Link to comment
Share on other sites

why do you search for #Region ;**** Directives created by AutoIt3Wrapper_GUI ****" ? You can add the Version-directive without the Wrapper-GUI and then this line does not exist.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

why do you search for #Region ;**** Directives created by AutoIt3Wrapper_GUI ****" ? You can add the Version-directive without the Wrapper-GUI and then this line does not exist.

Good Point!

I'd guess that most script writers would use the GUI though so it wouldn't be such a big problem. and those that don't use the GUI would know that the #Region statements have to be there for the script to work.

Also "rajeshontheweb" used the #Region statements to short circuit the search so that the entire file didn't need to be searched if there wasn't a #AutoIt3Wrapper_Res_Fileversion in the #region section.

BUT you do have a good point. :D

How about this version that handles both situations?

Func _GetAPPVersion()
    ; author rajesh v r
    ; http://www.autoitscript.com/forum/index.php?showtopic=96654
    Local $retVal
    If @Compiled Then
        $retVal = FileGetVersion(@ScriptFullPath, "FileVersion")
    Else
        Local $ScriptFileHandle, $Line, $DirectivesNotFound
        $ScriptFileHandle = FileOpen(@ScriptFullPath, 0)
        If $ScriptFileHandle = -1 Then
            SetError(@error, "File Could not be opened for reading")
            $retVal = ""
        Else
            $DirectivesNotFound = True
            While 1
                $Line = FileReadLine($ScriptFileHandle)
                If @error Then ExitLoop ; may never be used, coz we dont loop till end of script!!!

                If $DirectivesNotFound Then
                    If StringInStr(StringStripWS($Line, 3), "#Region ;**** Directives created by AutoIt3Wrapper_GUI ****") Then
                        $DirectivesNotFound = False
                    EndIf
                Else
                    If StringInStr($Line, "#EndRegion") Then ExitLoop
                EndIf
                If StringLeft(StringStripWS($Line, 8), 32) = "#AutoIt3Wrapper_Res_Fileversion=" Then
                    $retVal = StringTrimLeft($Line, 32)
                    ExitLoop
                EndIf
            WEnd
        EndIf
    EndIf
    Return $retVal
EndFunc   ;==>_GetAPPVersion

??????

John Morrison

Edited by storme
Link to comment
Share on other sites

Good Point!

I'd guess that most script writers would use the GUI though so it wouldn't be such a big problem. and those that don't use the GUI would know that the #Region statements have to be there for the script to work.

Also "rajeshontheweb" used the #Region statements to short circuit the search so that the entire file didn't need to be searched if there wasn't a #AutoIt3Wrapper_Res_Fileversion in the #region section.

Possibly you are right. If someone uses the directives without the GUI he should be able to edit this func, too :D So i don't know which one is better.

One last thing: you missed to include a FileClose.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Possibly you are right. If someone uses the directives without the GUI he should be able to edit this func, too :D So i don't know which one is better.

I prefer the second as it handles all of the situations without editing. :D

So if either situation (expert/novice) uses the script it should be handled.

One last thing: you missed to include a FileClose.

DAM :D

I'll blame that one on "rajeshontheweb" ;) ..... it is his code. I'm just messing with it. :P

Here is the new more-completerer version then the last complete version. ;)

Func _GetAPPVersion()
    ; author rajesh v r
    ; http://www.autoitscript.com/forum/index.php?showtopic=96654
    Local $retVal
    If @Compiled Then
        $retVal = FileGetVersion(@ScriptFullPath, "FileVersion")
    Else
        Local $ScriptFileHandle, $Line, $DirectivesNotFound
        $ScriptFileHandle = FileOpen(@ScriptFullPath, 0)
        If $ScriptFileHandle = -1 Then
            SetError(@error, "File Could not be opened for reading")
            $retVal = ""
        Else
            $DirectivesNotFound = True
            While 1
                $Line = FileReadLine($ScriptFileHandle)
                If @error Then ExitLoop ; may never be used, coz we dont loop till end of script!!!

                If $DirectivesNotFound Then
                    If StringInStr(StringStripWS($Line, 3), "#Region ;**** Directives created by AutoIt3Wrapper_GUI ****") Then
                        $DirectivesNotFound = False
                    EndIf
                Else
                    If StringInStr($Line, "#EndRegion") Then ExitLoop
                EndIf
                If StringLeft(StringStripWS($Line, 8), 32) = "#AutoIt3Wrapper_Res_Fileversion=" Then
                    $retVal = StringTrimLeft($Line, 32)
                    ExitLoop
                EndIf
            WEnd
        EndIf
    EndIf
    FileClose($ScriptFileHandle)
    Return $retVal
EndFunc   ;==>_GetAPPVersion

Thanks for the help!

John Morrison

Link to comment
Share on other sites

  • 1 year later...

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