Moving Specific files from one to another folder

Luka_LB

Board Regular
Joined
Nov 14, 2011
Messages
51
Hello everyone!

Came back to ask experts again :)

here is my issue,

I want to move all PDF files from one to another folder, which will be created depending on some conditions

I've looked through internet but either set fs = application.filesearch doesn't work, either code is opening window asking to indicate folder to look in for files.

can you please help, need just to move all pdf files from one specific to another specific folder, that's it!

many thanks in advance!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit
[/COLOR]
[COLOR=darkblue]Sub[/COLOR] MoveFiles()

    [COLOR=darkblue]Dim[/COLOR] sSourceFolder               [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sDestFolder                 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sFilename                   [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sFileType                   [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    sSourceFolder = "C:\Users\Domenic\Desktop\Folder A\"
    [COLOR=darkblue]If[/COLOR] Len(Dir(sSourceFolder, vbDirectory)) > 0 [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]If[/COLOR] Right(sSourceFolder, 1) <> "\" [COLOR=darkblue]Then[/COLOR] sSourceFolder = sSourceFolder & "\"
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "No source folder found.", vbCritical
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    sDestFolder = "C:\Users\Domenic\Desktop\Folder B\"
    [COLOR=darkblue]If[/COLOR] Len(Dir(sDestFolder, vbDirectory)) > 0 [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]If[/COLOR] Right(sDestFolder, 1) <> "\" [COLOR=darkblue]Then[/COLOR] sDestFolder = sDestFolder & "\"
    [COLOR=darkblue]Else[/COLOR]
        MsgBox "No destination folder found.", vbCritical
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    sFileType = "pdf"
    
    sFilename = Dir(sSourceFolder & "*." & sFileType, vbNormal)
    
    [COLOR=darkblue]If[/COLOR] Len(sFilename) = 0 [COLOR=darkblue]Then[/COLOR]
        MsgBox "No " & UCase(sFileType) & " files found.", vbInformation
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    
    [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] Len(sFilename) > 0
        Name sSourceFolder & sFilename [COLOR=darkblue]As[/COLOR] sDestFolder & sFilename
        sFilename = Dir
    [COLOR=darkblue]Loop[/COLOR]
    
    MsgBox "Completed...", vbInformation
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Change the path to the source and destination folders, accordingly.

Hope this helps!
 
Last edited:
Upvote 0
Thank you Domenic,

code is simple and does what it supposed to do! just what I wanted!

Thank you, it's very helpful for me!

if you don't mind I'll ask you one more question.

is it possible to make destination folder variable?

for e.g., in February code will create folder named "January" and all pdf files created should be moved to "January", in August, there will be "July" created and accordingly files should be moved to that.

is it possible?

thank you in advance!
 
Upvote 0
Domenic,

to give you just an idea,

maybe it will be easier if we put a cell in destination folder link, and changing that cell's value will cause folder address change.

it's just an idea, I can't write it in VBA :)

hope it helps

thanks
 
Upvote 0
The following macro will create a folder for the previous month, if one doesn't already exist...


Code:
    sDestFolder = "C:\Users\Domenic\Desktop\" & Format(DateSerial(Year(Date), Month(Date), 0), "mmmyy") & "\"
    [COLOR=darkblue]If[/COLOR] Len(Dir(sDestFolder, vbDirectory)) > 0 [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]If[/COLOR] Right(sDestFolder, 1) <> "\" [COLOR=darkblue]Then[/COLOR] sDestFolder = sDestFolder & "\"
    [COLOR=darkblue]Else[/COLOR]
        MkDir "C:\Users\Domenic\Desktop\" & Format(DateSerial(Year(Date), Month(Date), 0), "mmmyy") & "\"
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]

Note that the folder is named in the format month and year (ie. Oct14). Change the path, accordingly.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,680
Members
449,463
Latest member
Jojomen56

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