54

Is it possible to create a file and write lines to it in vbscript?

Something similar to echo in bat file (echo something something >>sometextfile.txt).

On execution of the vbscript depending on the path of the script would create an autorun.inf file to execute a particular program (\smartdriverbackup\sdb.exe).

Also how can I strip/remove the drive letter from the complete file path?

Dhinakar
  • 4,061
  • 6
  • 36
  • 68
Dario Dias
  • 817
  • 6
  • 19
  • 32
  • Does this answer your question? [Read and write into a file using VBScript](https://stackoverflow.com/questions/1142678/read-and-write-into-a-file-using-vbscript) – bobobobo Dec 25 '21 at 23:29

3 Answers3

131
Set objFSO=CreateObject("Scripting.FileSystemObject")

' How to write file
outFile="c:\test\autorun.inf"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test string" & vbCrLf
objFile.Close

'How to read a file
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine= objFile.ReadLine
    Wscript.Echo strLine
Loop
objFile.Close

'to get file path without drive letter, assuming drive letters are c:, d:, etc
strFile="c:\test\file"
s = Split(strFile,":")
WScript.Echo s(1)
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • Thanks ghostdog74 for the answer I have tried the script.It works.I need to put it into actual application. – Dario Dias Feb 04 '10 at 11:00
  • I have problem in writing a text line with inverted commas,Eg: This is a "Test" "String".Besides iF I want a variable value to be written in inverted commas I cant. – Dario Dias Feb 06 '10 at 10:10
  • you have to escape the quotes. try `""""` – ghostdog74 Feb 06 '10 at 10:25
  • how exactly do I escape the quotes? – Dario Dias Feb 06 '10 at 11:01
  • 2
    do a `Wscript.Echo """"Test""""` and see. – ghostdog74 Feb 06 '10 at 11:19
  • @ghostdog74 it is not working in Excel for Mac. I am getting "activex component can't create object" error. – meMadhav Mar 06 '19 at 09:24
  • You should note this won't work if you're trying to write Unicode characters into the file -- [see here for the Scripting.FileSystemObject.OpenTextFile command docs](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method) – bobobobo Dec 25 '21 at 23:27
7

You'll need to deal with File System Object. See this OpenTextFile method sample.

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 4
    @hdgarrood, if you get into a car and find the steering wheel is missing or hidden from plain view, would you be a "help vampire" if you sought help? Must everyone reinvent the wheel because the previous person was too lazy to implement F1 properly (or maybe they do this on purpose to create demand for "consulting" - you know how it works) – Sam Jun 24 '13 at 07:01
  • 1
    I upvoted this answer because it points the asker in the correct direction while still requiring that he/she does a little bit of research and thought, as opposed to doing all the work for him/her. (Although I do regret using the phrase "help vampire") – hdgarrood Jun 24 '13 at 10:30
  • 5
    I'm not here to learn VBScript. I'm here to write a single script that does nothing more than write a string to a file, and (if all goes according to plan) I will never touch the language again. If that makes me a "help vampire," so be it. – Kyle Strand Sep 30 '14 at 15:39
6
' Create The Object
Set FSO = CreateObject("Scripting.FileSystemObject")

' How To Write To A File
Set File = FSO.CreateTextFile("C:\foo\bar.txt",True)
File.Write "Example String"
File.Close

' How To Read From A File
Set File = FSO.OpenTextFile("C:\foo\bar.txt")
Do Until File.AtEndOfStream
    Line = File.ReadLine
    WScript.Echo(Line)
Loop
File.Close

' Another Method For Reading From A File
Set File = FSO.OpenTextFile("C:\foo\bar.txt")
Set Text = File.ReadAll
WScript.Echo(Text)
File.Close
TheRealSuicune
  • 369
  • 3
  • 10
  • it is not working in Excel for Mac. I am getting "activex component can't create object" error. Is there anything similar available for Mac as well? – meMadhav Mar 06 '19 at 09:25