/build/static/layout/Breadcrumb_cap_w.png

Deleting Blank lines from text file

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
objReadFile.Close

'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.

Scotty

0 Comments   [ + ] Show comments

Answers (2)

Posted by: WiseUser 19 years ago
Fourth Degree Brown Belt
0
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

Const iFORREADING = 1
Const iFORWRITING = 2
Const iTEMPFOLDER = 2

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

Loop

hReadFile.Close
hWriteFile.Close

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.
Posted by: Scotty 19 years ago
Senior Yellow Belt
0
Thanks WiseUser! I'll give it a crack and let you know how i get on.

Thanks again
Scotty
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ