+ Reply to Thread
Results 1 to 27 of 27

Renaming WAV Files in a Folder

Hybrid View

spiwere Renaming WAV Files in a Folder 06-06-2014, 06:58 AM
spiwere Re: Renaming WAV Files in a... 06-07-2014, 06:58 AM
spiwere Re: Renaming WAV Files in a... 06-08-2014, 02:47 PM
Leith Ross Re: Renaming WAV Files in a... 06-08-2014, 03:43 PM
spiwere Re: Renaming WAV Files in a... 06-09-2014, 03:14 AM
spiwere Re: Renaming WAV Files in a... 06-08-2014, 11:51 PM
spiwere Re: Renaming WAV Files in a... 06-08-2014, 11:52 PM
Leith Ross Re: Renaming WAV Files in a... 06-09-2014, 11:26 AM
LJMetzger Re: Renaming WAV Files in a... 06-09-2014, 12:22 PM
spiwere Re: Renaming WAV Files in a... 06-10-2014, 04:28 AM
spiwere Re: Renaming WAV Files in a... 06-10-2014, 04:16 AM
Leith Ross Re: Renaming WAV Files in a... 06-10-2014, 01:49 PM
spiwere Re: Renaming WAV Files in a... 06-11-2014, 05:00 AM
LJMetzger Re: Renaming WAV Files in a... 06-10-2014, 01:52 PM
spiwere Re: Renaming WAV Files in a... 06-11-2014, 04:29 AM
spiwere Re: Renaming WAV Files in a... 06-10-2014, 01:58 PM
LJMetzger Re: Renaming WAV Files in a... 06-11-2014, 09:39 AM
spiwere Re: Renaming WAV Files in a... 06-11-2014, 02:30 PM
spiwere Re: Renaming WAV Files in a... 06-12-2014, 12:18 PM
LJMetzger Re: Renaming WAV Files in a... 06-12-2014, 03:01 PM
spiwere Re: Renaming WAV Files in a... 06-13-2014, 05:26 AM
LJMetzger Re: Renaming WAV Files in a... 06-14-2014, 03:16 PM
spiwere Re: Renaming WAV Files in a... 06-15-2014, 12:50 PM
LJMetzger Re: Renaming WAV Files in a... 06-15-2014, 06:39 PM
spiwere Re: Renaming WAV Files in a... 06-16-2014, 01:32 PM
LJMetzger Re: Renaming WAV Files in a... 06-16-2014, 02:54 PM
spiwere Re: Renaming WAV Files in a... 06-19-2014, 11:40 AM
  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Renaming WAV Files in a Folder

    Hello spiwere,

    I have made a few changes to the code . This should work for now. Here is the updated code and workbook.

    Macro Version 2
    Option Explicit
    
    Sub CopyAndRenameFiles()
    
      ' Thread:  http://www.excelforum.com/excel-programming-vba-macros/1016265-renaming-wav-files-in-a-folder.html
      
        Dim colFiles     As Collection
        Dim EndRow       As Long
        Dim fc           As Long
        Dim Files        As Object
        Dim FileType     As String
        Dim Item         As Variant
        Dim NewName      As String
        Dim oFolder      As Object
        Dim oShell       As Object
        Dim ParentFolder As Variant
        Dim r            As Long
        Dim Rng          As Range
        Dim SubFolder    As Variant
        Dim Title        As String
        Dim Wks          As Worksheet
        
        Const ssfPersonal   As Long = 5     ' Points to My Documents folder.
        
            FileType = ".wav"
            
          ' This can be a Shell special folder constant or a folder path.
            ParentFolder = "C:\Users\ABC\Desktop\Renaming\Spi"
            
            SubFolder = "New Folder"
            
            Set Wks = ActiveSheet
            
            Set Rng = Wks.Range("B2:E2")
            
              ' Find the last row with data.
                EndRow = Wks.Cells.Find("*", , xlFormulas, xlWhole, xlByRows, xlPrevious, False, False, False).Row
                If EndRow < Rng.Row Then Exit Sub
            
                Set Rng = Rng.Resize(RowSize:=EndRow - Rng.Row + 1)
                
                    Set oShell = CreateObject("Shell.Application")
                    
                    On Error Resume Next
                        ParentFolder = oShell.Namespace(ParentFolder).Self.Path
                        If Err <> 0 Then
                            MsgBox "The folder '" & ParentFolder & " was Not Found.", vbCritical
                            Exit Sub
                        End If
                    On Error Resume Next
                    
                  ' Let the User choose a different folder if needed.
                    Title = "The Defauilt Parent Directory is ...   " & ParentFolder & vbCrLf
                    Title = Title & "You may choose a different folder if needed." & vbCrLf & "Click Cancel to use the Default Folder."
                    Set oFolder = oShell.BrowseForFolder(0, Title, &H271)
                    
                    If Not oFolder Is Nothing Then ParentFolder = oFolder.Self.Path
                            
                  ' Add characters if needed for proper syntax.
                    ParentFolder = IIf(Right(ParentFolder, 1) <> "\", ParentFolder & "\", ParentFolder)
                    SubFolder = ParentFolder & IIf(Right(SubFolder, 1) <> "\", SubFolder & "\", SubFolder)
                    FileType = IIf(Left(FileType, 1) <> ".", "." & FileType, FileType)
                    
                  ' Create the Subfolder if it does not exist.
                    If oShell.Namespace(SubFolder) Is Nothing Then MkDir SubFolder
            
                  ' Create a List only the files of the given file type.
                    Set Files = oFolder.Items
                    
                    Files.Filter 64, "*" & FileType
                    
                  ' Create a collection of unique Ids for the these files.
                    Set colFiles = New Collection
                    
                        For Each Item In Files
                          ' Unique Id is before first space in the file name.
                            colFiles.Add True, Left(Item.Name, InStr(1, Item.Name, " ") - 1)
                        Next Item
                        
                      ' Only copy and rename files on the worksheet if the file's unique Id is in the Collection.
                        For Each Item In Rng.Columns(1).Cells
                            r = r + 1
                            On Error Resume Next
                                If colFiles(Item.Value) Then
                                    If Err = 0 Then
                                        NewName = Format(Rng.Cells(r, "C"), "md")
                                        NewName = NewName & Format(Rng.Cells(r, "D"), "hhMMss")
                                        NewName = NewName & "_" & Rng.Cells(r, "E")
                                        Name ParentFolder & Item.Value As SubFolder & NewName & FileType
                                        fc = fc + 1
                                    End If
                                End If
                            On Error GoTo 0
                        Next Item
                    
                    MsgBox fc & " Files were Renamed and Copied to " & vbCrLf & SubFolder & ".", vbInformation
                    
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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. Listing/Renaming Files in a folder from excel
    By Kelshaer in forum Excel Tips
    Replies: 4
    Last Post: 01-23-2013, 11:04 AM
  2. Replies: 1
    Last Post: 01-15-2013, 03:05 AM
  3. Merging excel files + renaming them with folder name + restructuring them
    By BohkAl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2011, 07:47 AM
  4. Renaming multiple files in a Folder
    By csmithee in forum Excel General
    Replies: 1
    Last Post: 06-11-2009, 02:36 PM
  5. Excel files replicating and renaming in a folder
    By hpum in forum Excel General
    Replies: 0
    Last Post: 07-21-2007, 09:23 AM

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