Skip to main content

Redoing Renaming Files

I put this up on my other more work oriented blog, but thought I would share it here too.

In a previous post I explained how to batch rename files using a little DOS, Excel and Notepad. Now I have found a quicker way to accomplish this task. I searched for a vbScript and found this example. This was a great start, I just wanted to make it a little easier for the non-dos friendly users and get it away from the command line.  So here is my modified script.

Option Explicit

Dim StdIn:  Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Dim fso:    Set fso = CreateObject("Scripting.FileSystemObject")

Dim FilesRenamed:   FilesRenamed = 0
Dim FilesSkipped:   FilesSkipped = 0

dim path
dim olds
dim news
dim ext1
dim ext2

Main

set fso = nothing

Sub Main
 
   'get the parameter list

path=InputBox("Specify Path Of Files To Rename","Enter path")
olds=InputBox("Enter String To Replace","String To Replace")
news=InputBox("Enter New String","New String")
ext1=InputBox("Enter Original Extension","Old Extension","pdf")
ext2=InputBox("Enter New Extension","New Extension", "pdf")

    dim CurrentFolder: Set CurrentFolder = fso.GetFolder(path)

    ProcessFolder CurrentFolder , olds, news, ext1,ext2

    StdOut.Echo "Files renamed :" & FilesRenamed
    StdOut.Echo "Files Skipped :" & FilesSkipped

End Sub

Sub ProcessFolder (ByVal folder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
   Dim Files: Set Files = folder.Files

   Dim File
   For Each File In Files

      If inStr(1,File.Name,oldTag) > 0 Then
 
         if (extOld <> "" and extNew <> "") then
            StdOut.Echo Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
            File.Move Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
         else
            StdOut.Echo Replace(File.Path,oldTag,newTag)
            File.Move Replace(File.Path,oldTag,newTag)
         end if
             
         FilesRenamed = FilesRenamed + 1
      Else
         FilesSkipped = FilesSkipped + 1
       End If
   Next
End Sub

I then created a bat file to run the script.

cscript renameFiles.vbs

All the end user has to do is double click the bat file and fill in the required information in dialog boxes.

Watch the video below to see it in action


Comments

Popular posts from this blog

Name The People

I was asked by a co-worker to create some Revit families from CAD blocks. They are 2D plan representations of people. I asked the requester if she wanted them named any certain way because this is what I saw.

New Driver + New Car = Less $

Well, the kids are getting older and today our eldest received his license from the state of NC. So that means our insurance will increase, dramatically. Not only that, but we are once again a 3 car family. The wife has the Kia Sedona so that we can accommodate the family of six. We still have the Dodge Charger, and last week I bought a 2016 Hyundai Veloster. Gina and I had been in discussions for a couple of weeks about the "car situation" and I had been tentatively shopping online for a small hatchback. After searching Autotrader , Carmax , Carvana I found the Hyundai through Autotrader at a local dealer . So I messaged them online to make sure the car I had seen was still available and to make arrangements to see the car and take it for a test drive. I left work last Wednesday at lunch and made my way to the dealer. After driving the car I decided it was the right price and just what I had been looking for. Gina was away on an overnight field trip so I sent some tex...

A Thursday Weekend Wrap-up

It was another concert weekend. We kicked it off Friday at Local 506 seeing regional bands Youth League , Knowne Ghost and Cuzco . Earlier in the week my son told me that he remembered seeing the bassist for Youth League at Wreckingball last summer. When we arrived at the club I saw the band outside talking. I told my son to ask him if he was indeed there last summer. Being too shy he declined. I on the other hand have not many reservations about approaching people on the street to talk to them. When asked, he confirmed that he was there. Knowne Ghost was a last minute replacement for Outbound and I was grateful because I was able to hear a "new to me" band. Youth League Saturday morning was an early start. I got up early to see Gina off for her trip to the SAS conference in Orlando. I watched the first half of the Merseyside Derby. Then I headed out to watch local soccer as some of my friends were playing each other before my own 10:00 kickoff.  My game was a victory...