Macro that moves all files to a new created folder with today's date

I am trying to create a macro that moves all files from one folder, creates a new folder titled with today's date and moves those files into the newly created folder. Could I get some help with that? Thanks!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
You have listed several topic areas, but you mention macro in the question title.  Do you want a VBA macro that can be executed inside Excel?  Or something else?

brasimanAuthor Commented:
Hi Bill, sorry for the confusion. I am new to this and didn't even realize I referenced multiple topics. :) You are correct, in excel, I would like to create a button that runs a script to take files in a specific folder on my C drive, create a new folder with today's date in a different folder on my C drive, then paste those files into the newly created folder. I have multiple .xls and .csv files in C:\temp. When i click on this button in Excel, I am hoping to move those files in C:\temp and create a folder with today's date in C:\Complete\.
brasimanAuthor Commented:
I just remembered too, I also have other .xls and .csv files in another folder, C:\temp2 that another employee accesses. I did it that way for permissions on my network. So, I am looking to click a button in Excel which will take all the files in C:\temp and C:\temp2, combine them into one newly created folder with today's days in C:\Complete\. I hope i'm making sense. :)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Bill PrewIT / Software Engineering ConsultantCommented:
Here is a VBA sub that should be very close to what you are describing.  Add this to your Excel file and then run it as a marco.  You can certainly add a button that references it as well, once you get the logic working just right.  You will need to include a reference to the filesystem object as well, as mentioned right at the top of this reference page:

Sub MoveFiles()

   Dim fso As Scripting.FileSystemObject
   Dim FromDir As String
   Dim ToDir As String

   ' Load file system object
   Set fso = CreateObject("Scripting.FileSystemObject")

   ' Specify paths to work with
   FromDir1 = "B:\EE\EE28704614\From1\*.*"
   FromDir2 = "B:\EE\EE28704614\From2\*.*"
   ToDir = "B:\EE\EE28704614\To\" & Format(Now, "yyyy-mm-dd") & "\"
   ' Create new folder with todays date
   CreateDir (ToDir)

   ' Move files
   fso.MoveFile FromDir1, ToDir
   fso.MoveFile FromDir2, ToDir

   Set fso = Nothing

End Sub

Sub CreateDir(fullPath As String)

   ' from

   Dim str As String
   Dim strArray As Variant
   Dim i As Long
   Dim basePath As String
   Dim newPath As String

   str = fullPath

   ' add trailing slash
   If Right$(str, 1) <> "\" Then
      str = str & "\"
   End If

   ' split string into array
   strArray = Split(str, "\")

   basePath = strArray(0) & "\"

   ' loop through array and create progressively
   ' lower level folders
   For i = 1 To UBound(strArray) - 1
      If Len(newPath) = 0 Then
         newPath = basePath & newPath & strArray(i) & "\"
         newPath = newPath & strArray(i) & "\"
      End If

      If Not DirExists(newPath) Then
         MkDir newPath
      End If
   Next i

End Sub

Function DirExists(ByVal strPath As String) As Boolean

   ' from
   On Error Resume Next
   DirExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)

End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brasimanAuthor Commented:
Thanks Bill. When I run it, i get an error. It says Combile error: Sub or Function not identified. It is highlighting the "CreateDir (ToDir)" line.
Bill PrewIT / Software Engineering ConsultantCommented:
Did you past in all the code I put up, there were three routines that are needed, including the one you mentioned.

brasimanAuthor Commented:
Yes. I will re-paste it now and try it again.
brasimanAuthor Commented:
I must have not copied it all. You're awesome. That worked exactly like i was hoping. Thank you!!!
brasimanAuthor Commented:
Awesome, thank you!!
Bill PrewIT / Software Engineering ConsultantCommented:
Great, glad you got it working and it was useful.

brasimanAuthor Commented:
Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.