Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


newbie visual basic script help

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,60255
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> newbie visual basic script help
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 newbie visual basic script help - 5/15/2008 6:38:04 AM   
  LD

 

Posts: 2
Score: 0
Joined: 4/24/2008
Status: offline
Hi,
I am not a programmer by any means but am unfortunately stuck with trying to figure this out... So any help would be greatly appreciated...

I am looking to create a vbs script that will search multiple text files (see code below) for a line and then delete that line and the 6 lines below it....



In several text files we have the following lines:

SPECIFIC PICTURES BEING COPIED TO COMPUTERNAME1
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
SPECIFIC PICTURES BEING COPIED TO
0 file(s) copied.
0 file(s) copied.
0 file(s) copied.
0 file(s) copied.
0 file(s) copied.
0 file(s) copied.
SPECIFIC PICTURES BEING COPIED TO COMPUTERNAME2
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
SPECIFIC PICTURES BEING COPIED TO COMPUTERNAME15
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.


I need to modify the below script so that whenever it finds "SPECIFIC PICTURES BEING COPIED TO " with no computer name it deletes that line and the following 6 lines below it. The important thing here is that the COMPUTERNAME is dynamic and changes in every file.

Here is the current vbs script we have to search for strings and delete the single line:

Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim objFSO
Dim fld
Dim fil
Dim strFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set the directory you are looking at
Set fld = objFSO.GetFolder("DIRECTORYTOSEARCH\")
'Loop through each file in the folder
For Each fil in fld.Files
strFile = lcase(fil.path)
'If it is a text file send it off for editing
If Right(strFile,3) = "txt" Then
DeleteLines strFile, "SPECIFIC PICTURES BEING COPIED TO "
End If
Next
'Clean up
Set fld = Nothing
Set objFSO = Nothing

Private Sub DeleteLines(sFile, sSearch)
Dim objFile
Dim strLine
Dim strNewContents

Set objFile = objFSO.OpenTextFile(sFile, ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, sSearch) = 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close

Set objFile = objFSO.OpenTextFile(sFile, ForWriting)
objFile.Write strNewContents
objFile.Close

Set objFile = Nothing
End Sub

Again, I would greatly appreciate any help with this as I am absolutely stumped!
- LD
 
 
Post #: 1
 
 RE: newbie visual basic script help - 5/15/2008 7:25:42 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi LD,

how about changing the specs to: for each .txt file in a given folder: delete
all

"SPECIFIC PICTURES BEING COPIED TO" + maybe whitespace + lineending

and

" 0 file(s) copied." + maybe whitespace + lineending

lines?

Regards

ehvbs

PS:

Code:


      

and output:


      

to show what I'm driving at.

< Message edited by ehvbs -- 5/15/2008 7:39:45 AM >

(in reply to LD)
 
 
Post #: 2
 
 RE: newbie visual basic script help - 5/15/2008 7:37:27 AM   
  ebgreen


Posts: 5041
Score: 31
Joined: 7/12/2005
Status: offline
So I made a test file C:\Temp\Foo.txt and put the contents in it that you indicated. See if this code then gets you going in the right direction:

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim oRE : Set oRE = New Regexp
Dim strFileContents : strFileContents = oFSO.OpenTextFile("C:\temp\foo.txt").ReadAll
Dim colMatches
Dim oMatch
Dim strSub
oRE.Pattern = "(.*)(SPECIFIC PICTURES BEING COPIED TO \s(.*\s){6})(.*)"
Set colMatches = oRE.Execute(strFileContents)

WScript.Echo oRE.Replace(strFileContents, "$1$3")

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to LD)
 
 
Post #: 3
 
 RE: newbie visual basic script help - 5/15/2008 8:22:40 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Split your text in an array, then play with array numbers:


      

< Message edited by Fredledingue -- 5/15/2008 8:28:01 AM >


_____________________________

Fred

(in reply to ebgreen)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> newbie visual basic script help Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts