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!
brasimanAsked:
Who is Participating?
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 PrewCommented:
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?

~bp
0
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\.
0
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. :)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bill PrewCommented:
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:  http://www.jpsoftwaretech.com/vba/filesystemobject-vba-examples/

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 http://www.jpsoftwaretech.com/creating-subdirectories-in-vba-when-parent-folder-doesnt-exist/

   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) & "\"
      Else
         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 http://allenbrowne.com
   On Error Resume Next
   DirExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)

End Function

Open in new window

~bp
1

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.
0
Bill PrewCommented:
Did you past in all the code I put up, there were three routines that are needed, including the one you mentioned.

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

~bp
0
brasimanAuthor Commented:
Thanks again!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.