Consulting

Results 1 to 4 of 4

Thread: How do I move files into latest created subfolder

  1. #1
    VBAX Regular
    Joined
    May 2018
    Posts
    7
    Location

    How do I move files into latest created subfolder

    Hi

    I have a folder that has subfolders in it.

    Im moving files into the folder but im trying to then move the files into the latest created subfolder.

    Can you do that via VBA?

  2. #2

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    https://ss64.com/vb/filesystemobject.html

    https://www.rondebruin.nl/win/s3/win026.htm


    FileSystemObject can move files

    Here's how to find the latest folder if you don't know it

    An example will help … like


    "I want to move all the files in Folder1 (which I know / would like to select) into the subfolder with the latest create date (which I want the system to find)

    Folder1
    File1
    File2
    FIle3

    SubfolderA (created 3/4/2019)

    SubfolderB (created 4/4/2019)


    So SubfolderB ends with File1, File2, and File3




    Option Explicit
    Sub test()
        MsgBox LatestFolder("C:\Users\Daddy\Documents")
    End Sub
    
    
    'https://ss64.com/vb/filesystemobject.html
    Function LatestFolder(sPath As String) As String
        Dim oFSO As Object, oFolders As Object, oFolder As Object
        Dim sNewestFolder As String
        Dim dtDate As Date
        
        
        dtDate = 0
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        
        Set oFolders = oFSO.GetFolder(sPath).SubFolders
        
        For Each oFolder In oFolders
            If oFolder.DateCreated > dtDate Then
                sNewestFolder = oFolder.Name
                dtDate = oFolder.DateCreated
                
    '            Debug.Print sNewestFolder, dtDate
                
            End If
        Next
        
        LatestFolder = sNewestFolder
       
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    May 2018
    Posts
    7
    Location
    spot on big thanks

Posting Permissions

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