Welcome !
         

 vbscript - How to rename similar files in a folder?

Author Message
Clutch

  • Total Posts : 24
  • Scores: 0
  • Reward points : 0
  • Joined: 9/4/2015
  • Status: offline
vbscript - How to rename similar files in a folder? Tuesday, January 10, 2017 4:33 AM (permalink)
0
I need help in what I thought even for a nube would be a fairly simple script.  But of course…. NOT! 
Basically, I need to rename all files in a selected folder. 
 
Example, what I want is from this:
SOCAL_CP62.shp
SOCAL_CP62.dbf
SOCAL_CP62.prj
SOCAL_CP63.shp
SOCAL_CP63.dbf
SOCAL_CP63.prj
 
To this:
SOCAL_CP62_roadbuff.shp
SOCAL_CP62_roadbuff.dbf
SOCAL_CP62_roadbuff.prj
SOCAL_CP63_roadbuff.shp
SOCAL_CP63_roadbuff.dbf
SOCAL_CP63_roadbuff.prj
 
FYI-
'SOCAL' will be a variable
the alphanumeric 'CP62' would be variable
the 'roadbuff' would be constant.
and the extensions must remain the same.
 
So I can add in the "_roadbuff" with this script:
 
Dim objFSO            : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim sFolder1        : sFolder1 = "S:\SOCAL\Section_20\Road Buffers"
Dim sFile, sName

For Each sFile In objFSO.GetFolder(sFolder1).Files
    sName = Replace(sFile.Name, sFile.Name, sFile.Name & "_roadbuff")
    sFile.Name = sName
Next
 
This gives me: Socal_CP62.shp_roadbuff
 
So I need to get the file extensions moved to the end? Or, I can set up the “Before” files to look like this first:
SOCAL_roadbuff_CP62.shp
SOCAL_roadbuff_CP62.dbf
SOCAL_roadbuff_CP62.prj
SOCAL_roadbuff_CP63.shp
SOCAL_roadbuff_CP63.dbf
SOCAL_roadbuff_CP63.prj
 
So if I attempt this way I could parse each file using the “_” and I believe that would give me an Array? But I get lost on how to script telling which part of the Array to swap parts. Do they get numbered automatically? Do I need to declare the parts of the Array? And then there’s that extension part again. As you can see any input would be most helpful.
 
 
#1
    59cobalt

    • Total Posts : 2632
    • Scores: 253
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:vbscript - How to rename similar files in a folder? Tuesday, January 10, 2017 9:45 AM (permalink)
    0
    Appending something to the basename of a file is fairly trivial. Change this:
    sName = Replace(sFile.Name, sFile.Name, sFile.Name & "_roadbuff")
    into this:
    sName = objFSO.GetBaseName(sFile.Name) & "_roadbuff." & objFSO.GetExtensionName(sFile.Name)

     
    #2
      Clutch

      • Total Posts : 24
      • Scores: 0
      • Reward points : 0
      • Joined: 9/4/2015
      • Status: offline
      Re:vbscript - How to rename similar files in a folder? Thursday, January 12, 2017 5:02 AM (permalink)
      0
      Thx for such a quick reply.  Ya know... I did try it with the GetBaseName earlier and was having issues but musta did something wrong as yours works like a charm. 
       
      Many thx!
       
      #3
        59cobalt

        • Total Posts : 2632
        • Scores: 253
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:vbscript - How to rename similar files in a folder? Thursday, January 12, 2017 6:40 AM (permalink)
        0
        You're welcome.
         
        #4
          Clutch

          • Total Posts : 24
          • Scores: 0
          • Reward points : 0
          • Joined: 9/4/2015
          • Status: offline
          Re:vbscript - How to rename similar files in a folder? Thursday, January 12, 2017 7:04 AM (permalink)
          0
          Mmmmm.... something happening.   I am getting a double-entry on a few files.  My test sample renames 100 files but nine of them come out as for example:
          SOCAL_CQ64_roadbuff_roadbuff.dbf
           
          I am adding a little delay (Sleep), into the script to see if that helps but it's only 100 files and the files sizes are very small <500kb, should that make any difference.
          Any ideas?
           
           
          #5
            Clutch

            • Total Posts : 24
            • Scores: 0
            • Reward points : 0
            • Joined: 9/4/2015
            • Status: offline
            Re:vbscript - How to rename similar files in a folder? Thursday, January 12, 2017 8:10 AM (permalink)
            0
            As I investigate further it appears the exact same files always have the repeat.  I have run the script inside a bigger script (where it will end up) and I have run the script as it's own.  Same files have duplication.  Time delays have no effect.
             
            I do see somewhat of a pattern,  On my latest test I always encounter these files with the double _roadbuff:
            SOCAL_CQ62_roadbuff_roadbuff.cpg
            SOCAL_CQ62_roadbuff_roadbuff.dbf
            SOCAL_CQ62_roadbuff_roadbuff.prj

            SOCAL_CR63_roadbuff_roadbuff.cpg
            SOCAL_CS64_roadbuff_roadbuff.cpg
            SOCAL_CT65_roadbuff_roadbuff.cpg
             
            The pattern I see is
            CQ62  (with the addition of two other CQ62 files)
            CR63
            CS64
            CT65
            My theory is each file's "before" file is created correctly but for some unknown reason these files get looped a second time so for example
            SOCAL_CQ62.cpg is then looped to create
            SOCAL_CQ62_roadbuff.cpg but then this is looped again taking this as the new basename and adding _roadbuff once again resulting in
            SOCAL_CQ62_roadbuff_roadbuff.cpg.
            Why only these files I do not know?
             
            Ideas, thoughts?
             
            For clarification here is the script I run on it's own:
            Dim objFSO            : Set objFSO = CreateObject("Scripting.FileSystemObject")
            Dim sFolder1          : sFolder1 = "S:\SOCAL\Section_20\Road Buffers"
            Dim sFile, sName

            For Each sFile In objFSO.GetFolder(sFolder1).Files
                sName = objFSO.GetBaseName(sFile.Name) & "_roadbuff." & objFSO.GetExtensionName(sFile.Name)
                sFile.Name = sName
            Next
            <message edited by Clutch on Thursday, January 12, 2017 8:13 AM>
             
            #6
              59cobalt

              • Total Posts : 2632
              • Scores: 253
              • Reward points : 0
              • Joined: 7/17/2011
              • Status: offline
              Re:vbscript - How to rename similar files in a folder? Thursday, January 12, 2017 10:25 AM (permalink)
              0
              Add a filter that prevents renaming the file if its basename already has that suffix:
              If Right(objFSO.GetBaseName(sFile.Name), 9) <> "_roadbuff" Then
                sName = objFSO.GetBaseName(sFile.Name) & "_roadbuff." & objFSO.GetExtensionName(sFile.Name)
                sFile.Name = sName
              End If

               
              #7
                Clutch

                • Total Posts : 24
                • Scores: 0
                • Reward points : 0
                • Joined: 9/4/2015
                • Status: offline
                Re:vbscript - How to rename similar files in a folder? Saturday, January 14, 2017 7:23 AM (permalink)
                0
                Yep.... that did it!
                 
                #8
                  Online Bookmarks Sharing: Share/Bookmark

                  Jump to:

                  Current active users

                  There are 0 members and 1 guests.

                  Icon Legend and Permission

                  • New Messages
                  • No New Messages
                  • Hot Topic w/ New Messages
                  • Hot Topic w/o New Messages
                  • Locked w/ New Messages
                  • Locked w/o New Messages
                  • Read Message
                  • Post New Thread
                  • Reply to message
                  • Post New Poll
                  • Submit Vote
                  • Post reward post
                  • Delete my own posts
                  • Delete my own threads
                  • Rate post

                  2000-2017 ASPPlayground.NET Forum Version 3.9