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

VBscript to remove spaces from filenames?

694 views
Skip to first unread message

Warren Bell

unread,
Sep 3, 2003, 11:09:45 AM9/3/03
to
I'm running WinXP pro and need a script that will take all the files
in a directory and remove the black spaces in the filename. There
isn't a lot of variation in the filename so the script doesn't have
to do a lot of checking. All the files will look like this:

Picture 001.jpg
Picture 002.jpg
etc..

And I want them to end up like this:

Picture001.jpg
Picture002.jpg
etc..

All I need is a script I can run from the command line and use a
directory as the argument and it will rename all the files in the
directory. Somthing like this:

c:\windows\rename.vbs c:\my documents\my pictures\2003-9-3


Can anyone help me out with this?

TIA

Steve Fulton

unread,
Sep 3, 2003, 11:37:45 AM9/3/03
to

Mythran

unread,
Sep 3, 2003, 11:45:22 AM9/3/03
to

"Warren Bell" <wjb...@belletc.net> wrote in message
news:eh2l7zi...@tk2msftngp13.phx.gbl...

This will take me a bit, so give me a few :)

Mythran


Jason Melville

unread,
Sep 3, 2003, 11:45:30 AM9/3/03
to
Hi Warren

Save the text below to a vbs file:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''

Option Explicit

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

Dim FilesRenamed: FilesRenamed = 0

Main

Sub Main
Dim FolderPath
FolderPath = WScript.Arguments(0)

Dim CurrentFolder
Set CurrentFolder = fso.GetFolder(FolderPath)
ProcessFolder CurrentFolder
StdOut.WriteLine FilesRenamed & " Files renamed."
End Sub

Sub ProcessFolder (ByVal Folder)
Dim Files: Set Files = Folder.Files
Dim File
For Each File In Files
If InStr(1,File.Name," ") > 0 Then
File.Move Replace(File.Path," ","")
FilesRenamed = FilesRenamed + 1
End If
Next
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''

Useage:

cscript <filename>.vbs <folderpath>
eg. cscript rename.vbs c:\myfolder
Seems cool on 2000 so should be OK for XP.

Jason


"Mythran" <kip_p...@hotmail.com> wrote in message
news:eAYMmJjc...@TK2MSFTNGP12.phx.gbl...

Warren Bell

unread,
Sep 3, 2003, 7:50:33 PM9/3/03
to
First, thanks guys!

I tried them both but am using this one for now. One problem, it
has a problem with the path. I think it has a problem with paths
with spaces (c:\documents and settings\...)

when I move the folder to c:\2003-09-02 and use it that way it works
perfect.

Is there a way you can put the main path in the script so it
automatically know to look in:

C:\Documents and Settings\Warren Bell\My Documents\My Pictures

and then I can just specify the directory form there?

So instead of:

cscript rename.vbs C:\Documents and Settings\Warren Bell\My
Documents\My Pictures\2003-09-02

for the command line, I would use:

cscript rename.vbs 2003-09-02

Thanks again!

Jason Melville

unread,
Sep 3, 2003, 9:13:43 PM9/3/03
to
Hi Warren

If you want to put the path in the script just add the path (minus the final
folder if you are going to pass it) in the FolderPath variable i.e :

To rename files in C:\Documents and Settings\Warren Bell\My Documents\My
Pictures\2003-09-02 the first two lines in sub main become

Dim FolderPath
FolderPath = "C:\Docume~1\Warren~1\MyDocu~1\MyPict~1\" &
WScript.Arguments(0)

which would be run using:
cscript rename.vbs 2003-09-02

The ~1 is just the dos naming format for folder names with spaces (shorten
any name longer than eight letters to six letters while removing spaces and
adding the tilde/number).

Good luck

Jason

"Warren Bell" <wjb...@belletc.net> wrote in message

news:eDDB8Wnc...@TK2MSFTNGP10.phx.gbl...

Warren Bell

unread,
Sep 3, 2003, 10:58:01 PM9/3/03
to
Jason Melville wrote:
[cut]

Thanks, works perfect.

Warren Bell

unread,
Sep 4, 2003, 10:14:55 AM9/4/03
to
Hi Jason,

One other thing, if you have time...

It would be nice if I could double click on the vbs file and have it
pop up a form field that I can enter in the argument, like
2003-09-02. Then hit OK and it would preform the action.

If you have time I would appreciate it, if not you've helped me out
hugely already, thanks.

Jason Melville

unread,
Sep 4, 2003, 10:33:00 AM9/4/03
to
No probs.

Full source again, just incase.....
Just dbl click and you will get an inputform for the folder name arg.
By the way - just check the FolderPath variable - I think this was it.

Jason

----------------------------------------------------------------------------
-

Option Explicit

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

Dim FilesRenamed: FilesRenamed = 0

Main

Sub Main
Dim FolderPath
Dim FolderName
FolderName = InputBox("Enter Folder Name")

FolderPath = "C:\Docume~1\Warren~1\MyDocu~1\MyPict~1\" & FolderName

Dim CurrentFolder
Set CurrentFolder = fso.GetFolder(FolderPath)
ProcessFolder CurrentFolder

msgbox FilesRenamed & " Files renamed."
End Sub

Sub ProcessFolder (ByVal Folder)
Dim Files: Set Files = Folder.Files
Dim File
For Each File In Files
If InStr(1,File.Name," ") > 0 Then
File.Move Replace(File.Path," ","")
FilesRenamed = FilesRenamed + 1
End If
Next
End Sub

--------------------------------------------------------


"Warren Bell" <wjb...@belletc.net> wrote in message

news:OKDF85uc...@TK2MSFTNGP09.phx.gbl...

Warren Bell

unread,
Sep 4, 2003, 11:01:07 AM9/4/03
to
Awsome, Thank you!

TDM

unread,
Sep 4, 2003, 11:09:28 AM9/4/03
to

Warren,

Did you try putting the path in quotes ?

cscript rename.vbs "C:\my path with spaces"

TDM


"Warren Bell" <wjb...@belletc.net> wrote in message

news:eDDB8Wnc...@TK2MSFTNGP10.phx.gbl...

0 new messages