Jump to content

_InstallMyApp UDF


JohnOne
 Share

Recommended Posts

This code was to help me install files needed for a project which is regularly being updated with different files.

If you drop the below .au3 file into your AutoIt3 project folder and run it, it will generate the code needed to install all the files and folders into the correct places for executable deployment.

Blah blah.. here's the code...

#include <File.au3>

Global Const $MyAppName = "MyAppName" ; >>> You Must Alter This <<<
Global Const $MyAppNameIni = $MyAppName & ".ini"

_FileInstallAid()

Func _FileInstallAid()
    ;I filtered out .au3 and .bak files for my needs, simple to change filter if needed
    $a = _FileListToArrayRec(@ScriptDir, "*|*.au3;*.bak", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_RELPATH)
    $s = '_InstallMyApp()' & @CRLF & @CRLF
    $s &= 'Func _InstallMyApp()' & @CRLF
    $s &= @TAB & 'If Int(IniRead("' & $MyAppNameIni & '", "' & $MyAppName & '", "Installed", "0")) Then' & @CRLF
    $s &= @TAB & @TAB & 'Return' & @CRLF
    $s &= @TAB & 'EndIf' & @CRLF
    $s &= @TAB & '_CreateFolders()' & @CRLF
    $s &= @TAB & '_FileInstall()' & @CRLF
    $s &= @TAB & 'IniWrite("' & $MyAppNameIni & '", "' & $MyAppName & '", "Installed", "1")' & @CRLF
    $s &= 'EndFunc' & @CRLF & @CRLF
    $s &= _CreateFoldersAid()
    $s &= 'Func _FileInstall()' & @CRLF & @TAB
    For $i = 1 To Int($a[0])
        $s &= 'FileInstall("' & @ScriptDir & "\" & $a[$i] & '", @ScriptDir & "\' & $a[$i] & '")' & @CRLF & @TAB
    Next
    $s = StringTrimRight($s, 1)
    $s &= "EndFunc" & @CRLF & @CRLF
    ClipPut($s)
    Exit MsgBox(0, "Complete", "Code in Clipboard", 3)
EndFunc   ;==>_FileInstallAid

Func _CreateFoldersAid()
    $a = _FileListToArrayRec(@ScriptDir, "*|*.au3", $FLTAR_FOLDERS, $FLTAR_RECUR, $FLTAR_NOSORT)
    For $i = Int($a[0]) To 1 Step -1
        If $a[$i] == "BackUp" Then
            _ArrayDelete($a, $i)
        EndIf
    Next
    $a[0] = UBound($a) - 1
    $s = 'Func _CreateFolders()' & @CRLF
    $s &= @TAB & 'Local $aFolders[' & UBound($a) - 1 & ']' & @CRLF
    For $i = 1 To Int($a[0])
        $s &= @TAB & '$aFolders[' & $i - 1 & '] = "' & $a[$i] & '"' & @CRLF
    Next
    $s &= @TAB & 'For $i = 0 To Ubound($aFolders) - 1' & @CRLF
    $s &= @TAB & @TAB & 'If Not FileExists(@ScriptDir & "\" & $aFolders[$i]) Then' & @CRLF
    $s &= @TAB & @TAB & @TAB & 'DirCreate(@ScriptDir & "\"  & $aFolders[$i])' & @CRLF
    $s &= @TAB & @TAB & 'EndIf' & @CRLF
    $s &= @TAB & 'Next' & @CRLF
    $s &= 'EndFunc' & @CRLF & @CRLF
    Return $s
EndFunc   ;==>_CreateFoldersAid

I've done it many times, working on a project which uses many folders and files, then when I come to create an executable, I have to either zip all the folders and files with the exe or start writing the FileInstall code.

Many people will say, it's easier to zip them, better to add them as resources or base64 binary embedded. And they might all be correct.

But regardless of that it's just personal preference for me.

If you need a code generator for just one folder then >see here.

Below is a zipped file with some ready made files and folders for you to test with if you wish.

How to use:

It's quite simple, when your script is working how you want, then clear any crap out of the project folder (yes, it should be in its own folder, if it is in say the root c drive then you'll be distributing all those files on that drive NOT GOOD)

Copy the InstallAid.au3 to you project root folder and run it.

Generated code will be in your clipboard.

Paste it into your main script file, move the globals near the top, and the functions wherever you like, be sure to add the name of your application, and you should move the "_InstallMyApp()" function to be ran before any other code except globals.

As per my ethic the code is very very basic and should be simple to follow.

EDIT: well it appears basic here, only because the real code is in the _FileListToArrayRec function, Thanks to big M23.

EDIT2: below is an example of the code generated within the attached test folder.

_InstallMyApp()

Func _InstallMyApp()
    If Int(IniRead("MyAppName.ini", "MyAppName", "Installed", "0")) Then
        Return
    EndIf
    _CreateFolders()
    _FileInstall()
    IniWrite("MyAppName.ini", "MyAppName", "Installed", "1")
EndFunc

Func _CreateFolders()
    Local $aFolders[4]
    $aFolders[0] = "Folder1"
    $aFolders[1] = "Folder2"
    $aFolders[2] = "Folder3"
    $aFolders[3] = "Folder1\Folder1SubFolder"
    For $i = 0 To Ubound($aFolders) - 1
        If Not FileExists(@ScriptDir & "\" & $aFolders[$i]) Then
            DirCreate(@ScriptDir & "\"  & $aFolders[$i])
        EndIf
    Next
EndFunc

Func _FileInstall()
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\RootFile1.txt", @ScriptDir & "\RootFile1.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\RootFile2.txt", @ScriptDir & "\RootFile2.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\Folder3\Folder3File1.txt", @ScriptDir & "\Folder3\Folder3File1.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\Folder2\Folder2File1.txt", @ScriptDir & "\Folder2\Folder2File1.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\Folder2\Folder2File2.txt", @ScriptDir & "\Folder2\Folder2File2.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\Folder1\Folder1File1.txt", @ScriptDir & "\Folder1\Folder1File1.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\Folder1\Folder1File2.txt", @ScriptDir & "\Folder1\Folder1File2.txt")
    FileInstall("C:\Documents and Settings\John\Documents\MY_CODE\AU3\TestScripts\MyAppInstallFolder\Folder1\Folder1SubFolder\Folder1SubFolderFile1.txt", @ScriptDir & "\Folder1\Folder1SubFolder\Folder1SubFolderFile1.txt")
EndFunc

MyAppInstallFolder.zip

Edited by JohnOne

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