包含目录中的文件(在运行时)

时间:2016-10-13 19:01:23

标签: file include autoit

我想在我的脚本中包含文件夹中的文件(我尝试使用Google,但似乎无法通过AutoIt找到任何方法)。我想要实现的例子:

LoadFiles()

Func LoadFiles()
    $FL = _FileListToArray(@ScriptDir&"\Test\", "*")    
    $X=1

    Do    
        #include $FL[$X]  <== How ?    
        $X=$X+1 
    Until $X=$FL[0]
EndFunc

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:1)

要在已编译的脚本中包含文件,您需要FileInstall

FileInstall ( "source", "dest" [, flag = 0] )
  • source要编译的文件的源路径。这必须是文字字符串;它不能是变量或函数调用的结果。它可以是源文件(.au3)的相对路径(在路径中使用。\或.. \。)

  • dest如果仅使用目录,则带有反斜杠的文件的目标路径。这可以是变量。

  • flag [可选]此标志确定是否覆盖已存在的文件: $ FC_NOOVERWRITE(0)=(默认值)不覆盖现有文件 $ FC_OVERWRITE(1)=覆盖现有文件

另一种方式是adding the files as a resource

答案 1 :(得分:0)

下午好!目前还没有一种方法可以做你要求的事情。我一直致力于构建一个UDF来做你想做的事情,但我已经遇到了一些问题。我有一个工作原型,但它有一些错误。首先,下载此脚本并将其命名为_includeDir.au3。

<强> _includeDir.au3

#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 <File.au3>


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

要使用此UDF,请将文件包含在:

  

包括&#34; _includeDir.au3&#34;

接下来,按照以下格式调用该函数。

  

_includeDir(&#34;要包含的目录&#34;,$ lineToIncludeOn,$ restart)

要包含的目录将是包含您尝试包含的所有文件的目录名称。

$ lineToIncludeOn指定#Include将写入的脚本行。这是一个可选参数,默认为第1行。

最后,$ restart指定是否需要重新启动脚本。遗憾的是,最大的错误是需要重新启动脚本才能让UDF包含所有文件。这可能会剥夺整个脚本的有用功能。这是一个可选参数,默认情况下,它将设置为True并自动重新启动脚本。

以下是一个例子。

内部工作目录

  

包含文件夹

     

Example.au3

     

_includeDir.au3

INSIDE OF包含文件夹

  

One.au3

     

Two.au3

     

Three.au3

     

Four.au3

     

Five.au3

One.au3

$oneVar = "First variable"

Two.au3

$twoVar = "Second variable"

Three.au3

$threeVar = "Third variable"

Four.au3

$fourVar = "Fourth variable"

Five.au3

$fiveVar = "Fifth variable"

因此,我们将尝试将One.au3,Two.au3,Three.au3,Four.au3和Five.au3包含到Example.au3中。

Example.au3

; Exclude the numbers before the code. It's there just to show you the line the code is written on.

(1) #Include "_includeDir.au3"

(2)

(3) _includeDir("Includes Folder")

(4) MsgBox(0,"Included Variables","Variable One: " & $oneVar & @CRLF & "Variable Two: " & $twoVar & @CRLF & "Variable Three: " & $threeVar & @CRLF & "Variable Four: " & $fourVar & @CRLF & "Variable Five: " & $fiveVar)

这将添加以下行:

#Include "IncludeDirUDF\loadIncludes.au3"

到Examples.au3的第一行,然后重新启动Example.au3以显示包含文件中的变量。如果更改了Included Files目录中的文件,则需要删除loadIncludes.au3的#Include行,并删除生成的文件夹。 (IncludeDirUDF)。

我们假设您不希望将#Include写入Example.au3的第一行...要指定要将其写入的行,只需将下一个参数添加到函数调用。例如,我们想将它写到Example.au3的第5行,我们将使用它:

_includeDir("Includes Folder", 5)

最后一个参数是restart参数。这指定在包含目录后是否应重新启动Example.au3。默认设置为True。如果您希望Example.au3退出并保持终止,只需在函数调用结束时添加False即可。

_includeDir("Includes Folder", 5, False)

为了做你正在尝试做的事情,使用它的最好方法是将它放在包含正下方的Example.au3(或任何脚本)的顶部。这样做的原因是,如果脚本生成库,它将自动重启脚本,如果它不在顶部,则可能导致错误。我希望这篇写得不好的UDF会帮助你!让我知道,如果有什么事情你不需要它做。如果没有,请告诉我,我会解决它!快乐编程我的朋友!如果这太难以理解,请参阅官方AutoIt论坛HERE

上的更详细的演示

谢谢,

答案 2 :(得分:0)

如果有人遇到同样的问题,这就是我修复它的方法。 在运行脚本之前,只有IncludeList.au3必须在目录中存在,否则您将收到包含错误

#include <WinAPIFiles.au3>
#include <File.au3>


; Delete Old IncludeList.au3
If FileExists("IncludeList.au3") Then
FileDelete("IncludeList.au3")
EndIf

; Get Files From Dir
$IL = _FileListToArray(@ScriptDir&"\Functions\", "*")

; Create New IncludeList.au3
$FH = FileOpen("IncludeList.au3", $FO_APPEND)

; Check For Errors
If @error <> 1 or @error <> 4 Then

; Loop True Files In Dir
For $FC = 1 To UBound($IL)-1 Step +1

; Write New #Include '.\Function\FilesToInclude.au3'
FileWrite($FH, "#Include '.\Functions\"&$IL[$FC]&"'"& @CRLF)

Next
EndIf
; Close File Handler
FileClose($FH)

; Include All The Files In Directory True IncludeList We Created
#include "IncludeList.au3" 

; And Now You Can Call Any Functions From The Scripts From That Directory