Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create text file and write to it in vbscript

Tags:

vbscript

I have the following script which locates all access files on a 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'm very amateur when it comes to vbscript. Instead of Echoing to a dialog box, how do I have the script write each line out to a text file called "Results.txt"?

Also, as a bonus, how do I include the date modified of each Access file?

like image 952
Kevin Avatar asked Mar 15 '23 00:03

Kevin


2 Answers

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
like image 188
Jake Avatar answered Apr 27 '23 06:04

Jake


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
like image 32
ManishChristian Avatar answered Apr 27 '23 07:04

ManishChristian