Announcement

Collapse
No announcement yet.

How to use Rename to append sequenced numbers

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use Rename to append sequenced numbers

    Is there a way to use the Rename method (or maybe some other tool) such that you can rename a selection of files where the new name has prefixed or postfixed a number incremented with each rename? For example, I would like to rename:

    abc.txt
    defgh.txt
    i.txt

    to:

    1-abc.txt
    2-defgh.txt
    3-i.txt

    Thanks.

  • #2
    Use the FileSystemObject in a vbScript. Create a text file named "Rename.vbs" and copy in the following
    Code:
    Option Explicit
    
    Dim fso, oFolder, oFile, iCount
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    iCount = 0
    Set oFolder = fso.GetFolder(fso.GetAbsolutePathName("."))
    For Each oFile In oFolder.Files
        If oFile.Name <> "Rename.vbs" Then
            iCount = iCount + 1    
            oFile.Name = iCount & "-" & oFile.Name
        End If
    Next
    Set oFile = Nothing
    Set oFolder = Nothing
    Set fso = Nothing
    Drop the file into your folder and double-click on it to execute it.
    BC v4.0.7 build 19761
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

    Comment


    • #3
      Thanks for this.

      Instead of:

      oFile.Name = iCount & "-" & oFile.Name

      will this work?

      oFile.Name = oFolder.Name & ".jpg"

      (obviously I'm preferring to grab the folder naming structure and placing it into the filename itself)
      Last edited by Seeker; 05-Jun-2008, 02:41 PM.

      Comment


      • #4
        To recursively rename files (including in subfolders), you can start with this:
        Code:
        Option Explicit
        
        Dim fso, oFolder, oSubFolder, oFile, sPath
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        sPath = fso.GetFolder(fso.GetAbsolutePathName(".")) + "\"
        RenameFiles(sPath)
        
        Sub RenameFiles(Path)
            Dim FolderPath
            FolderPath = Path
            Set oFolder = fso.GetFolder(FolderPath)
            For Each oFile In oFolder.Files
                If oFile.Path <> sPath + "Rename.vbs" Then
                   oFile.Name = oFolder.Name & "_" & oFile.Name
                End If
            Next
            For Each oSubFolder In oFolder.Subfolders
                RenameFiles(oSubFolder.Path)
            Next
        End Sub
        
        Set oFile = Nothing
        Set oSubFolder = Nothing
        Set oFolder = Nothing
        Set fso = Nothing
        BC v4.0.7 build 19761
        ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

        Comment


        • #5
          Originally posted by Seeker View Post
          will this work?

          oFile.Name = oFolder.Name & ".jpg"
          Yes, you can use just the folder name so long as you are only renaming a single file in each subfolder and know that the file is a .jpg image file. It's a little dangerous, though, as it would rename any file format to .jpg even if it was something else...

          Also, if the same subfolder exists under more than one tree, you would still have duplicate file names...unless you renamed the file based on the relative path from the root folder as follows:

          NewName = Replace(Mid(oFile.Path, Len(sPath) + 1), "\", "_")
          If oFile.Name <> NewName Then oFile.Name = NewName
          Last edited by Michael Bulgrien; 06-Jun-2008, 09:31 AM.
          BC v4.0.7 build 19761
          ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

          Comment


          • #6
            Using the second vbScript as a starting point, you can do most anything you want with it.

            Instead of:
            If oFile.Path <> sPath + "Rename.vbs" Then
            You could say:
            If oFile.Name = "Folder.jpg" Then
            Or:
            Select Case oFile.Type
            Case "GIF Image", "JPG Image", "PNG Image", "Text Document"
            End Select
            (with your action coded under the case statement)
            File Types would need to match what shows up for the file type in Windows Explorer.
            BC v4.0.7 build 19761
            ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

            Comment


            • #7
              Thanks for the reply. I was hoping there was something built into BC3 to do this

              Comment


              • #8
                I don't see anything pertinent in the help file for Regular Expression Renames, although renaming "common" portions of filenames is pretty straightforward. Michael Bulgrien posted a screencast link about renames recently.

                ACDSee's 10 Photomanager product (and older versions at least back to 5.0) does provide the feature you want with their batch rename rules, and so does BRU (Bulk Rename Utility). The last one is free, IIRC, but I'm a heavy ACDSee user, so I was more than willing to purchase that.

                Perhaps Aaron or Chris can add an enumerator option. I've tried attaching a screenshot of Photomanager's dialog showing one type of usage.

                /dps

                Comment

                Working...
                X