Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

move files from folders and subfolder

0

Hi  experts 

I  need fix  this  dilemma   i have  files  pdf extension  in  folder ZZ    and  contains  some  subfolders  inside  the  folder ZZ    when  move  to  folder MMM   just  move  files from  subfolders  and  ignores   the  files  are  existed  in  folder ZZ  .

here  is  the  code 

Sub r()

Dim Fso As Object, objFolder As Object, objSubFolder As Object, ct As Long
Dim sourcePath, destPath As String
Dim FileInFolder As Object
sourcePath = "C:\Users\PC WORLD\Desktop\zz\"  'Change path and folder name to suit
destPath = "C:\Users\PC WORLD\Desktop\mmm\"  'Change path and folder name to suit
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = Fso.GetFolder(sourcePath)

For Each objSubFolder In objFolder.subfolders
    For Each FileInFolder In objSubFolder.Files
    If FileInFolder.Name Like "*.pdf*" Or FileInFolder.Name Like "*PDF*" Then
        ct = ct + 1
        FileInFolder.Move destPath
    End If
Next FileInFolder
Next objSubFolder
If ct > 0 Then
    MsgBox ct & " pdf files have been moved"
Else
    MsgBox "No pdf files found in the source folder"
End If
End Sub
Answer
Discuss

Answers

0
Selected Answer

Mussa

If you have files in a folder and first-level subfolders, this should work... 

In the modified code below, I've declared the variables ct and destpath at a module level so they can be used when each folder or subfolder name is passed to a (new) function which performs  your actions:

Dim ct As Long, destPath As String

Sub r()

Dim Fso As Object, objFolder As Object, objSubFolder As Object ', ct As Long
Dim sourcePath', destpath as String
Dim FileInFolder As Object

sourcePath = "C:\Users\PC WORLD\Desktop\zz\"  'Change path and folder name to suit
destPath = "C:\Users\PC WORLD\Desktop\mmm\"  'Change path and folder name to suit
Set Fso = CreateObject("Scripting.FileSystemObject")


LoopFolder (sourcePath)

Set objFolder = Fso.GetFolder(sourcePath)

For Each objSubFolder In objFolder.subfolders
    LoopFolder (objSubFolder)
Next objSubFolder

If ct > 0 Then
    MsgBox ct & " pdf files have been moved"
Else
    MsgBox "No pdf files found in the source folder"
End If
End Sub


Private Function LoopFolder(AFolder)
    Set Fso = CreateObject("Scripting.FileSystemObject")
    Set ThisFolder = Fso.GetFolder(AFolder)

    For Each FileInFolder In ThisFolder.Files
    If FileInFolder.Name Like "*.pdf*" Or FileInFolder.Name Like "*PDF*" Then
        ct = ct + 1
        FileInFolder.Move destPath
    End If
Next FileInFolder

End Function

Hope this works for you.

Discuss

Discussion

thanks  john,
it  gives error  invalid procedure call or  argument in this  line
FileInFolder.Move destPath
Mussa (rep: 48) Dec 14, '22 at 3:10 pm
Mussa. I didn't test that line but destpath needs to be declared at a module level too - please see my revised answer
John_Ru (rep: 6152) Dec 14, '22 at 3:48 pm
this  great! 
everything  is  ok .
thanks  very much .
Mussa (rep: 48) Dec 14, '22 at 3:51 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login