VBScript Get File Extension
Before anything else, you may love to read this book too!...
Actually, I got this question from the reader of my previous blog post about VBScript to Replace Underscores in Filename with Spaces for All Files in a Folder, and I decided to answer it here.
Question from Justin:
I tried something simular..but modified your VBS..
i have Files in a given folder that has Names in them...
ie.
With the modified VBS above Ive run it and i get the following...
My First answer:
Dear Justin,
check your code again. Search for line of code containing
Specifically, the code:
Justin Replies:
Ok i Changed the Line in question...and Reran the code...
Now i get...
They Should read...
My Answer:
If that is the case, you should get and strip the file extension before you replace the name and add it back to the filename after replacement. Here is the snippet:
and your new code should look like this:
Actually, I got this question from the reader of my previous blog post about VBScript to Replace Underscores in Filename with Spaces for All Files in a Folder, and I decided to answer it here.
Question from Justin:
I tried something simular..but modified your VBS..
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder(".")
' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "The ") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "The ", "") & ", The"
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"
i have Files in a given folder that has Names in them...
ie.
The Last Star Fighter Gone with the wind The Good, the Bad, and the Ugly.
With the modified VBS above Ive run it and i get the following...
Last Star Fighter.mp4, The Gone with the Wind Good, the Bad, and the Ugly.mp4, TheHow do i get rid of the extention in the middle of the name? What am I doing wrong?
My First answer:
Dear Justin,
check your code again. Search for line of code containing
"sName = Replace"and remove the code after ')'.
Specifically, the code:
'& ", The"'have to be removed.
Justin Replies:
Ok i Changed the Line in question...and Reran the code...
Now i get...
Gone with the Wind --ok Good, the Bad, and the Ugly --Problem Last Star Fighter --Problem
They Should read...
Gone with the Wind Good, the Bad, and the Ugly, The Last Star Fighter, The
My Answer:
If that is the case, you should get and strip the file extension before you replace the name and add it back to the filename after replacement. Here is the snippet:
' get the file extension sExtension = fso.GetExtensionName(fil.Name) ' strip the extension from file name sName = Replace(fil.Name, "." & sExtension, "") ' replace the whatever and put the extension back sName = Replace(sName, "The ", "") & ", The" & "." & sExtension
and your new code should look like this:
'========================================================
' VBScript to replace underscore in file name with space
' for each files in a folder
' Written by ApOgEE of http://coderstalk.blogspot.com
'========================================================
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder(".")
' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "The ") <> 0 Then
' replace underscore with space
' sName = Replace(fil.Name, "The ", "") & ", The"
' get the file extension
sExtension = fso.GetExtensionName(fil.Name)
' strip the extension from file name
sName = Replace(fil.Name, "." & sExtension, "")
' replace the whatever and put the extension back
sName = Replace(sName, "The ", "") & ", The" & "." & sExtension
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"
Good luck!
Comments