Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open an bat file

Status
Not open for further replies.

celia1

MIS
Nov 25, 2002
20
FR
Hy,

i want to open and write in a bat file in vbs. It is possible?
 
sounds more like you want to edit the file, append is easy, if you want to edit then i think you will need to read file into memory (FSO.OpenTextFile) edit the contents in memory, then write it back (FSO.CreateTextFile)

dont let the TextFile thing put you off, you just tell it to create a file call xxx.bat
 
Set FSO = CreateObject("Scripting.FileSystemObject")
Set dicSource = CreateObject("Scripting.Dictionary")
'dont know what a dic just lazy and i hate redim
'1 is for reading
Set tsSource = FSO.OpenTextFile("c:\test.bat", 1, True)
Do While Not tsSource.AtEndOfStream
sLine = ""
sLine = Trim(tsSource.ReadLine)
If sLine = "echo hello world" Then
sLine = "echo hello celia1"
End If
dicSource.Add dicSource.Count, sLine
Loop
tsSource.Close
Set tsSource = Nothing

Set tsSource = FSO.CreateTextFile("c:\test.bat", True)
'True isto overwrite existing file
For Each aLine In dicSource
tsSource.WriteLine dicSource.Item(aLine)
Next
tsSource.Close
Set tsSource = Nothing
Set FSO = Nothing

'i think

 
I write this script :
Sub FtpdConfig(ftpdFile, psaIP)

Dim fso, ftpdFileOri, fori, fbat

Set fso = CreateObject("Scripting.FileSystemObject")
ftpdFileOri = ftpdFile&".ori"

If (fso.FileExists(ftpdFileOri))=False Then
fso.CopyFile ftpdFile, ftpdFileOri
End If

Set fori = fso_OpenTextFile(ftpdFileOri, ForReading, False)
Set fbat = fso_OpenTextFile(ftpdFile, ForWriting, False)

Do While fori.AtEndOfStream <> True
ReadLineTextFile = fori.ReadLine
If Instr(ReadLineTextFile, "FtpServer.server.config.host") = 0 Then
fbat.WriteLine ReadLineTextFile
Else
fbat.WriteLine "FtpServer.server.config.host="&psaIP
End If
Loop

fori.Close
fbat.Close

End Sub

 
yeap, my iterations were required (ie. double loops) cause i was editing a file as opposed to creating a new one based on the contents of an existing one, but there you go
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top