VBScript で モジュールをインクルードする方法 | ぺこちゃんのPC教室

ぺこちゃんのPC教室

パソコンに関する備忘録を書いていこうと思います。

1、VBSのメインモジュール(Main.vbs)
'***** Main.vbs *****
Option Explicit
call IncludeFunction()

Private fso          'ファイルシステムオブジェクト
Dim strPath        'SDカードのパス名
Const strParam1 = ":\PRIVATE\SHARP\FAX\DOC"        'FAXデータのフォルダー名
Const strParam2 = "C:\Users\user1\OneDrive - 【工房52】(アトリエコニー)\WORK2019\6.ファックス\"
Set fso = CreateObject("Scripting.FileSystemObject")

strPath = SD_Drive
If strPath <> "" then
    Call AutoMsg("FAXデータを処理します。", 1)    'メッセージを1秒表示
    Call Main(strPath, "txt")
    Call AutoMsg("処理終了", 1)                           '「処理終了」メッセージを1秒表示
End If

Set fso = Nothing
WScript.Quit
'--------------- END ---------------

'------------------------------------------------------------------------------
' 自作関数をコールするサブルーチン
'------------------------------------------------------------------------------

sub IncludeFunction()
Dim FullPath,FileName,pathLen,crrDir,chiDir
Dim objFileSys,objFolder,objFile
Dim FuncFile
   '----------------------------------------------------------------------------
   ' 自分(=VBSファイル)のディレクトリを取得する
   '----------------------------------------------------------------------------

   FullPath = wscript.scriptfullname
   FileName = wscript.scriptname
   pathLen= len(FullPath) - len(FileName)
   crrDir = left(FullPath, pathLen)
   chiDir = "myFunc"
   crrDir = crrDir & chiDir
   '----------------------------------------------------------------------------
   ' ファイルシステムを扱うオブジェクトを作成
   '----------------------------------------------------------------------------

   Set objFileSys = CreateObject("Scripting.FileSystemObject")
   '----------------------------------------------------------------------------
   ' フォルダのオブジェクトを取得
   '----------------------------------------------------------------------------

   Set objFolder = objFileSys.GetFolder(crrDir)
   '----------------------------------------------------------------------------
   ' FolderオブジェクトのFilesプロパティからFileオブジェクトを取得
   '----------------------------------------------------------------------------

   For Each objFile In objFolder.Files
      '--------------------------------------------------------------------------
      ' 自作関数のコール
      '--------------------------------------------------------------------------

      FuncFile = objFile.Name
      if instr(1, FuncFile, ".txt") = 0 then
         Set objFile = objFileSys.OpenTextFile(crrDir & "\" & FuncFile)
         ExecuteGlobal objFile.ReadAll()
         objFile.Close
      end if
   Next
   '----------------------------------------------------------------------------
   ' メモリ解放
   '----------------------------------------------------------------------------

   Set objFile = Nothing
   Set objFolder = Nothing
   Set objFileSys = Nothing
end sub