Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Searching through the contents of a Folder of text files

Morning,

Not sure if any one can help on this one! not even too sure if it is
possible in VBA!
I need to search through the contents of a folder full of text files,
and look for a specific word in each file and if the word is found then
various data needs to be extracted from that file.

I was thinking of using the filesystem object for looping through the
files inn the folder i.e the following code that I found will cover
this bit

Dim aryFiles
Dim oFSO

Sub LoopFolders()
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")
selectFiles "c:\MyTest"
Set oFSO = Nothing

End Sub

'--------------------------------------------------------------------------*-

Sub selectFiles(sPath)
'--------------------------------------------------------------------------*-

Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
' your macro here
Activeworkbook.Save
Activeworkbook.Close
End If
Next file

End Sub

HOW DO I SEARCH TEXT FILES FOR A SPECIFIC WORD?....AND THEN EXTRACT
SPECIFIC INFORMATION (ONLY THING IN MY FAVOUR FOR THE EXTRACTION IS
THAT ALL THE TEXT FILES ARE THE SAME LAYOUT)

Any help greatly appreciated

Regards
Jason

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 175
Default Searching through the contents of a Folder of text files

Read the UsedRange of the sheet into a variant array then process this array
looking for your specific word.

Dim addr as String
Dim vaData() as Variant
addr = ActiveSheet.UsedRange.Address
vaData = Range(addr).value
For x = 0 to UBound(vaData)
For y = 0 to UBound(vadata, 2)
If vaData(x, y) = "your string" then go sub FoundItRoutine
Next y
Next x
Set vaData = nothing

If you know a specific row/col or range of row/col that will have the
search-for-text. then you can limit the for loops. Just remember that the
array cells start at 0. so a 10x10 range on your sheet will be in (0..9,
0..9)

Hope that helps.
--
Regards,
John


"jase" wrote:

Morning,

Not sure if any one can help on this one! not even too sure if it is
possible in VBA!
I need to search through the contents of a folder full of text files,
and look for a specific word in each file and if the word is found then
various data needs to be extracted from that file.

I was thinking of using the filesystem object for looping through the
files inn the folder i.e the following code that I found will cover
this bit

Dim aryFiles
Dim oFSO

Sub LoopFolders()
Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")
selectFiles "c:\MyTest"
Set oFSO = Nothing

End Sub

'--------------------------------------------------------------------------Â*-

Sub selectFiles(sPath)
'--------------------------------------------------------------------------Â*-

Dim Folder As Object
Dim Files As Object
Dim file As Object
Dim fldr

Set Folder = oFSO.GetFolder(sPath)

For Each fldr In Folder.Subfolders
selectFiles fldr.Path
Next fldr

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
' your macro here
Activeworkbook.Save
Activeworkbook.Close
End If
Next file

End Sub

HOW DO I SEARCH TEXT FILES FOR A SPECIFIC WORD?....AND THEN EXTRACT
SPECIFIC INFORMATION (ONLY THING IN MY FAVOUR FOR THE EXTRACTION IS
THAT ALL THE TEXT FILES ARE THE SAME LAYOUT)

Any help greatly appreciated

Regards
Jason


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling pdf files from general folder to specific folder [email protected] Excel Discussion (Misc queries) 2 September 8th 09 09:41 PM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM
Exporting folder contents to excel or text format Sr. Bio Excel Discussion (Misc queries) 1 January 9th 06 06:51 PM
delete all the contents (sub folders and files) in the temp folder Joseph Excel Discussion (Misc queries) 0 June 6th 05 08:01 AM
Searching multiple files and copying text Marcelo P. Excel Programming 0 July 23rd 04 08:30 PM


All times are GMT +1. The time now is 04:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"