'-------------------------------------------------------------------
'-- VbScript File Rename sample - Copyright © 2006, Dave Moats
'--
'-- This sample is provided 'as-is', without any express or implied warranty.
'-- In no event will the authors be held liable for any damages arising from
'-- the use of this sample code.
'--
'-- Permission is granted to anyone to use this sample code for any purpose,
'-- including commercial applications, subject to the following restrictions:
'--
'-- The origin of this code must not be misrepresented;
'--       you must not claim that you wrote the original code.
'--       If you use this code, an acknowledgment in the
'--       documentation is requested - shown below:
'--
'-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
'--
'-------------------------------------------------------------------
'================================================
'==  A simple example of how to rename the files in
'==  a given directory. This example uses the
'==  FileSystemObject to find and manipulate the
'==  files.
'================================================

'================================================
'==  Declare our variables
'================================================
dim fso
dim args
dim folderName
dim ourDir
dim ourFile
dim tmpFile

'================================================
'==  Create an instance of the file system object
'================================================
set fso = createobject ( "scripting.filesystemobject" )

'================================================
'== Get the command line arguments
'================================================
set args = wscript.arguments
num = args.count
if num = 0 then
     wscript.echo "Usage: cscript renaming.files.vbs <foldername>"
     wscript.quit 1
else
     folderName = args.item (0)
end if

'================================================
'==  Open the target directory
'================================================
set ourDir = fso.getfolder( folderName )

'================================================
'==  Now loop through all the files in this directory
'================================================
for each ourFile in ourDir.files

     '=============================================
     '==  Get a copy of the file name
     '=============================================
     tmpFile = ourFile.name

     '=============================================
     '==  Here we are replacing any spaces & dashes
     '==  with underscores
     '=============================================
     tmpFile = replace ( tmpFile, " " , "_" )
     tmpFile = replace ( tmpFile, "-" , "_" )

     '=============================================
     '==  Now we are updating the original file name
     '=============================================
     ourFile.name = tmpFile

next

'================================================
'==  Cleanup the reference to the directory
'================================================
set ourDir = nothing

'================================================
'==  Cleanup our reference to the file system object
'================================================
set fso = nothing

wscript.quit 0






Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.