Consulting

Results 1 to 15 of 15

Thread: Add Date to all files in a folder

  1. #1

    Add Date to all files in a folder

    Hi,

    I need some help on making a VBA macro to add date "DDMMYYYY" to the end of all files in a specific folder.

    Let me know if you need anything else.

    Thanks

  2. #2
    Do you need help so you will be able to do it yourself or do you want a turn key solution ?

  3. #3
    Quote Originally Posted by snb View Post
    Do you need help so you will be able to do it yourself or do you want a turn key solution ?
    Turn Key definitely.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    If it's a one time thing, there are plenty of 'file rename' utilities that can add it to the end

    I use 'Lupas Rename 2000' at http://rename.lupasfreeware.org/download.php

    It's been out a long time, but has the features I use, including adding a suffix to file names in folders and sub-folders, as well as a lot of other things
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    i have a macro to copy files to a different folder. Then i wanted to add todays date as DDMMYYYY in that folder. I didn't want to use another application and use it in excel.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    OK, NP

    What's your macro?

    As you copy each file, could you just add

     .... & "-" & Format (Date, "ddmmyyyy")
    to each file name?
    ---------------------------------------------------------------------------------------------------------------------

    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

  7. #7
    Hi there,

    The above proposed approach is quite simple - I believe you will use it. But If you still want a bit another approach(for other tasks), I can give you the following Sub, which implies active "Microsoft Scripting Runtime" (it is easily activated through: "tools" > "references" and check the box "Microsoft Scripting Runtime")

    The proposed approach is flexible in terms of:
    - writing in the dialog window any date or other tag you may ever want (Region/Division etc) to use in files' names
    - picking the folder you want (if you need subfolders too - the code needs some corrections)


    Sub AddDateToAllFilesInFolder()
    Dim TargetFolder As Scripting.Folder
        Dim FileToChange As Scripting.File
        Dim DateToAdd As String
        Dim TargetFolderPath As String
        Dim fso As Scripting.FileSystemObject
    DateToAdd = InputBox("Inut The Date To Add or any other tag you wish to be used in files's names", "Add Date/Tag")
    Application.FileDialog(msoFileDialogFolderPicker).Show
        TargetFolderPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
    Set fso = New Scripting.FileSystemObject
        Set TargetFolder = fso.GetFolder(TargetFolderPath)
    For Each FileToChange In TargetFolder.Files
            FileToChange.Name = Replace(FileToChange.Name, "." & fso.GetExtensionName(FileToChange.Name), "") & " " & DateToAdd & "." & fso.GetExtensionName(FileToChange.Name)
        Next FileToChange
    End Sub





  8. #8
    Quote Originally Posted by Paul_Hossler View Post
    OK, NP

    What's your macro?

    As you copy each file, could you just add

     .... & "-" & Format (Date, "ddmmyyyy")
    to each file name?

    This is the current code. I'm using how would i implement this?

    Sub Copy_Folder()
    'This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
        Dim fso As Object
        Dim FromPath As String
        Dim ToPath As String
    FromPath = "C:\Users\tbent_000\Desktop\New folder"  '<< Change
        ToPath = "C:\Users\tbent_000\Desktop\New folder1\"    '<< Change
    If Right(FromPath, 1) = "\" Then
            FromPath = Left(FromPath, Len(FromPath) - 1)
        End If
    If Right(ToPath, 1) = "\" Then
            ToPath = Left(ToPath, Len(ToPath) - 1)
        End If
    Set fso = CreateObject("scripting.filesystemobject")
    If fso.FolderExists(FromPath) = False Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
    fso.CopyFolder Source:=FromPath, Destination:=ToPath
        MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    End Sub

  9. #9
    So you have sub folders also?

  10. #10
    Please do not quote !
    Please use code tags around VBA code !

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    I didn't realize you were using FSO and had subfolders

    Try something like this then but you'll have to copy one file at a time


    Option Explicit
    Dim oFSO As Object
    Dim FromPath As String
    Dim ToPath As String
    
    Sub Copy_Folders()
        
        FromPath = "L:\Test"
        ToPath = "L:\TestOut"
        
        If Right(FromPath, 1) = "\" Then FromPath = Left(FromPath, Len(FromPath) - 1)
        If Right(ToPath, 1) = "\" Then ToPath = Left(ToPath, Len(ToPath) - 1)
        
        Set oFSO = CreateObject("scripting.filesystemobject")
        
        If Not oFSO.FolderExists(FromPath) Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
        
        pvtCopyFolder oFSO.GetFolder(FromPath)
        MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    End Sub
    
    Private Sub pvtCopyFolder(FolderFrom As Object)
        
        Dim oSubFolder As Object
        Dim oFile As Object
        Dim sDestPath As String, sDestFile As String
        Dim i As Long
        
        sDestPath = FolderFrom.Path
        sDestPath = Right(sDestPath, Len(sDestPath) - Len(FromPath))
        If Left(sDestPath, 1) = "\" Then sDestPath = Right(sDestPath, (Len(sDestPath) - 1))
        sDestPath = ToPath & "\" & sDestPath
        
        If Not oFSO.FolderExists(sDestPath) Then oFSO.CreateFolder (sDestPath)
        
        
        For Each oSubFolder In FolderFrom.SubFolders
            pvtCopyFolder oSubFolder
        Next
        
        For Each oFile In FolderFrom.Files
        
            sDestFile = sDestPath & "\" & oFSO.getbasename(oFile.Path) & "-" & Format(Date, "ddmmyyyy") & "." & oFSO.getextensionname(oFile.Path)
            
            Call oFSO.copyfile(oFile.Path, sDestFile, True)
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  12. #12
    I'd like to give you my version. It makes almost no changes to your code (which you thoroughly understand) - just a line - to call a procedure. Hope you will like it. :-)

    [VBA]

    Sub Copy_Folder()
    ''This example copy all files and subfolders from FromPath to ToPath.
    'Note: If ToPath already exist it will overwrite existing files in this folder
    'if ToPath not exist it will be made for you.
    Dim fso As Object
    Dim FromPath As String
    Dim ToPath As String
    FromPath = "D:\Docs\Excel trials\Add Date To Files in Folder\Target Folder" '<< Change
    ToPath = "D:\Docs\Excel trials\Add Date To Files in Folder\Destination folder" '<< Change
    If Right(FromPath, 1) = "\" Then
    FromPath = Left(FromPath, Len(FromPath) - 1)
    End If
    If Right(ToPath, 1) = "\" Then
    ToPath = Left(ToPath, Len(ToPath) - 1)
    End If
    Set fso = CreateObject("scripting.filesystemobject")
    If fso.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
    End If
    fso.CopyFolder Source:=FromPath, Destination:=ToPath
    Call Add_Date_to_all_files_in_a_folder_and_subfolders(ToPath)
    MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    End Sub




    Sub Add_Date_to_all_files_in_a_folder_and_subfolders(ToPath As String)
    Dim fso As Scripting.FileSystemObject
    Dim FileToChange As Scripting.File
    Dim DateToAdd As String
    Dim Asubfolder As Scripting.Folder
    Set fso = New Scripting.FileSystemObject
    DateToAdd = Format(Date, "ddmmyyyy")
    For Each FileToChange In fso.GetFolder(ToPath).Files
    FileToChange.Name = Replace(FileToChange.Name, "." & fso.GetExtensionName(FileToChange.Name), "") & " " & DateToAdd & "." & fso.GetExtensionName(FileToChange.Name)
    Next FileToChange
    For Each Asubfolder In fso.GetFolder(ToPath).SubFolders
    Call Add_Date_to_all_files_in_a_folder_and_subfolders(Asubfolder.Path)
    Next Asubfolder
    End Sub
    [/VBA]

  13. #13
    I'll give you my version:

    Sub M_snb()
        sn = Filter(Split(CreateObject("wscript.shell").exec("cmd /c Dir G:\OF\* /b/s/a-d").stdout.readall, vbCrLf), ".")
        
        For Each it In sn
           Name it As Replace(it, ".", Format(Date, "yyyymmdd."))
        Next
    End Sub

  14. #14
    Quote Originally Posted by Paul_Hossler View Post
    I didn't realize you were using FSO and had subfolders

    Try something like this then but you'll have to copy one file at a time


    Option Explicit
    Dim oFSO As Object
    Dim FromPath As String
    Dim ToPath As String
    
    Sub Copy_Folders()
        
        FromPath = "L:\Test"
        ToPath = "L:\TestOut"
        
        If Right(FromPath, 1) = "\" Then FromPath = Left(FromPath, Len(FromPath) - 1)
        If Right(ToPath, 1) = "\" Then ToPath = Left(ToPath, Len(ToPath) - 1)
        
        Set oFSO = CreateObject("scripting.filesystemobject")
        
        If Not oFSO.FolderExists(FromPath) Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
        
        pvtCopyFolder oFSO.GetFolder(FromPath)
        MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    End Sub
    
    Private Sub pvtCopyFolder(FolderFrom As Object)
        
        Dim oSubFolder As Object
        Dim oFile As Object
        Dim sDestPath As String, sDestFile As String
        Dim i As Long
        
        sDestPath = FolderFrom.Path
        sDestPath = Right(sDestPath, Len(sDestPath) - Len(FromPath))
        If Left(sDestPath, 1) = "\" Then sDestPath = Right(sDestPath, (Len(sDestPath) - 1))
        sDestPath = ToPath & "\" & sDestPath
        
        If Not oFSO.FolderExists(sDestPath) Then oFSO.CreateFolder (sDestPath)
        
        
        For Each oSubFolder In FolderFrom.SubFolders
            pvtCopyFolder oSubFolder
        Next
        
        For Each oFile In FolderFrom.Files
        
            sDestFile = sDestPath & "\" & oFSO.getbasename(oFile.Path) & "-" & Format(Date, "ddmmyyyy") & "." & oFSO.getextensionname(oFile.Path)
            
            Call oFSO.copyfile(oFile.Path, sDestFile, True)
        Next
    End Sub
    Worked perfectly, how would i take off the sub folder option and make it faster

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    Probably something like this

    Option Explicit
     
    Sub Copy_A_Folder()
        Dim FromPath As String, ToPath As String
        Dim oFile As Object, oFolder As Object, oFSO As Object
        Dim sDestPath As String, sDestFile As String
         
        FromPath = "L:\Test"
        ToPath = "L:\TestOut1"
         
        If Right(FromPath, 1) = "\" Then FromPath = Left(FromPath, Len(FromPath) - 1)
        If Right(ToPath, 1) = "\" Then ToPath = Left(ToPath, Len(ToPath) - 1)
         
        Set oFSO = CreateObject("scripting.filesystemobject")
         
        If Not oFSO.FolderExists(FromPath) Then
            MsgBox FromPath & " doesn't exist"
            Exit Sub
        End If
         
         
        sDestPath = FromPath
        sDestPath = Right(sDestPath, Len(sDestPath) - Len(FromPath))
        If Left(sDestPath, 1) = "\" Then sDestPath = Right(sDestPath, (Len(sDestPath) - 1))
        sDestPath = ToPath & "\" & sDestPath
     
        If Not oFSO.FolderExists(ToPath) Then oFSO.CreateFolder (ToPath)
        Set oFolder = oFSO.GetFolder(FromPath)
         
        For Each oFile In oFolder.Files
            sDestFile = sDestPath & "\" & oFSO.getbasename(oFile.Path) & "-" & Format(Date, "ddmmyyyy") & "." & oFSO.getextensionname(oFile.Path)
            Call oFSO.copyfile(oFile.Path, sDestFile, True)
        Next
         
        MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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