vbscript
Inkludera filer
Sök…
Introduktion
När du kör VbScript i Windows-skalet finns det ingen inbyggd funktion för att inkludera en fil, för att organisera din kod i olika filer måste du skapa en metod för att göra det.
Anmärkningar
Några saker att tänka på när du använder IncludeFile(p_Path)
:
- Det finns ingen begränsning av filtyp som kan inkluderas men innehållet i filerna måste vara VbScript.
- Om det finns ett syntaxfel i den inkluderade filen får du inte raden / kolumnen för felet.
- Du måste definiera och initiera
std_internal_LibFiles
innan det första samtalet tillIncludeFile(p_Path)
- Du kan använda
IncludeFile(p_Path)
helst i din kod, inklusive andra metoder.
Skapa en "inkludera fil" -metod
Så det huvudsakliga målet med denna funktion är att:
- Var fristående eftersom den måste skrivas i VbScript-huvudfilen och inte kan vara i en inkluderad fil (eftersom den definierar inkluderingsfunktionen)
- Ge tillräckligt med information om något går fel (dvs. filen som ingick, felet som inträffade, ...)
- Inkludera en fil en gång och bara en gång för att undvika att inkludera slingor.
' *************************************************************************************************
'! 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
Inklusive filer
Om du vill inkludera en fil i en annan fil använder du bara en liner:
IncludeFile "myOtherFile.vbs"
Global initiering
Innan vi använder IncludeFile-metoden måste vi:
- Förklara
std_internal_LibFiles
globalt - Initiera det med en ny ordlista
Dim std_internal_LibFiles
Set std_internal_LibFiles = CreateObject("Scripting.Dictionary")
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow