vbs script help: deleting a line in a text file

ZippyDan

Platinum Member
Sep 28, 2001
2,141
1
81
Code:
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\Users\Usuario\textfile.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine,"Some Line")> 0 Then
        strLine = Replace(strLine,"Some Line","")
    End If
     WScript.Echo strLine
Loop

This is some code I use to parse a text file for "Some Line". The text file is arranged with some lines like this:

Code:
Hello
Goodbye
Some Line
Blah
Blahblah

When I run the script on it, I end up with:

Code:
Hello
Goodbye

Blah
Blahblah

Which is to be expected. But what I really want is:

Code:
Hello
Goodbye
Blah
Blahblah

In other words, I want to backspace the line out of existence, not just replace it with blank space. How should I go about doing that?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
You're replacing the text "Some Line" with an empty string (no text), but you're leaving the newline character (Environment.Newline constant, equal to the combo 0x0A 0x0D on most windows machines) at the end of the string, which is why you have a blank line.

The reason you're "leaving" the newline character in this case is that you are basically calling WScript.Echo with an empty string. I don't know that method, but from your example when it is asked to echo an empty string it just writes the newline character, which is fairly reasonable.

So... why call it if the string is empty and you don't want a space? :)
 

GaryJohnson

Senior member
Jun 2, 2006
940
0
0
As far as I know, there's no way to delete a line in a textstream while looping through it with .readline. You have to find another way like reading out the whole file into a variable or array, removing the lines you don't want, and then overwriting the file with the variable contents.
 

Firetower

Senior member
Jul 15, 2003
447
0
0
write the lines to a temp and copy temp replacing the original

Code:
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFS.OpenTextFile("C:\Test\Test.txt")
Set objFS1 = objFS.CreateTextFile("C:\test\temp.txt",true)
objFS1.close
set objTextFile2 = objFS.OpenTextFile("C:\test\temp.txt",8)
strFile = "c:\test\test.txt"

Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine
    If InStr(strLine,"DeleteMe")> 0 Then
        strLine = Replace(strLine,"DeleteMe",1)
      Else
	objTextFile2.writeline strLine
    End If
   
Loop

set objTextFile = nothing
set objTextFile2 = nothing

objFS.copyFile "C:\test\temp.txt", "c:\test\test.txt", true
objfs.DeleteFile "C:\test\temp.txt"

set objfs = nothing
set objfs1 = nothing
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
As far as I could see he was just echoing the lines to the screen, and wondered why he was getting a blank line. I agree that if you want to actually remove the line from the file then loop through and write the lines you want to a temp file, and rename it afterward.