Modify code to move ONLY text files from multiple folders

joand

Active Member
Joined
Sep 18, 2003
Messages
267
The macro below will move ALL files from a specified path (with multiple subfolders) to another folder. However, how do you change this code so that it will move ONLY txt files?

Code:
Sub MoveFiles()

Dim fso, fldr, subFldr, oFiles, oFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("E:\DILPA\Compile\Compiled\")
    
    For Each subFldr In fldr.SubFolders
        Set oFiles = subFldr.Files
        
        For Each oFile In oFiles
            
        oFile.Move "I:\4xmit\4xmit 2moro\"
                
        Next
    Next
    Set oFiles = Nothing
    Set subFldr = Nothing
    Set fldr = Nothing
    Set fso = Nothing

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
There might be other ways to identify the file type, eg Type, but this checks file extension.
Code:
If Left (oFile.Name, 4) = ".txt" Then
     oFile.Move "I:\4xmit\4xmit 2moro\"
 
End If
 
Upvote 0
It's not doing anything. Can you tell why?

Code:
Sub MoveFiles()

Dim fso, fldr, subFldr, oFiles, oFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("E:\DILPA\Compile\Compiled\")
    
    For Each subFldr In fldr.SubFolders
        Set oFiles = subFldr.Files
        
        For Each oFile In oFiles
            If Left(oFile.Name, 4) = ".txt" Then
               oFile.Move "I:\4xmit\4xmit 2moro\"
            End If
        Next
    Next
    Set oFiles = Nothing
    Set subFldr = Nothing
    Set fldr = Nothing
    Set fso = Nothing

End Sub
 
Upvote 0
Hi

Should be Right (not Left):

Code:
Sub MoveFiles()

Dim fso, fldr, subFldr, oFiles, oFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("E:\DILPA\Compile\Compiled\")
    
    For Each subFldr In fldr.SubFolders
        Set oFiles = subFldr.Files
        
        For Each oFile In oFiles
            If Right(oFile.Name, 4) = ".txt" Then
               oFile.Move "I:\4xmit\4xmit 2moro\"
            End If
        Next
    Next
    Set oFiles = Nothing
    Set subFldr = Nothing
    Set fldr = Nothing
    Set fso = Nothing

End Sub
 
Upvote 0
Oops, should be Right(oFile.Name, 4).:oops:

I used Name here instead of Type because though Type might return 'Text Document' for a text file that's system dependent.

So using Name you can check for the extension '.txt' which is quite common for text files, though not universal.

If the files you are interested in do have a different file extension(s) then the code will neeed to be changed.
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,696
Members
452,938
Latest member
babeneker

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