#CS Name: _includeDir.au3 Developer: Timothy Bomer Copyright: Amarok Studios LLC 2016 Version: 1.0 Description: The purpose of this UDF is to dynamically include all files inside of a folder. It works for the most part, but I am still working on a couple of bugs. #CE #Include Global $mainUDF = "IncludeDirUDF" Global $includeLib = $mainUDF & "\" & "loadIncludes.au3" Global $tempLib = $mainUDF & "\" & "lib.txt" Global $includeRewrite = $mainUDF & "\rewrite.au3" Global $iDirHolder = "" Func _includeDir($iDir, $lineToInc = 1, $restart = True) If (checkInclude()) = 1 Then FileDelete($tempLib) return EndIf If NOT (FileExists($iDir)) Then MsgBox(16,"Directory Doesn't Exists | _includeDir","The directory " & $iDir & " does not exist!") return 0 EndIf $iDirHolder = $iDir initializeCheck() ; MsgBox(0,"Include Directory", "Attempting to include: " & $iDir) populateLib($iDir) populateIncLib() finalize($lineToInc, $restart) EndFunc Func checkInclude() FileOpen(@ScriptName, 0) For $i = 1 to _FileCountLines(@ScriptName) $checkLine = FileReadLine(@ScriptName, $i) If ($checkLine = '#Include "IncludeDirUDF\loadIncludes.au3"') Then return 1 EndIf Next EndFunc ; START Initialize Check Func initializeCheck() ; MsgBox(0,"Checking. . .", "Is this initialized?") If (FileExists($mainUDF)) Then If NOT (FileExists($includeLib)) Then isError(2) return EndIf ; MsgBox(0,"Initialized","The UDF has been initialized") Else isError(1) return EndIf EndFunc ; END Initialize Check ; START Library Population Func populateLib($iDir = $iDirHolder) ; MsgBox(0,"Populating","Attempting to populate the library") If (FileExists($tempLib)) Then ; MsgBox(0,"Temp File Found","The temporary library file has been found. Attempting to populate.") $tLibCont = _FileListToArray(@ScriptDir & "\" & $iDir & "\", "*") $iDirSize = $tLibCont[0] ; MsgBox(0,"Size of Included Directory", $iDir & " contains " & $iDirSize & " files to include!") $writeLib = FileOpen($tempLib, 1) While $iDirSize > 0 FileWriteLine($writeLib, '#Include "..\' & $iDir & '\' & $tLibCont[$iDirSize] & '"') $iDirSize -= 1 WEnd FileClose($writeLib) Else isError(3) return EndIf EndFunc ; END Library Population ; START Include Library Population Func populateIncLib() ; MsgBox(0,"Rewriting. . .", "Attempting to re-write the include library") #CS If (FileExists($includeLib)) Then FileDelete($includeLib) _FileCreate($includeLib) EndIf #CE FileOpen($tempLib, 0) For $i = 1 to _FileCountLines($tempLib) $line = FileReadLine($tempLib, $i) $reWriteLib = FileOpen($includeLib, 9) FileWriteLine($reWriteLib, $line) FileClose($reWriteLib) Next FileClose($tempLib) EndFunc ; END Include Library Population ; START Finalize Func finalize($lineToInc, $restart) _FileWriteToLine(@ScriptName, $lineToInc, '#Include "IncludeDirUDF\loadIncludes.au3"', False) If ($restart = True) Then runFile(@ScriptName) EndIf exit return EndFunc Func runFile($rFile) $file_loc = $rFile If @Compiled = 1 Then $file_exe = FileGetShortName(@AutoItExe & ' /AutoIt3ExecuteScript "' & $file_loc & '"') Run($file_exe) Else $file_au3 = FileGetShortName($file_loc) Run(@AutoItExe & " " & $file_au3, "", @SW_HIDE) EndIf EndFunc ; START Error Reporting Func isError($eFlag = "", $eMessage = "There was an error!") If ($eFlag = "") Then ; MsgBox(16,"ERROR", $eMessage) Exit EndIf If ($eFlag = 1) Then ; MsgBox(16,"Not Initialized","This UDF has not been initialized") DirCreate($mainUDF) Sleep(250) initializeCheck() return ElseIf ($eFlag = 2) Then ; MsgBox(16,"Missing File","Missing the include library!") _FileCreate($includeLib) initializeCheck() return ElseIf ($eFlag = 3) Then ; MsgBox(16,"Missing File", "Missing the temporary library! Creating it now!",3) _FileCreate($tempLib) populateLib() return EndIf EndFunc ; END Error Reporting