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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to delete a line...But how?!

Status
Not open for further replies.

Phreeneazie

Technical User
Apr 6, 2003
27
GB
HI all,

I'm back pleading with you guys for some more help! Regular visitors will know I'm completely dumb s'far as VB goes :(

I need to be able to delete a line of text in a file that contains the word "projected". It can appear anywhere on the line.

I have been looking at this script that was previously supplied to me for a different project and I'm sure that this is the way to go - but it needs slight modification...Can anyone help me out?! The script is:
Code:
Const ForReading=1,ForWriting=2
Const myFile="C:\VBTesting\edmstest.txt"
Set fso=CreateObject("Scripting.FileSystemObject")
Set f=fso.OpenTextFile(myFile,ForReading)
a=Split(f.ReadAll,vbCrLf)
f.Close
Set f=fso.OpenTextFile(myFile,ForWriting,True)
For i=0 To UBound(a)
  If Left(a(i),1)<>"projected" Then f.WriteLine a(i)
Next
f.Close

As always, I'll be eternally grateful for any help!

Many thanks,

Neil.
 
This will do what you are asking. i have it write the data back to a new file...just in case you need to have the original to refer to.


'==========================================================================
'
' NAME: StripOutLineIfTextFound.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: ' DATE : 3/16/2004
'
' COMMENT: <comment>
'
'==========================================================================

On Error Resume Next

'open the file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
'open the data file
Set oTextStream = oFSO.OpenTextFile("c:\projected.txt")
'make an array from the data file
TextLines = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

Report = ""

For Each Line In Textlines
lineLen = Len(Line)
projectedFound = "False"

For x = 1 To lineLen
'check for the word projected
If lcase(Mid(line,x,9)) = "projected" Then
'don't save that line
projectedFound = "True"
End If
Next

If projectedFound = "False" Then
'projected not there, add the line to the new file report
Report = report & line & vbCrLf
End If
Next

If Not oFSO.FileExists("C:\newfile.txt") Then
Set ts = oFSO.CreateTextFile ("C:\newfile.txt", ForWriting)
ts.write Report
End If

WScript.Echo("new file is:" & vbCrlf & Report)
MsgBox "Done"

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hello Phreeneazie,

Just change the corresponding line to read as this.
Code:
If instr(1,a(i),"projected",1)<> 0 Then f.WriteLine a(i)
regards - tsuji

 
erratum

I mean the contrary, sorry!
Code:
If instr(1,a(i),"projected",1)= 0 Then f.WriteLine a(i)
Of course, if something like pro-jected hypenated and across two lines, then it won't do.

- tsuji
 
Guys,

thank-you VERY much! I'm not in the office at the moment, but I'll have a go when I get in tonight and will report back the results.

Once again you've saved my life!

I keep on & on asking my boss to send me on some courses to learn VB but the answer? Always the same, "We'll look at next year's budget" :-(
 
Do yourself a favor and buy a VBScript for Dummies book. It will get you started well enough that you will be able to piece things together though not always in the most efficient manner (like my script above <g>).

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top