Recurse and Rename

Recurses down a directory tree and renames files

My Own Work
<%@ Language=VBScript %>
Option Explicit
'Script: RecurseAndRename.asp
'Version: 1.1
'Function: Recurses down a directory tree and renames
'   any files in the directories that have blanks in their filename.
'   ###
'   ###
'Author: Jeremy Nevill
', October 2000
'Website: - ASP/SQL/XML Development, Scripts and Tools
'Copyright: Copyright © 2000 Lateral Information Systems Ltd, All Rights Reserved
'License: RecurseAndRename.asp may be used and modified free of charge by anyone
'   so long as this copyright notice and the comments above remain intact.
'   By using this code you agree to indemnify Lateral Information Systems Ltd
'   from any liability that might arise from it's use.
'   Selling the code for this program without prior written consent is
'   expressly forbidden.
'   Obtain permission before redistributing this software over the Internet or
'   in any other medium. In all cases copyright and header must remain intact
'Last Upd: 10/01/01 Added safety bEnabled variable, also made script rename in start dir.
' INSTRUCTIONS - 10/01/01
' =======================
' This script renames files, as a couple of people have run it and had problems
' I have modified the script to require the user to modify the bEnabled
' variable to true for renaming to actually take place. (Go to Line 42 to enable this script)

Dim bEnabled
Dim fso     'as file system object
Dim nTotalFilesRenamed 'as integer

bEnabled=false    '**** Set to false the script will not rename files, change this to true for renaming to take place.
server.ScriptTimeout=120 'Set the page timeout to 2 mins
Const sStartDirectory="c:\temp"  '*** This is the starting directory
         'Be carefull where you start as this
         'script renames any file under this dir
         'that contain blanks. ***

'Setup the file system object
Set fso = server.CreateObject("Scripting.FileSystemObject")
<title>Recurse and Rename (</title>
 Body {
 background-color: #FFFFFF;
 color: #000000;
 font-family: Verdana, Arial, Helvetica, sans-serif, "MS sans serif";

 TD {
 background-color: #FFFFFF;
 color: #000000;
 font-family: Verdana, Arial, Helvetica, sans-serif, "MS sans serif";
 font-size: 70%

 .TableTitle {
 background-color: #FFFFCE;

 .Summary {
 background-color: #FFCE9C;

 .Rename {
 background-color: #FFFF9C;
<table width="60%" border="1" align="center" cellspacing="0" cellpadding="2">
 <td class="TableTitle"><b>Recurse and Rename - Script from <a href=""></a></b>

 <i>Start Directory: <%=sStartDirectory %></i></td>
 if bEnabled=false then
  Response.Write "<tr><td class=""TableTitle""><b>This script is disabled, please read the instructions (only a couple of lines) to enable it!</b></td></tr>" & vbcrlf 
 end if

 'Start with current folder
 Response.write "<tr><td>" & sStartDirectory & "</td></tr>" & vbcrlf
 call RenameFilesStripBlanks(sStartDirectory)

 'Call the RecurseFolder function to recurse down from the start directory.
 Call RecurseFolder(sStartDirectory, fso)
 <td class="TableTitle">Total Files Renamed: <%=nTotalFilesRenamed %></i></td>

Sub RecurseFolder(sPath, fso)
'Page:  RecurseFolder(sPath, fso)
'Function: Recurses down folders from sPath calling the
'   function to rename files with blanks
Dim fFolder, fSubFolders, fSubFolder

Set fFolder = fso.GetFolder(sPath)
Set fSubFolders = fFolder.SubFolders

'Now recurse for each subfolder in the sPath folder...
For Each fSubFolder In fSubFolders

 Response.write "<tr><td>" & sPath & "\" & & "</td></tr>" & vbcrlf

 'Rename all files in this directory where filename contains blanks
 RenameFilesStripBlanks(sPath & "\" &

 '*** Call self to recurse down folders
 Call RecurseFolder(sPath & "\" &, fso)


End Sub

Sub RenameFilesStripBlanks(sPath)
'Page:  RenameFilesStripBlanks(sPath)
'Function: Renames all files in sPath directory where
'   file contains a blank space - remove this space
Dim nFilesRenamed, drive, folder, filelist, file, sNewFile


Set drive = server.CreateObject("Scripting.FileSystemObject")
Set folder = drive.GetFolder(sPath)
Set filelist = folder.files

For Each file In filelist
 sNewFile=Replace(," ","")
 If (sNewFile<> and bEnabled=true) Then
  Response.write "<tr><td class=""Rename"">" & "Rename:" & & " to " & sNewFile & "</td></tr>"
  'If the new filename does not exist then change file, else flag to user
  if not(fso.FileExists(sPath & "\" & sNewFile)) then
   'Rename the file = sNewFile   
   Response.write "<tr><td class=""Rename"">" & "Renamed File Exists:" & & " to " & sNewFile & "</td></tr>"

  end if
  'Increment the files renamed counter
  nFilesRenamed=nFilesRenamed + 1

 End If


'Inform user of number of files renamed in current directory
if nFilesRenamed<>0 then
 Response.write "<tr><td class=""Summary"">" & nFilesRenamed & " Renamed" & "</td></tr>" & vbcrlf
end if

'Increment the total files renamed counter
nTotalFilesRenamed=nTotalFilesRenamed + nFilesRenamed

End Sub

Views 3059 Downloads 1097

Classic ASP File System