수색…
소개
Windows 셸에서 VbScript를 실행할 때 파일을 포함하는 기능이 없으므로 코드를 다른 파일에 구성하여이를 수행하는 방법을 만들어야합니다.
비고
IncludeFile(p_Path)
메소드를 사용할 때 유의해야 할 몇 가지 사항은 다음과 같습니다.
- 포함될 수있는 파일 형식에는 제한이 없지만 포함 된 파일 내용은 VbScript 여야합니다.
- 포함 된 파일에 구문 오류가 있으면 오류의 행 / 열을 가져올 수 없습니다.
-
IncludeFile(p_Path)
대한 첫 번째 호출 전에std_internal_LibFiles
를 정의하고 초기화해야합니다. - 코드의
IncludeFile(p_Path)
사용할 수 있으며 다른 방법도 사용할 수 있습니다.
"파일 포함"방법 만들기
따라서이 기능의 주요 목표는 다음과 같습니다.
- 기본 VBScript 파일에 작성해야하며 포함 된 파일에 포함될 필요가 없기 때문에 독립형이어야합니다 (include 함수를 정의하기 때문에).
- 문제가 발생하면 충분한 정보를 제공하십시오 (즉, 포함 된 파일, 발생한 오류 ...)
- 포함 루프를 피하려면 한 번만 파일을 포함하십시오.
' *************************************************************************************************
'! Includes a VbScript file
'! @param p_Path The path of the file to include
' *************************************************************************************************
Sub IncludeFile(p_Path)
' only loads the file once
If std_internal_LibFiles.Exists(p_Path) Then
Exit Sub
End If
' registers the file as loaded to avoid to load it multiple times
std_internal_LibFiles.Add p_Path, p_Path
Dim objFso, objFile, strFileContent, strErrorMessage
Set objFso = CreateObject("Scripting.FileSystemObject")
' opens the file for reading
On Error Resume Next
Set objFile = objFso.OpenTextFile(p_Path)
If Err.Number <> 0 Then
' saves the error before reseting it
strErrorMessage = Err.Description & " (" & Err.Source & " " & Err.Number & ")"
On Error Goto 0
Err.Raise -1, "ERR_OpenFile", "Cannot read '" & p_Path & "' : " & strErrorMessage
End If
' reads all the content of the file
strFileContent = objFile.ReadAll
If Err.Number <> 0 Then
' saves the error before reseting it
strErrorMessage = Err.Description & " (" & Err.Source & " " & Err.Number & ")"
On Error Goto 0
Err.Raise -1, "ERR_ReadFile", "Cannot read '" & p_Path & "' : " & strErrorMessage
End If
' this allows to run vbscript contained in a string
ExecuteGlobal strFileContent
If Err.Number <> 0 Then
' saves the error before reseting it
strErrorMessage = Err.Description & " (" & Err.Source & " " & Err.Number & ")"
On Error Goto 0
Err.Raise -1, "ERR_Include", "An error occurred while including '" & p_Path & "' : " & vbCrlf & strErrorMessage
End If
End Sub
파일 포함
다른 파일에 파일을 포함하려면 하나의 라이너 만 사용하십시오.
IncludeFile "myOtherFile.vbs"
전역 초기화
IncludeFile 메서드를 사용하기 전에 다음을 수행해야합니다.
- 전역으로
std_internal_LibFiles
선언 - 새 사전으로 초기화하십시오.
Dim std_internal_LibFiles
Set std_internal_LibFiles = CreateObject("Scripting.Dictionary")
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow