tags:

views:

37

answers:

2

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?

+1  A: 

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

Rubens Farias
A: 
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
Thanks ghostdog74 for the answer I have tried the script.It works.I need to put it into actual application.
Dario Dias
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
you have to escape the quotes. try `""""`
ghostdog74
how exactly do I escape the quotes?
Dario Dias
do a `Wscript.Echo """"Test""""` and see.
ghostdog74
tried,does not work
Dario Dias
see http://tinyurl.com/y9l3l2g
ghostdog74