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.

A Weekend in the VA Mountains

This past weekend we took a trip to the mountains of Virginia. It is an area we have visited before with the Boy Scout troop, but this time we went without the scouts. I have been riding consistently again for nearly a year after I bought a new to me cyclocross bike. You can read more about that here . After more than 2,800 miles on that bike since last June, it needed some more work. The headset was pretty much shot and would not be repaired in time for our planned trip to Virginia. So Gina suggested that I get a new bike. I ended up getting a Giant Revolt Advanced 2 from Giant Wake Forest . We arrived at our rental house very early Saturday morning. 1:30am in fact. We found a place to stay that was literally steps away from The Virginia Creeper Trail . Saturday was to be a day we went hiking at Grayson Highlands State Park . We met Gina's co-workers at the park just after 10:00 am and started our hike. We have camped at this park before with the Boy Scouts but have never ...

Buddhapalooza 2019

If you have been following this blog you will know that I have started cycling regularly again. In my past, I tried racing. I never did exceptionally well, but I did enjoy it. Now that I am back on a bike and getting fitter, I decided to give a race a try. I saw a notification for Buddhapalooza  and did some research. It looked like the perfect event for me to try racing again. It is set up as a fundraiser, 10 miles long (7 miles on road and 3 miles single track.) It looked like a fun, low-pressure event not so focused on winning and more on having fun. So I registered for the event and showed up to a course I have never ridden. Billed mainly as a mountain bike race, there were several other people on cross bikes like me. It was a Le Mans start, set your bike down in a given area, all the racers are fifty yards away on foot. When the race starts, you have to run to your bike and then start riding. I placed my bike close to the run start line. I think this was a mistake. By the ...