खोज…


परिचय

जब विंडोज शेल में VbScript चल रहा है, तो फ़ाइल को शामिल करने के लिए कोई फ़ंक्शन नहीं बनाया गया है, इसलिए, विभिन्न फ़ाइलों में अपने कोड को व्यवस्थित करने के लिए आपको ऐसा करने के लिए एक विधि बनाने की आवश्यकता होगी।

टिप्पणियों

IncludeFile(p_Path) विधि का उपयोग करते समय कुछ बातों का ध्यान रखें:

  • फ़ाइल प्रकार की कोई सीमा नहीं है जिसे शामिल किया जा सकता है लेकिन इसमें शामिल फ़ाइलों की सामग्री VbScript होनी चाहिए।
  • यदि शामिल फ़ाइल में कोई सिंटैक्स त्रुटि है, तो आपको त्रुटि की रेखा / स्तंभ नहीं मिलेगा।
  • शामिल करने के लिए पहली कॉल से पहले आपको std_internal_LibFiles को परिभाषित और आरंभ करना होगा IncludeFile(p_Path)
  • आप अन्य तरीकों सहित अपने कोड में कहीं भी शामिल करें IncludeFile(p_Path) उपयोग कर सकते हैं।

एक "फ़ाइल शामिल करें" विधि बनाना

तो इस समारोह का मुख्य लक्ष्य है:

  • स्टैंडअलोन रहें क्योंकि इसे मुख्य VbScript फ़ाइल में लिखा जाना चाहिए और इसमें शामिल फ़ाइल नहीं हो सकती है (क्योंकि इसमें यह फ़ाइल शामिल नहीं है)
  • कुछ गलत होने पर पर्याप्त जानकारी प्रदान करें (अर्थात, जो फ़ाइल शामिल की जा रही थी, जो त्रुटि हुई, ...)
  • लूप्स से बचने के लिए केवल एक बार फ़ाइल शामिल करें और केवल एक बार।
' *************************************************************************************************
'! 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"

वैश्विक आरंभ

इनक्लूड विधि का उपयोग करने से पहले, हमें निम्न करने की आवश्यकता है:

  • विश्व स्तर पर 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