programming-examples/asp/FilesMaths/Recurse and Rename.asp

139 lines
4.0 KiB
Plaintext
Raw Permalink Normal View History

2019-11-18 14:25:58 +01:00
<%@ Language=VBScript %>
<%
Option Explicit
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: emp" '*** 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")
%>
<HTML>
<HEAD>
<title>Document</title>
<style>
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;
}
</style>
</HEAD>
<BODY>
<table width="60%" border="1" align="center" cellspacing="0" cellpadding="2">
<tr>
<td Class="TableTitle"><b>X <a href="http://www.X.com"></a></b><br>
<i>Start Directory: <%=sStartDirectory %></i></td>
</tr>
<%
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)
%>
<tr>
<td Class="TableTitle">Total Files Renamed: <%=nTotalFilesRenamed %></i></td>
</tr>
</table>
</BODY>
</HTML>
<%
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 & "" & fSubFolder.name & "</td></tr>" & vbCrLf
'Rename all files in this directory where filename contains blanks
RenameFilesStripBlanks(sPath & "" & fSubFolder.name)
'*** Call self to recurse down folders
Call RecurseFolder(sPath & "" & fSubFolder.name, fso)
Next
End Sub
Dim nFilesRenamed, drive, folder, filelist, file, sNewFile
nFilesRenamed=0
Set drive = Server.CreateObject("Scripting.FileSystemObject")
Set folder = drive.GetFolder(sPath)
Set filelist = folder.files
For Each file In filelist
sNewFile=Replace(file.name," ","")
If (sNewFile<>file.name And bEnabled=True) Then
Response.Write "<tr><td class=""Rename"">" & "Rename:" & file.name & " 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
file.name = sNewFile
Else
Response.Write "<tr><td class=""Rename"">" & "Renamed File Exists:" & file.name & " to " & sNewFile & "</td></tr>"
End If
'Increment the files renamed counter
nFilesRenamed=nFilesRenamed + 1
End If
Next
'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
%>