Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change file extensions with VBScript

4,261 views
Skip to first unread message

SANTANDER

unread,
Oct 5, 2007, 6:35:41 PM10/5/07
to
Does anyone have a sample VBS script that will change file extensions for
some file types in a current folder?
I.e. i have many files which have a particular extension that needs to be
changed. For example, I want to change extension from .zip to .jpg for all
zip files located in current folder. Script looks through all the ZIP files
found in a particular folder, and searches for extension patterns in each
file. If the pattern is found, it is replaced by the extension that was
provided. The Confirm function echoes a message and waits for the standard
input to return. The user must enter Y to continue.

Thanks.
S.

jmi...@hotmail.com

unread,
Oct 5, 2007, 10:16:28 PM10/5/07
to

SANTANDER

unread,
Oct 6, 2007, 10:48:01 AM10/6/07
to

<jmi...@hotmail.com> wrote in message
news:1191636988.1...@k79g2000hse.googlegroups.com...-------
I tried it, not work for me. And it have no Confirm function which echoes a
message.

Santander

jmi...@hotmail.com

unread,
Oct 7, 2007, 3:31:18 PM10/7/07
to
Did you first modify the script? I just tried it and it worked
perfectly.

Here's a modified version, I entered the zip and jpg extension names
for you. (See - (strExtension = Replace(strExtension, "zip", "jpg"))

You just need to change the path variable (line 2) to point to the
folder you want to make the changes in. I also added a confirmation if
that's what you were talking about.

Jeff

strComputer = "."
Path = "C:\testfolder"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='"& Path &"'} Where " _
& "ResultClass = CIM_DataFile")

Msgbx = MsgBox ("You're about to rename all the files with zip
extensions to jpg in" & vbCrLf & _
"" & Path & vbCrLf & _
"" & vbCrLf & _
"Do you wish to continue?", 4 + 48, "Rename File Extensions")

If Msgbx = 6 Then

For Each objFile In colFiles
strExtension = objFile.Extension
strExtension = Replace(strExtension, "zip", "jpg")
strNewName = objFile.Drive & objFile.Path & objFile.FileName & "."
& strExtension
errResult = objFile.Rename(strNewName)
Next

WScript.Echo "Finished!"

Else
WScript.quit()
End If

SANTANDER

unread,
Oct 7, 2007, 6:50:17 PM10/7/07
to

<jmi...@hotmail.com> wrote in message
news:1191785478....@o80g2000hse.googlegroups.com...

> Jeff

WScript.Echo "Finished!"

Else
WScript.quit()
End If
----------------
Hi,
I tried, script twice showed error:

Line: 4
Char: 67
Error: Unterminated string constant
Code: 800A0409

this line originally was breaked in word \root\cimv2" so I deleted space
and it to make it one line.
Second error was same in another place where is message box long text.
Now it show another error: Line: 21; Char:1; Error: Expected Statement;
Code: 800A0400
it is text "& strExtension"

thanks.

jmi...@hotmail.com

unread,
Oct 7, 2007, 7:39:39 PM10/7/07
to
Yep, you're right. It looks like when I post it's breaking some of the
lines. Just condense each of the following broken lines into single
lines and it should work for you.

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")

Msgbx = MsgBox ("You're about to rename all the files with zip


extensions to jpg in" & vbCrLf & _

biskipina

unread,
Oct 8, 2007, 4:22:48 AM10/8/07
to
Hi if it is not a big problem to you can you help me to modify this
script

I need to on double click on file named file.bak name and extension of
file change to
fila.bak.dwg
i try to change this script myself, but didnt suceed
thanks Igor

jmi...@hotmail.com

unread,
Oct 8, 2007, 10:44:03 AM10/8/07
to
Try this. If you try to change the extensions back to .bak
from .bak.dwg, however, the script will not work because the .bak is
now part of the filename.


strComputer = "."
Path = "C:\testfolder"

StartExt = "bak"
EndExt = "bak.dwg"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & _
"\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='"& Path &"'} Where " _

& "ResultClass = CIM_DataFile")


Msgbx = MsgBox ("You're about to rename all the files with " _
& StartExt & " extensions to " & EndExt & " in" & vbCrLf & _


"" & Path & vbCrLf & _
"" & vbCrLf & _
"Do you wish to continue?", 4 + 48, "Rename File Extensions")

If Msgbx = 6 Then

For Each objFile In colFiles
strExtension = objFile.Extension

strExtension = Replace(strExtension, StartExt, EndExt)
strNewName = objFile.Drive & objFile.Path & objFile.FileName & _

biskipina

unread,
Oct 8, 2007, 3:27:40 PM10/8/07
to
thanks for quick replying
but still doesn't work
when i doubleclick on file.bak
it popups error like ..... file.bak is not vald win32 application

:) sorry if i bother you

i am not a programmer but if I can get path and name of file i
doubleclick
a can with

strNewName = objFile.Drive & objFile.Path & objFile.FileName &

objFile.Extension & ".dwg"

change it...
hmmmmm

SANTANDER

unread,
Oct 8, 2007, 4:51:56 PM10/8/07
to

<jmi...@hotmail.com> wrote in message
news:1191800379....@r29g2000hsg.googlegroups.com...

----------

Still have problem: show error:
Line:4, Char:1, Error: "File name or class name not found during automation
operation 'GetObject'

Line 4 is


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Thanks.

jmi...@hotmail.com

unread,
Oct 8, 2007, 6:49:31 PM10/8/07
to
I e-mailed you a copy.

Jeff

jmi...@hotmail.com

unread,
Oct 8, 2007, 7:03:10 PM10/8/07
to
This script doesn't allow you to double click on the original file to
change the extension. (I don't think that's possible) Instead, you
have to modify the first 4 lines and then double click on the script
file to launch it.

The script will then change any files with extension types you specify
within the folder you specify.

Jeff

biskipina

unread,
Oct 9, 2007, 2:10:32 AM10/9/07
to
Thanks Jeff
i'll keep on trying

Igor

SANTANDER

unread,
Oct 11, 2007, 12:41:17 PM10/11/07
to
Just tested extension renamer on win XP - works fine, but not work on win
98se.

(Show error: Line:4, Char:1, Error: "File name or class name not found

during automation operation 'GetObject'
Line 4 is
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

thanks,
Santander

jmi...@hotmail.com

unread,
Oct 11, 2007, 10:33:26 PM10/11/07
to
I never deal with Win98 so I'm actually not sure how to troubleshoot.
Maybe you could try upgrading the version of WSH running on the 98
machine, or see if there's a comparable class that will work with
Win98.

SANTANDER

unread,
Oct 12, 2007, 5:53:06 AM10/12/07
to

<jmi...@hotmail.com> wrote in message
news:1192156406.0...@k35g2000prh.googlegroups.com...
--

so, probably WinXP WSH have a class, that missing in Win98?

S.

Paul Randall

unread,
Oct 12, 2007, 11:58:11 AM10/12/07
to

"SANTANDER" <sant...@microsoft.news> wrote in message
news:ehN6yWLD...@TK2MSFTNGP06.phx.gbl...

Look at the download details for WMI for W98:
File Name: wmi9x.exe
Version: 1.5
Date Published: 2/11/2000

I think it does NOT have a lot of stuff that was built into the newer WMIs
for newer OSs.

-Paul Randall


SANTANDER

unread,
Oct 12, 2007, 2:21:05 PM10/12/07
to

"Paul Randall" <paul...@cableone.net> wrote in message
news:eyNrkiOD...@TK2MSFTNGP04.phx.gbl...

>
> "SANTANDER" <sant...@microsoft.news> wrote in message
> news:ehN6yWLD...@TK2MSFTNGP06.phx.gbl...
>>
>> <jmi...@hotmail.com> wrote in message
>> news:1192156406.0...@k35g2000prh.googlegroups.com...
>>>I never deal with Win98 so I'm actually not sure how to troubleshoot.
>>> Maybe you could try upgrading the version of WSH running on the 98
>>> machine, or see if there's a comparable class that will work with
>>> Win98.
------

>> so, probably WinXP WSH have a class, that missing in Win98?
>
> Look at the download details for WMI for W98:
> File Name: wmi9x.exe
> Version: 1.5
> Date Published: 2/11/2000
>
> I think it does NOT have a lot of stuff that was built into the newer WMIs
> for newer OSs.
>
> -Paul Randall

---------------
yes, that's right. Perhaps there is universal way that works on both
systems, though.

Thanks.

Landon

unread,
Nov 15, 2007, 2:08:00 AM11/15/07
to
Hi,

I am in need of a similar script, but all the files currently have no
extension. I just need to add ".pdf" to about 900 files in the same folder.
What would it take to modify this script to work for that?

Thanks!

Tom Lavedas

unread,
Nov 15, 2007, 1:51:49 PM11/15/07
to

Is this a one-shot deal? If so, try opening a command console an
navigating tothe folder. Then issue the following statement at the
prompt:

ren *. *.pdf

Or, this can be done under script control ...

sPathspec = "D:\your\target\folder"
with createobject("wscript.shell")
.currentdirectory = sPathspec
.run "%comspec% /c ren *. *.pdf", 0, true
end with

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Landon

unread,
Nov 15, 2007, 2:02:05 PM11/15/07
to

"Tom Lavedas" wrote:

Oh wow, that was easy! The "ren" command worked great. I was trying to
make it much more complicated than it should have been...

Thanks!!

Tom Lavedas

unread,
Nov 15, 2007, 2:14:53 PM11/15/07
to

Yes, the command console is actually still useful for some things.

Just an aside: Have you ever wanted a listing of the contents of a
certain folder to add to a document. Well, the GUI cannot provide an
electronically alterable listing of a folders contents (only a
picture), while it is a piece of cake to get one at the command
console ...

dir /o /a-d *.* > listing.txt

Tom

0 new messages