vbscript
Incluir archivos
Buscar..
Introducción
Al ejecutar VbScript en el shell de Windows, no hay una función incorporada para incluir un archivo, por lo tanto, para organizar su código en diferentes archivos, deberá crear un método para hacerlo.
Observaciones
Algunas cosas a tener en cuenta al usar el IncludeFile(p_Path)
:
- No hay limitación de tipo de archivo que se pueda incluir, pero el contenido de los archivos incluidos debe ser VbScript.
- Si hay un error de sintaxis en el archivo incluido, no obtendrá la línea / columna del error.
- Debe definir e inicializar
std_internal_LibFiles
antes de la primera llamada aIncludeFile(p_Path)
- Puede usar
IncludeFile(p_Path)
en cualquier parte de su código, incluidos otros métodos.
Creando un método de "incluir archivo"
Entonces el objetivo principal de esta función es:
- Sea independiente porque necesita estar escrito en el archivo principal de VbScript y no puede estar en un archivo incluido (porque define la función de inclusión)
- Proporcione suficiente información si algo sale mal (es decir, el archivo que se estaba incluyendo, el error que ocurrió, ...)
- Incluya un archivo una vez y solo una vez para evitar incluir bucles.
' *************************************************************************************************
'! 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
Incluyendo archivos
Para incluir un archivo en otro archivo, solo use el forro:
IncludeFile "myOtherFile.vbs"
Inicialización global
Antes de usar el método IncludeFile, necesitamos:
- Declarar
std_internal_LibFiles
globalmente - Inicialízalo con un nuevo diccionario.
Dim std_internal_LibFiles
Set std_internal_LibFiles = CreateObject("Scripting.Dictionary")
Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow