Share via

Create Subfolders using VBA

Anonymous
2012-10-31T19:02:37+00:00

I have to add an empty subfolder called "2012" in about 1000 files, all in one directory.  Then I have to move anything with "2012" in the title into those folders.  Just to be clear here's a word pic"

C:  MainFolder \ subfolder 1 \ 2012

\ subfolder 2 \ 2012

\ subfolder 3 \ 2012

etc.

Can anyone help?

Thanks!

Microsoft 365 and Office | Install, redeem, activate | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-11-02T03:02:51+00:00

Here is a starting point.  Assuming you want to  add it to all first level subfolders of the main folder, try this

Option Explicit

Sub AddSubfolder()

'Under tools, add reference to Microsoft Scripting Runtime

Dim FSO As Scripting.Filesystemobject

Dim RootFolder As Object

Dim SubFolder As Object

Dim myFolder As String

Dim myNewFolder As String

Set FSO = CreateObject("Scripting.FileSystemObject")

myFolder = "C:\Users\User.Name\Documents"   'Change to identify your main folder

Set RootFolder = FSO.GetFolder(myFolder)

For Each SubFolder In RootFolder.SubFolders

    Debug.Print SubFolder.Path

    myNewFolder = SubFolder.Path & "\2012"

    If Not FSO.FolderExists(myNewFolder) Then

        MkDir (myNewFolder)

    End If

Next SubFolder

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-11-02T03:09:48+00:00

    To move a file with 2012 ANYWHERE in the name from the subfolder to the newfolder, try this.  I'd STRONGLY recommend that you step through this code using F8 until you are sure it does what you want. 

    Sub AddSubfolder()

    'Under tools, add reference to Microsoft Scripting Runtime

    Dim FSO As Scripting.Filesystemobject

    Dim RootFolder As Object

    Dim SubFolder As Object

    Dim myFolder As String

    Dim myNewFolder As String

    Dim mySubfolderPath As String

    Dim myFile As String

    Set FSO = CreateObject("Scripting.FileSystemObject")

    myFolder = "C:\Users\barbara.reinhardt\Documents"   'Change to identify your main folder

    Set RootFolder = FSO.GetFolder(myFolder)

    For Each SubFolder In RootFolder.SubFolders

        Debug.Print SubFolder.Path

        mySubfolderPath = SubFolder.Path

        myNewFolder = mySubfolderPath & "\2012"

        If Not FSO.FolderExists(myNewFolder) Then

            MkDir (myNewFolder)

            myFile = Dir(mySubfolderPath & "\*2012*")

            Do While myFile <> ""

                FSO.MoveFile mySubfolderPath & "" & myFile, myNewFolder & "" & myFile

                myFile = Dir

            Loop

        End If

    Next SubFolder

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2015-04-28T04:20:38+00:00

    How about this an alternative

    Public Function NewFolder(strFolder As String) As Boolean

      Dim fso As New FileSystemObject

      Dim strParentFolder As String

    On Error GoTo NewFolder_Error

        strParentFolder = fso.GetParentFolderName(strFolder)

        If Not fso.FolderExists(strParentFolder) Then

            NewFolder strParentFolder

            If Not fso.FolderExists(strParentFolder) Then

                fso.CreateFolder strParentFolder

            End If

            If fso.FolderExists(strParentFolder) Then

                fso.CreateFolder strFolder

                NewFolder = True

            End If

        Else

            If fso.FolderExists(strParentFolder) Then

                fso.CreateFolder strFolder

                NewFolder = True

            End If

        End If

    NewFolder_Error:

        If Err Then

            MsgBox "Error " & Err.Number & " (" & Err.Description & ") "

            Err.Clear

        End If

        Set fso = Nothing

    End Function

    Call NewFolder "C:\Temp\T1\T2\T3\T4"

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-11-12T20:13:47+00:00

    A zillion hours!  Wow!  ;)   I'm glad I could help.  

    Barb Reinhardt

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2012-11-12T20:06:50+00:00

    Barb, This worked perfectly and will save me a zillion hours.  Thank you so much!

    Was this answer helpful?

    0 comments No comments