+ Reply to Thread
Results 1 to 32 of 32

Remove Suffix From Files

Hybrid View

  1. #1
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Remove Suffix From Files

    Hello,

    I'm looking to create a macro to remove a suffix from many files stored on my hard drive within subfolders. My files are stored in the order listed below. Can it be done?

    E: > Publishing > Catalog

    inside "Catalog" their are "artist" folders. Inside the "artist" folders, their are two subfolders, "Vox" and "Ins" The files with the suffix I want to remove are located in "Vox" and "Ins".

    The suffix starts with O-

    I want to remove the O- and everything that comes after it.

  2. #2
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Looking To Remove A Suffix From Files On Hard Drive

    hi Orangeworker,

    Yes, it can be done

    Here are some links that may help when modified & used in conjuction with the VBA "Name" statement:
    http://www.mrexcel.com/forum/showpos...68&postcount=2
    http://www.vbaexpress.com/forum/showthread.php?t=15194
    http://www.codeforexcelandoutlook.co...der-using-vba/
    Let us know if you need any help modifying them...

    Leith,
    I'm being a slacker & am off to bed so I'm sort of hoping you'll work your normal magic & give the OP a nice tidy finished product

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    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: Looking To Remove A Suffix From Files On Hard Drive

    Hello Orangeworker,

    This macro will rename the files with the "O-"suffix in the folders "Vox" and "Ins".
    Sub RenameFiles()
    
      Dim FileName As String
      Dim FilePath As String
      Dim NewFileName As String
      Dim RegExp As Object
      Dim vFolder As Variant
      
        FilePath = "E:\Publishing\Catalog\artist\"
        
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.IgnoreCase = True
        RegExp.Pattern = "(.+)(O\-.+)(\..*)"
            
          For Each vFolder In Array("Vox", "Ins")
            FileName = Dir(FilePath & vFolder & "*.*")
              While FileName <> ""
                If RegExp.Test(FileName) Then
                   NewFileName = RegExp.Replace(FileName, "$1$3")
                   Name FilePath & vFolder & FileName As FilePath & vFolder & NewFileName
                End If
                FileName = Dir()
              Wend
          Next vFolder
        
    End Sub
    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!)

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Looking To Remove A Suffix From Files On Hard Drive

    Nice one Leith

    I really have to learn how to use Regular Expressions, I just don't seem to have a use for it often enough to make a better effort at learning them.
    I may play with your code tomorrow to see if I can use the Reg Ex to improve my tests in http://www.excelforum.com/2451794-post13.html

    When I first read the op I thought that there were many different folders (for different artists) inside the Catalog folder. On re-reading the op, I think it may be that there are many different folders below "E:\Publishing\" for different "catalogs" with even more sub folders for each artist. Perhaps an extra couple of For Each loops are needed...?

    Rob

  5. #5
    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: Looking To Remove A Suffix From Files On Hard Drive

    Hello Rob,

    That thought had crossed my mind as well. I decided to go with the simpler option. If more sub-folders need to be searched then the code can be amended accordingly. We'll just have to wait for an answer.

  6. #6
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Looking To Remove A Suffix From Files On Hard Drive

    hi Leith,

    Good point
    I've just been reading some software design pages including http://en.wikipedia.org/wiki/You_ain%27t_gonna_need_it which ties right in with your use of the simpler option. It's good to see theoretical principles used in the real world - "yagni" or KISS is one I often have to remind myself of

    Rob

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Looking To Remove A Suffix From Files On Hard Drive

    Tha's why I'd prefer:

    Sub tst()
      With CreateObject("scripting.filesystemobject")
        For Each fl In .getfolder("E:\publishing\Catalog\Artist").subfolders
          c01 = Dir(fl & "\*O-*.*")
          Do While c01 <> ""
            Name fl & "\" & c01 As Split(c01, "O-")(0) & "." & .getextensionname(fl & "\" & c01)
            c01 = Dir
          Loop
        Next
      End With
    End Sub



  8. #8
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    Hi Everyone,

    Thanks for all the input on this...sorry it has taken me a few days to respond. I couldn't get any of the code to work just yet, but I think it's close. I believe the line that has "Artist" may be where the problem lies. For the sake of clarity, the directory is as such:

    E:\Publishing\Catalog

    inside "Catalog" their are artist folders/subfolders, like such:

    The Examples >
    Ins >
    Song One O-0001_Instrumental
    Song Two O-0002_Instrumental
    Vox >
    Song One O-0001
    Song Two O-0002

  9. #9
    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: Remove Suffix From Files

    Hello Orangeworker,

    I have change the main folder path. It is marked in bold. Test this out and let me the results.
    Sub RenameFiles()
    
      Dim FileName As String
      Dim FilePath As String
      Dim NewFileName As String
      Dim RegExp As Object
      Dim vFolder As Variant
      
        FilePath = "E:\Publishing\Catalog\"
        
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.IgnoreCase = True
        RegExp.Pattern = "(.+)(O\-.+)(\..*)"
            
          For Each vFolder In Array("Vox", "Ins")
            FileName = Dir(FilePath & vFolder & "*.*")
              While FileName <> ""
                If RegExp.Test(FileName) Then
                   NewFileName = RegExp.Replace(FileName, "$1$3")
                   Name FilePath & vFolder & FileName As FilePath & vFolder & NewFileName
                End If
                FileName = Dir()
              Wend
          Next vFolder
        
    End Sub

  10. #10
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    Hi Leith,

    The code seems to run in VB -- I don't get any errors, but it doesn't remove the O- and everything after it.
    Nothing has changed.

  11. #11
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    After reading "broro183's" helpful links, maybe we are missing the file extensions? The file extensions below are going to be the only types of files listed in the "Vox" and "Ins" folders.

    .aif
    .aiff
    .mp3
    .wav

  12. #12
    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: Remove Suffix From Files

    Hello Orangeworker,

    I apologize for the error. It appears the problem is due to a typo I made in the code. The code below has been corrected. The change has been marked in bold type.
    Sub RenameFiles()
    
      Dim FileName As String
      Dim FilePath As String
      Dim NewFileName As String
      Dim RegExp As Object
      Dim vFolder As Variant
      
        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.IgnoreCase = True
        RegExp.Pattern = "(.+)(O\-.+)(\..*)"
            
        FilePath = "E:\Publishing\Catalog\"
        FileName = "Song One O-0001_Instrumental.mp3"
        
        RegExp.Test (FileName)
        NewFileName = RegExp.Replace(FileName, "$1$3")
        
        
         For Each vFolder In Array("Vox", "Ins")
            FileName = Dir(FilePath & vFolder & "\*.*")
              While FileName <> ""
                If RegExp.Test(FileName) Then
                   NewFileName = RegExp.Replace(FileName, "$1$3")
                   Name FilePath & vFolder & FileName As FilePath & vFolder & NewFileName
                End If
                FileName = Dir()
              Wend
          Next vFolder
        
    End Sub

  13. #13
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    Hi Leith,

    No need to apologize; thank you for helping me! I tried the code and am still getting the same result.

    I noticed that you added this in the code:

    FileName = "Song One O-0001_Instrumental.mp3"

    It's not an actual song title but an example. Maybe it would be better to descibe it as:

    Example O-XXXX

    I just don't know enough about code to understand what you were doing there.

    Thanks

  14. #14
    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: Remove Suffix From Files

    Hello Orangeworker,

    I am not awake. That was a test line to check that the file name was parsing correctly, which it is. One possible cause would be an incorrect file path. Are the main folder path and the subfolder names correct?

    You may have to step through the macro to see what is and isn't happening. Do you know how to insert break points into the code?

  15. #15
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    Leith,

    The only thing I changed in the path was, "E:" to "C:" , for purposes of testing it out first. So, I moved a few folders to "C:\Publishing\Catalog"

    I'm not sure how to insert break points into the code, is it something fairly easily understood?

    Thanks

  16. #16
    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: Remove Suffix From Files

    Hello Orangeworker,

    Inserting Breakpoints
    1. Open the Visual Basic Editor from Excel using ALT+F11
    2. In the Project Window, Double Click the module the macro code is in.
    3. Move the mouse cursor down to the line Name FilePath & vFolder & FileName As FilePath & vFolder & NewFileName.
    4. Press F9 to set the breakpoint. Pressing F9 again will clear it.
    5. Run the macro using F5
    6. Move the mouse cursor to the variable NewFileName. This will display the contents.
    7. Go to the next file by pressing F5.

  17. #17
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    Leith,

    Got it. Will insert the break points and see what happens. In the meantime, I've attached a PDF snapshot of my directory...should have done this from the beginning.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    03-04-2010
    Location
    South Florida
    MS-Off Ver
    Excel 2007
    Posts
    72

    Re: Remove Suffix From Files

    When I inserted break points and ran the code, I got yellow highlights on the following lines

        Set RegExp = CreateObject("VBScript.RegExp")
        RegExp.IgnoreCase = True
        RegExp.Pattern = "(.+)(O\-.+)(\..*)"
            
        FilePath = "D:\Publishing\Catalog\"
        
        RegExp.test (FileName)
        NewFileName = RegExp.Replace(FileName, "$1$3")
        
        
         For Each vFolder In Array("Vox", "Ins")
            FileName = Dir(FilePath & vFolder & "\*.*")
              While FileName <> ""
    
    Next vFolder
        
    End Sub

  19. #19
    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: Remove Suffix From Files

    Hello Orangeworker,

    Unfortunately, colors are not rendered in the code window. You will need to bold the lines or add comments so I know which to look at.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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