Find given string inside .txt file in folder and copy file to another directory

JrExceler

New Member
Joined
Nov 18, 2016
Messages
17
Hello,

I've tried hard to find a solution for my problem but all the codes I've tried don't work or something is missing.

What I need is a Macro that finds a given string inside a .txt file in a given directory (more than 100 files in it) and then copy the file to a specific path.

The code I have is:

Code:
Sub StringExistsInFile()
    Dim theString As String
    Dim path As String
    Dim StrFile, NewFile As String
    Dim fso As New FileSystemObject
    Dim file As TextStream
    Dim line As String
     
     
    theString = Range("K3").Value
    path = Range("K4").Value
    StrFile = Dir(path & "*.txt")
     
     
    Do While StrFile <> ""
         
         
        Set file = fso.OpenTextFile(path & StrFile)
        Do While Not file.AtEndOfLine
            line = file.ReadLine
            If InStr(1, line, theString, vbTextCompare) > 0 Then
                 
                FileCopy path & StrFile, "C:\MyData\" & StrFile
                 
                Exit Do
            Else
                 
            End If
        Loop
         
         
        file.Close
        Set file = Nothing
        Set fso = Nothing
         
         
        StrFile = Dir()
    Loop
End Sub


I cannot make it work, do you think you can help me with this?

Thank you!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi,

I ran your code and it worked OK. I did make one change; "AtEndOfLine" should probably be "AtEndOfStream" - perhaps that would make a difference? You should also remove the "Set fso = Nothing" line since you're trying to dispose of an object that's still in use. What's happening for you? Do you get an error or does it just not work? Have tried setting breakpoints to determine what's happening?

WBD
 
Upvote 0
WBD is correct. Change to AtEndOfStream.

I managed to recreate an issue where it only read the first line on a tab delimited file. Changing this line fixed it.
 
Upvote 0
Works for me.
You haven't said what error you are seeing.

Make sure K4 path ends in a backslash or add that as below.
Another possibility is permissions for the paths you are accessing.

Code:
Set file = fso.OpenTextFile(path & "\" & StrFile)
 
Upvote 0
The error I get is

Compile Error:
User defined type not defined


These two below are marked with color:
Sub StringExistsInFile()
Dim fso As New FileSystemObject
 
Upvote 0
Haha!! I thought you'd already added the reference. I guess you copied/pasted this from somewhere. Go to Tools/References and tick the box next to "Microsoft Scripting Runtime".

WBD
 
Upvote 0
Yes I found the code on the internet,
Thank you for your help.
I ran the code with the modifications, it worked perfectly however it takes a lot of time and I cannot use Excel during the process but I think I will save me some time at the end.

=)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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