deleting cetain lines on a file

dtaylor

Active Member
Joined
Mar 21, 2002
Messages
379
Hello ALL,
I have a need for a macro that will open up all text files (with extension .csv) in a directory, find lines that have a cell entry of **** Header Line ****
and then delete those lines, close and save the files. There is only one column containing the string, col "J". The number of lines with a cell containing this string will vary as will the number of files.
There are 52 diff columns on each file and each are in the same format.

Any assistance would be gratefully recieved!

Thanks and have a great day....

Dan
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
hope this does it for you... will search all files as prescribed and strip any whole lines that contain the search text anywhere in that line...




Sub strip_files()

search_dir = "c:"
file_type = ".csv"
find_string = "*** Header Line ***"

filesfound = 0

Dim filefound As String
filefound = Dir(search_dir & "*" & file_type)
If filefound = "" Then GoTo end_files
nextfile:
filesfound = filesfound + 1
file1 = search_dir & filefound
file2 = Left(file1, Len(file1) - (Len(file_type) - 1)) & "tmpx"
If Dir(file2) <> "" Then Kill file2
Open file1 For Input As #1
Open file2 For Output As #2
Do Until EOF(1)
Line Input #1, strg
If InStr(1, strg, find_string) = 0 Then Print #2, strg
Loop
Close #1
Close #2
Kill file1

filefound = Dir(search_dir & "*" & file_type)
If filefound <> "" Then GoTo nextfile

end_files:
filefound = Dir(search_dir & "*.tmpx")
Do Until filefound = ""
file1 = search_dir & filefound
file2 = Left(file1, Len(file1) - 5) & file_type
Name file1 As file2
filefound = Dir(search_dir & "*.tmpx")
Loop

MsgBox filesfound & " files processed."
End Sub
 
Upvote 0
darn, it did it again, what is wrong with this thing! ha ha

should only be on forward slash in the directory name specified.
 
Upvote 0
On 2002-03-29 17:09, daleyman wrote:
there is definately something wrong with this site, mail me if you want the code as i submitted it (if it doesn't work from copy & pasting) daley_123@hotmail.com

Yes there is the [] double up...known prob
This is getting looked into.....please bere
with us until it gets a fix....


Regards

Ivan
 
Upvote 0
wow thanks for all the interest. i am sorry it took so long for my reply but i was out of town. i will, now that i am back at work uugh!, give this a go. do appreciate the input. you don't know how much work you have saved more than one person. have a good day.

dan
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top