Move files in folder based on wildcard match

cortexnotion

Board Regular
Joined
Jan 22, 2020
Messages
150
Office Version
  1. 2013
Platform
  1. Windows
Hi All

I need to loop through the source folder and move all files that don't contain the string to the target folder.

I have read that using "Dir" is the best way to proceed, but would value your help with the code. What I have so far is below.

Many thanks :)

VBA Code:
Sub movefiles()

Dim SourceDir As String, TargetDir As String, Filename As String, LW1 as long

SourceDir = "S:\Test\"
TargetDir = "S:\Test\Completed\"
LW1 = Application.Evaluate("INT((TODAY()-1)/7)*7+1")
Filename = "*WE-" & LW1("dd-mm-yyyy") & "*.xlsx"

Do While (Dir(SourceDir <> ""))
    'If SourceDir file does not contain FileName string then move SourceDir file to TargetDir
Loop
   
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try using the "Like" keywork as it works with wildcards

VBA Code:
Sub movefiles()

Dim SourceDir As String, TargetDir As String, Filename As String, LW1 as long

SourceDir = "S:\Test\"
TargetDir = "S:\Test\Completed\"
LW1 = Application.Evaluate("INT((TODAY()-1)/7)*7+1")
Filename ="*WE-" & LW1("dd-mm-yyyy") & "*.xlsx"

Do While (Dir(SourceDir <> ""))
    'If SourceDir file does not contain FileName string then move SourceDir file to TargetDir
    If Dir Like Filename then 
            Move the file
    End If
Loop
  
End Sub
 
Upvote 0
Thank you @EFANYoutube

I have made progress where the files are move if the string is found within the filename. How would I turn the statement negative so files that don't match are moved? I tried "If Not" but nothing was moved?

Thanks again

VBA Code:
Sub Create()

Dim SourceDir As Variant, NewDir As Variant, FileName As Variant, LW1 As Long, SourceFiles As Variant

SourceDir = "S:\Test\"
NewDir = "S:\Test\Completed\"
LW1 = Application.Evaluate("INT((TODAY()-1)/7)*7+1")
FileName = "*WE-" & Format(LW1, "dd-mm-yyyy") & "*.xlsx"
SourceFiles = Dir(SourceDir & FileName)

With CreateObject("Scripting.FileSystemObject")
    Do While SourceFiles <> ""
    FileName = SourceFiles
        If SourceFiles Like FileName Then
            .MoveFile Source:=SourceDir & FileName, Destination:=NewDir & FileName
        End If
        SourceFiles = Dir
    Loop
End With

End Sub
 
Upvote 0
VBA Code:
Sub Create()

Dim SourceDir As Variant, NewDir As Variant, FileName As Variant, LW1 As Long, SourceFiles As Variant

SourceDir = "S:\Test\"
NewDir = "S:\Test\Completed\"
LW1 = Application.Evaluate("INT((TODAY()-1)/7)*7+1")
FileName = "*WE-" & Format(LW1, "dd-mm-yyyy") & "*.xlsx"
SourceFiles = Dir(SourceDir & FileName)

With CreateObject("Scripting.FileSystemObject")
    Do While SourceFiles <> ""
    FileName = SourceFiles
        If SourceFiles Like FileName Then
            .MoveFile Source:=SourceDir & FileName, Destination:=NewDir & FileName
        Else
            'Move everything else
        End If
        SourceFiles = Dir
    Loop
End With

End Sub
 
Upvote 0
How about this:
VBA Code:
Sub movefiles()

    Dim SourceDir As String, TargetDir As String, matchFilename As String, LW1 As Long
    Dim Filename As String
   
    SourceDir = "S:\Test\"
    TargetDir = "S:\Test\Completed\"

    If Right(SourceDir, 1) <> "\" Then SourceDir = SourceDir & "\"
    If Right(TargetDir, 1) <> "\" Then TargetDir = TargetDir & "\"
   
    LW1 = Int((Date - 1) / 7) * 7 + 1
    matchFilename = "*WE-" & Format(LW1, "dd-mm-yyyy") & "*.xlsx"
   
    Filename = Dir(SourceDir & "*.*")
    Do While Filename <> vbNullString
        If Not Filename Like matchFilename Then
            Name SourceDir & Filename As TargetDir & Filename
        End If
        Filename = Dir
    Loop
  
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

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