Creating and writing lines to a file

Dario Dias picture Dario Dias · Feb 4, 2010 · Viewed 354.7k times · Source

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?

Answer

ghostdog74 picture ghostdog74 · Feb 4, 2010
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)