'====================================================================
'= Enumerate and Rename Files VBScript sample - Copyright © 2008, 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 © 2008, Dave Moats (http://www.davemoats.com/).
'=
'====================================================================
'====================================================================
'== fileRename.vbs - script to enumerate all the folders and
'== files in a specified folder and then rename
'== all the files to include the folder name
'== this is helpful for a folder structure that
'== contains a large number of files that are
'== still the default name set by whatever
'====================================================================
option explicit
'====================================================================
'== windows script host does not have an easy way to check for
'== syntax errors, the only way to do this is to run the entire
'== script, to work around this issue, uncomment the wscript.quit
'== command below then run the script, this will get the wsh
'== environment to check the script for syntax errors and the
'== script will exit when it hits the wscript.quit command, so
'== the entire will not be exceuted
'====================================================================
'wscript.quit
dim startFolder, fso, objFolder
if wscript.arguments.count = 0 then
wscript.echo ( "Usage: fileRename.vbs <path to folder to process>")
wscript.quit ( )
elseif wscript.arguments.item ( 0) = "/?" Or wscript.arguments.item ( 0) = "-?" then
wscript.echo ( "Usage: fileRename.vbs <path to folder to process>")
wscript.quit ( )
else
startFolder = wscript.arguments.item ( 0)
end if
set fso = createobject ( "Scripting.FileSystemObject")
set objFolder = fso.getfolder ( startFolder)
listFolders ( objFolder )
set objFolder = nothing
set fso = nothing
wscript.quit
'====================================================================
'== sub listFolders - enumerates all the folders under the target
'== folder
'====================================================================
sub listFolders ( inFolder )
dim subFolders
'==================================================================
'== ignore any folder does not exist errors
'==================================================================
on error resume next
set subFolders = inFolder.subfolders
for each folder in subFolders
set objSubFolder = fso.getfolder ( folder.path )
wscript.echo " Folder: " & objSubFolder.path
listFolders ( objSubFolder )
renameFiles ( objSubFolder )
set objSubFolder = nothing
next
end sub
'====================================================================
'== sub renameFiles - enumerate and rename all the files located
'== in the target folder
'====================================================================
sub renameFiles ( inFolder )
dim objFile, file
set objFiles = inFolder.files
for each file in objFiles
if instr ( 1, file.name, ".LOG" ) <> 0 or instr ( 1, file.name, ".log" ) <> 0 then
wscript.echo "existing file name: " & file.name
wscript.echo "new file name: " & inFolder.name & file.name
file.name = inFolder.name & file.name
end if
next
end sub
'= Enumerate and Rename Files VBScript sample - Copyright © 2008, 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 © 2008, Dave Moats (http://www.davemoats.com/).
'=
'====================================================================
'====================================================================
'== fileRename.vbs - script to enumerate all the folders and
'== files in a specified folder and then rename
'== all the files to include the folder name
'== this is helpful for a folder structure that
'== contains a large number of files that are
'== still the default name set by whatever
'====================================================================
option explicit
'====================================================================
'== windows script host does not have an easy way to check for
'== syntax errors, the only way to do this is to run the entire
'== script, to work around this issue, uncomment the wscript.quit
'== command below then run the script, this will get the wsh
'== environment to check the script for syntax errors and the
'== script will exit when it hits the wscript.quit command, so
'== the entire will not be exceuted
'====================================================================
'wscript.quit
dim startFolder, fso, objFolder
if wscript.arguments.count = 0 then
wscript.echo ( "Usage: fileRename.vbs <path to folder to process>")
wscript.quit ( )
elseif wscript.arguments.item ( 0) = "/?" Or wscript.arguments.item ( 0) = "-?" then
wscript.echo ( "Usage: fileRename.vbs <path to folder to process>")
wscript.quit ( )
else
startFolder = wscript.arguments.item ( 0)
end if
set fso = createobject ( "Scripting.FileSystemObject")
set objFolder = fso.getfolder ( startFolder)
listFolders ( objFolder )
set objFolder = nothing
set fso = nothing
wscript.quit
'====================================================================
'== sub listFolders - enumerates all the folders under the target
'== folder
'====================================================================
sub listFolders ( inFolder )
dim subFolders
'==================================================================
'== ignore any folder does not exist errors
'==================================================================
on error resume next
set subFolders = inFolder.subfolders
for each folder in subFolders
set objSubFolder = fso.getfolder ( folder.path )
wscript.echo " Folder: " & objSubFolder.path
listFolders ( objSubFolder )
renameFiles ( objSubFolder )
set objSubFolder = nothing
next
end sub
'====================================================================
'== sub renameFiles - enumerate and rename all the files located
'== in the target folder
'====================================================================
sub renameFiles ( inFolder )
dim objFile, file
set objFiles = inFolder.files
for each file in objFiles
if instr ( 1, file.name, ".LOG" ) <> 0 or instr ( 1, file.name, ".log" ) <> 0 then
wscript.echo "existing file name: " & file.name
wscript.echo "new file name: " & inFolder.name & file.name
file.name = inFolder.name & file.name
end if
next
end sub
Copyright © 2010 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.