How to create text file and write to it in vbscript

后端 未结 3 1472
长发绾君心
长发绾君心 2020-12-02 02:40

I have the following script which locates all access files on a machine:

strComputer = \".\"

Set objWMIService = GetObject(\"winmgmts:\\\\\" & strComput         


        
相关标签:
3条回答
  • 2020-12-02 02:45

    Simple Google search like "vbscript create and write to text file" will give you ocean of information on how to tackle this. Anyway here is simplest one to give you kick start.

    '~ Create a FileSystemObject
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    
    '~ Provide file path
    outFile="YouFolderPath\Results.txt"
    
    '~ Setting up file to write
    Set objFile = objFSO.CreateTextFile(outFile,True)
    
    
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_DataFile Where Extension = 'mdb' OR Extension = 'ldb'")
    
    For Each obj_File in colFiles
        'Wscript.Echo objFile.Name  'Commented out
    
        '~ Write to file
        objFile.WriteLine obj_File.Name
    Next
    
    '~ Close the file
    objFile.Close
    
    0 讨论(0)
  • 2020-12-02 02:56

    This is what you are looking for. In this part: ("C:\test.txt" ,8 , True), the first parameter is the path to the file. The second parameter is the iomode option. There are three options for the second parameter, 1 means for reading, 2 means for writing, and 8 means for appending. The third parameter is a boolean, true means a new file can be created if it doesn't exist. False means a new file cannot be created.

    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set OutPutFile = FSO.OpenTextFile("C:\test.txt" ,8 , True)
    OutPutFile.WriteLine("Writing text to a file")
    
    Set FSO= Nothing
    
    0 讨论(0)
  • 2020-12-02 02:56

    Use the FileSystemObject's .CreateTextFile method to create a text file. Study the documentation/sample carefully.

    A CIM_DataFile has a .LastAccess property.

    0 讨论(0)
提交回复
热议问题