+ Reply to Thread
Results 1 to 3 of 3

Create FOLDER in subfoldesr and move folders with specific names into FOLDER

  1. #1
    Registered User
    Join Date
    12-01-2008
    Location
    India
    MS-Off Ver
    Microsoft 365 Enterprise - Excel version2301
    Posts
    63

    Create FOLDER in subfoldesr and move folders with specific names into FOLDER

    Hi,

    the below code creates a "2013" Folder in all the subfolder and move any "FILE having 2013 in it's name" to "2013" Folder, is it possible to move any "SUBFOLDER that has 2013 in it's name" to "2013" Folder? Current code moves only Files that have 2013 in their name but not subfolders that have 2013 in their name.

    Please Login or Register  to view this content.
    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 = "D:\Users\703038355\Desktop\oFile\" '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 & "\2013"
    If Not FSO.FolderExists(myNewFolder) Then
    MkDir (myNewFolder)
    myFile = Dir(mySubfolderPath & "\*2013*")
    Do While myFile <> ""
    FSO.MoveFile mySubfolderPath & "\" & myFile, myNewFolder & "\" & myFile

    myFile = Dir
    Loop

    End If
    Next SubFolder

    End Sub
    Please Login or Register  to view this content.
    Thanks in advance.

    Amar
    Last edited by Amarjeet Singh; 02-25-2015 at 09:56 AM. Reason: Code not in Quote

  2. #2
    Valued Forum Contributor Parth007's Avatar
    Join Date
    12-01-2014
    Location
    Banglore
    MS-Off Ver
    2010
    Posts
    879

    Re: Create FOLDER in subfoldesr and move folders with specific names into FOLDER

    Hi Amar,

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between
    Please Login or Register  to view this content.
    tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here


    Go through this Link - http://www.excelforum.com/forum-rule...rum-rules.html
    Regards
    Parth

    I appreciate your feedback. Hit * if u Like.
    Rules - http://www.excelforum.com/forum-rule...rum-rules.html

  3. #3
    Registered User
    Join Date
    12-01-2008
    Location
    India
    MS-Off Ver
    Microsoft 365 Enterprise - Excel version2301
    Posts
    63

    Re: Create FOLDER in subfoldesr and move folders with specific names into FOLDER

    [QUOTE=Amarjeet Singh;3999068]Hi,

    the below code creates a "2013" Folder in all the subfolder and move any "FILE having 2013 in it's name" to "2013" Folder, is it possible to move any "SUBFOLDER that has 2013 in it's name" to "2013" Folder? Current code moves only Files that have 2013 in their name but not subfolders that have 2013 in their name.

    Please Login or Register  to view this content.
    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 = "D:\Users\703038355\Desktop\oFile\" '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 & "\2013"
    If Not FSO.FolderExists(myNewFolder) Then
    MkDir (myNewFolder)
    myFile = Dir(mySubfolderPath & "\*2013*")
    Do While myFile <> ""
    FSO.MoveFile mySubfolderPath & "\" & myFile, myNewFolder & "\" & myFile

    myFile = Dir
    Loop

    End If
    Next SubFolder

    End Sub
    Please Login or Register  to view this content.
    Thanks in advance.

    Amar

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Create folder and move folders
    By Amarjeet Singh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2015, 11:56 AM
  2. [SOLVED] Create new folder and sub folders
    By Will_iam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2013, 04:10 AM
  3. Improvement - Option To Nest Folders as Sub Folder Under a Main Folder / Heading
    By :) Sixthsense :) in forum Suggestions for Improvement
    Replies: 4
    Last Post: 01-28-2013, 07:09 AM
  4. Replies: 6
    Last Post: 08-11-2006, 03:41 PM
  5. Replies: 2
    Last Post: 05-07-2006, 03:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1