【问题标题】:Include files from directory (at run-time)包含目录中的文件(在运行时)
【发布时间】:2017-02-23 01:06:11
【问题描述】:

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

LoadFiles()

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

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

谁能指出我正确的方向?

【问题讨论】:

    标签: file include autoit


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      下午好!目前没有一个很好的方法来做你所要求的。我一直在努力构建一个 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,请包含以下文件:

      包括“_includeDir.au3”

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

      _includeDir("要包含的目录", $lineToIncludeOn, $restart)

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

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

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


      这是一个例子。

      工作目录内部

      包括文件夹

      Example.au3

      _includeDir.au3

      INSIDE OF 包含文件夹

      One.au3

      两个.au3

      三.au3

      四.au3

      五.au3

      One.au3

      $oneVar = "First variable"
      

      两个.au3

      $twoVar = "Second variable"
      

      三.au3

      $threeVar = "Third variable"
      

      四.au3

      $fourVar = "Fourth variable"
      

      五.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 行,并删除生成的文件夹。 (包括DirUDF)。

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

      _includeDir("Includes Folder", 5)
      

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

      _includeDir("Includes Folder", 5, False)
      

      为了做你想做的事,使用它的最佳方法是将它放在你的 Example.au3(或任何你的脚本)的顶部,就在包含的下面。这样做的原因是,如果它在生成库时会自动重新启动您的脚本,并且如果它不在顶部可能会导致错误。我希望这个写得不好的UDF可以帮助你!让我知道如果它没有做你需要它做的事情。如果没有,请告诉我,我会修复它!快乐编程我的朋友!如果这太难理解了,可以看我在 AutoIt 官方论坛HERE987654321@上的更详细的演示

      谢谢,

      提姆

      【讨论】:

        【解决方案3】:

        如果有人有同样的问题,我就是这样解决的.. 在运行脚本之前,只有 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
        

        【讨论】:

          猜你喜欢
          • 2023-04-07
          • 2013-06-21
          • 1970-01-01
          • 1970-01-01
          • 2013-11-10
          • 2010-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多