vbscript
Fichiers à inclure
Recherche…
Introduction
Lorsque vous exécutez VbScript dans un shell Windows, il n'y a pas de fonction intégrée pour inclure un fichier. Par conséquent, pour organiser votre code dans différents fichiers, vous devez créer une méthode pour ce faire.
Remarques
Quelques points à garder à l'esprit lors de l'utilisation de la IncludeFile(p_Path)
:
- Il n'y a pas de limitation du type de fichier pouvant être inclus, mais le contenu des fichiers inclus doit être VbScript.
- S'il y a une erreur de syntaxe dans le fichier inclus, vous n'obtiendrez pas la ligne / colonne de l'erreur.
- Vous devez définir et initialiser
std_internal_LibFiles
avant le premier appel àIncludeFile(p_Path)
- Vous pouvez utiliser
IncludeFile(p_Path)
n'importe où dans votre code, y compris d'autres méthodes.
Créer une méthode "include file"
Le but principal de cette fonction est donc de:
- Soyez autonome car il doit être écrit dans le fichier VbScript principal et ne peut pas être dans un fichier inclus (car il définit la fonction include)
- Fournir suffisamment d'informations si quelque chose ne va pas (c.-à-d. Le fichier qui était inclus, l'erreur qui s'est produite, ...)
- Inclure un fichier une fois et une seule fois pour éviter d'inclure des boucles.
' *************************************************************************************************
'! 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
Y compris les fichiers
Pour inclure un fichier dans un autre fichier, utilisez simplement le liner:
IncludeFile "myOtherFile.vbs"
Initialisation globale
Avant d'utiliser la méthode IncludeFile, nous devons:
- Déclarez
std_internal_LibFiles
globalement - Initialisez-le avec un nouveau dictionnaire
Dim std_internal_LibFiles
Set std_internal_LibFiles = CreateObject("Scripting.Dictionary")
Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow