Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop file & find text and move 1

Status
Not open for further replies.

testare

Programmer
Jan 21, 2005
127
Hi,
I would like to loop to a specific folder.
Then i would like to rename the files from .dat to .txt because then to find a specific text and if i find then move it.

i've started with this code but doesn't feel good.

Code:
    Dim fso As FileSystemObject
    
    Set fso = New FileSystemObject

    sFolder = SourcePath & "C:\Temp\*.*"
    sfile = Dir$(sFolder, vbNormal)

    Do While sfile <> ""
        Debug.Print sfile
        fso.MoveFile "C:\Temp\" & sfile, "C:\"
        sfile = Dir$
    Loop
I would like to use for each but i dont know how.
 
You may need to tweak this a little...

Code:
Dim fso As FileSystemObject
Dim oFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim strData As String

Set fso = New FileSystemObject

Set oFolder = fso.GetFolder("C:\Temp")

For Each oFile In oFolder.Files
    strData = oFile.OpenAsTextStream(ForReading).ReadAll
    If InStr(strData, "This is what I want to find.") > 0 Then
        Call fso.MoveFile(oFile.Name, "C:\")
    End If
Next

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks i changed the movefile. and it worked perfect.

Code:
Call fso.MoveFile("C:\temp\" & oFile.Name, "C:\")

noticed that i didn't have to rename the filename .dat file to .txt for it to find the text "test and then move the file. right George?
 
No, you don't have to rename the file to open it. Of course, if your business requirement mandates that you rename the file, then you will have to add that.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
You are welcome, and thanks for the star.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I found a little problem today.
When i search ex for test and in the files there is text test then the files is moved.
But i noticed when i search for test and in the files the text is TEST then it is not being found and moved.
I would like to find and move the file if the text is TEST or test.

Code:
Dim fso As FileSystemObject
Dim oFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim strData As String

Set fso = New FileSystemObject

Set oFolder = fso.GetFolder("C:\Temp")

For Each oFile In oFolder.Files
    strData = oFile.OpenAsTextStream(ForReading).ReadAll
    If InStr(strData, "test") > 0 Then
        Call fso.MoveFile("C:\temp\" &oFile.Name, "C:\")    End If
Next

What should i do to fix this problem?
 
Dim fso As FileSystemObject
Dim oFolder As Scripting.Folder
Dim oFile As Scripting.File
Dim strData As String

Set fso = New FileSystemObject

Set oFolder = fso.GetFolder("C:\Temp")

For Each oFile In oFolder.Files
strData = oFile.OpenAsTextStream(ForReading).ReadAll
If InStr(LCase(strData), LCase("test")) > 0 Then
Call fso.MoveFile("C:\temp\" &oFile.Name, "C:\") End If
Next



------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Now it works, it doesn't care if the text is test or TEST
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top