How to create a text file and write it in vbscript

I have the following script that finds all the access files on the machine:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Extension = 'mdb' OR Extension = 'ldb'")

For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next

I am very amateur when it comes to vbscript. Instead of echoing in a dialog box, how can I script to write each line to a text file called "Results.txt"?

Also, as a bonus, how do I include a date changed for each Access file?

+2
source share
3 answers

A simple Google search, for example "vbscript create and write to text file", will give you information on how to handle this. In any case, this is the easiest way to give you a kick.

'~ 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
+2
source

, . : ( "C:\test.txt", 8, True) . - iomode. : 1 , 2 8 . , true , , . False , .

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
+2

FileSystemObject. CreateTextFile . /.

A CIM_DataFile .LastAccess.

0

Source: https://habr.com/ru/post/1679656/


All Articles