|
-
Feb 7th, 2011, 11:17 AM
#1
Thread Starter
New Member
Find and extract data from text files
Hi guys, sorry to be a newbie. I have attempted to find a solution everywhere and this was my last resort.
I have a folder named 'Output' , this folder contains 200-300 .txt files.
Each file when opened has an ID a timestamp and a status the following format
ID: 1073863
TIMESTAMP: 07/02/2011 16:12:05
STATUS:Good
I want to write a script in VB so that on button press I can obtain the ID's
from all those with a status of 'Bad' and save them in a file delimited by new lines and then delete the files with the 'Bad' status from the folder.
Any help or ideas with this would be greatly appreciated.
Kind Regards
Minimatrix
-
Feb 7th, 2011, 11:26 AM
#2
Fanatic Member
Re: Find and extract data from text files
You need to go to project/references and add a reference to the VBScripting runtime. This will give you a whole heap of stuff to use that will allow you to open, read and close files as well as traverse directories etc.
-
Feb 7th, 2011, 11:31 AM
#3
Member
Re: Find and extract data from text files
Here's a fine example of how to find a string in a text file.
http://www.vbforums.com/showthread.php?t=381822
First you have to get all the filenames in your 'Output' folder. Then open each one and search ID and STATUS. For each STATUS found bad, save ID in an array, along with filename.
Then, save in a file all ID's from the array, and delete all files from the array.
-
Feb 7th, 2011, 12:28 PM
#4
Re: Find and extract data from text files
Try this, add a reference to the microsoft scripting runtime and then you just need to change FULLPATH to the folder you need too. And also the path for BADIDS, i just called the textfile badids.txt but you need to set path.
Code:
Option Explicit
Private Const FULLPATH As String = "C:\test\"
Private Const BADIDS As String = "C:\test\BadIDs.txt"
Private Sub Command1_Click()
Dim oFile As Object
Dim buf As String
Dim tmp_FULLPATH As String
Dim oFs As New FileSystemObject
Dim oFolder As Folder
tmp_FULLPATH = FULLPATH
If Right$(tmp_FULLPATH, 1) <> "\" Then
tmp_FULLPATH = tmp_FULLPATH & "\"
End If
Set oFolder = oFs.GetFolder(tmp_FULLPATH)
For Each oFile In oFolder.Files
Open tmp_FULLPATH & oFile.Name For Input As #1
buf = Input$(LOF(1), #1)
Close #1
If InStr(LCase$(buf), "bad") > 0 Then
Open BADIDS For Append As #1
Print #1, Mid$(buf, 1, InStr(1, buf, vbNewLine))
Close #1
Kill tmp_FULLPATH & oFile.Name
End If
Next oFile
End Sub
This assumes a couple of things, first that there will only be one occurance of the word bad in the file, and second that the ID is alays on the first line.
Last edited by Jmacp; Feb 7th, 2011 at 12:32 PM.
-
Feb 7th, 2011, 12:55 PM
#5
Re: Find and extract data from text files
You don't write "scripts" in VB, you write programs. Or are you in the wrong forum and asking about writing a WSH script in VBScript?
In VB you can use the FSO, but it tends to be slower than native I/O operations and can only move forward through TextStreams. Using the Dir$() function can be much quicker than using the FSO's GetFolder() method and Folder object. In VBScript that's your only option though.
Generally you wouldn't mix both techniques in the same VB program, but nothing prevents you from doing so.
-
Feb 7th, 2011, 04:52 PM
#6
Thread Starter
New Member
Re: Find and extract data from text files
Thanks for all your input, I did indeed mean a vb program not script. Truth is I am perl/php developer so call everything code wise a script.
I will take into account all of your input and see what I can do. Thanks for helping me on my venture into VB.
I will post back soon to let you know how I get on
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|