Hi all,

I am trying to delete specific lines from the middle of a text file and move the contents at the bottom of the file upwards once the lines have been deleted, ie. delete the blank lines left by the deletion.

I have managed to code the deletion of the text but not the deletion of blank lines. Any ideas.

here is my code. Any advice on how to improve this would be good too.

Option Explicit

Dim objFSO, objFile, objReadFile, objWriteFile, objNewFile
Dim strContents, strIniFile, strNewContents
Const ForReading = 1
Const ForWriting = 2

strIniFile = "c:\program files\sybase\ini\sql.ini"
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Read contents of sql.ini
Set objFile = objFSO.GetFile(strIniFile)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile(strIniFile, ForReading)
strContents = objReadFile.ReadAll

'Remove Maui data from sql.ini contents
strNewContents = strContents
strNewContents = Replace(strNewContents,"[blahblah]","")
strNewContents = Replace(strNewContents,"blah","")

'Write contents back to sql.ini
Set objNewFile = objFSO.OpenTextFile(strIniFile, ForWriting)
objNewFile.write (strNewContents)
End If

The text file has contents both before and after the text i'm trying to delete.

0 Comments   [ - ] Hide Comments


Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
Answer this question or Comment on this question for clarity


I'm feeling generous today!!

sMyFile = Wscript.Arguments(0)

Msgbox bRemoveLines(sMyFile, "AppDeploy")
Msgbox bRemoveLines(sMyFile, "WiseUser")

Function bRemoveLines(sFile, sFindString)

Dim oFso, hReadFile, hWriteFile, sReadLine, hTemp, sTemp, sTempFile

bRemoveLines = False


Set oFso = CreateObject("Scripting.FileSystemObject")

Set hTemp = oFso.GetSpecialFolder(iTEMPFOLDER)

sTemp = hTemp.Path

Set hTemp = Nothing

sTmpFile = sTemp & "\" & oFso.GetTempName

oFso.CopyFile sFile, sTmpFile, True

Set hReadFile = oFSO.OpenTextFile(sTmpFile, iFORREADING, True)

Set hWriteFile = oFSO.OpenTextFile(sFile, iFORWRITING, True)

Do Until hReadFile.AtEndofStream

sReadLine = Trim(hReadFile.ReadLine)

If InStr(LCase(sReadLine), LCase(sFindString)) = 0 AND sReadLine <> "" then

hWriteFile.WriteLine sReadLine

End If



Set hReadFile = Nothing
Set hWriteFile = Nothing

bRemoveLines = True

End Function

I suggest you put the indentation back (lost during posting) to make it more readable.

If you find this function useful, hold on to it - you never know when you might need something similar again. I keep a library of functions I've written in the past so I can use them again. There's nothing worse than rewriting something you know full well you've written before.
Answered 05/27/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
Thanks WiseUser! I'll give it a crack and let you know how i get on.

Thanks again
Answered 05/30/2005 by: Scotty
Senior Yellow Belt

Please log in to comment