For website designers, it is inevitable to process large quantities of files from time to time, especially pictures and some text files, which are processed frequently. Due to the large number of files on the website, files of the same type are often named using increasing numbers with certain rules. For example, our common picture files are often named 1001.jpg, 1002. The advantage of jpg method is that the file names will not be repeated and it is easy to manage. Here, we specifically introduce a simple and easy method to batch rename all files in any folder. Of course, the file names after renaming are incremented numerically according to the needs of the website designer.
We use ASP to implement the above functions. It should be noted that because the design involves file operations and uses the FileSystemObject object, the implementation of this function must be carried out on a website with file operation permissions. General virtual attention: considering security requirements, file existence permissions may not be given. This is the first thing we need to pay attention to; in addition, in the following program, we will operate all files in the specified folder strFromDir, as long as it is this file Regardless of the file type, the program will rename the files in the folder. Of course, the file type will not be changed. The renamed files will no longer be saved in the original folder, but will be moved to a new folder. strTargetDir, please note that we are moving here, not copying, so after the operation, all files in the original folder will no longer exist; the program makes good use of various properties and features provided by the FileSystemObject object, and is simple to implement. Obviously, friends who program in other languages may feel deeply; now, let’s look at the function implementation code:
<% @LANGUAGE = VBSCRIPT %>
<%Option Explicit%>
<%
'The following program renames files in a folder in batches and moves all files to a new folder;
Response.Write "<html>" & VbCrLf & "<head>" & VbCrLf
Response.Write "<title>Batch file rename</title>" & VbCrLf
Response.Write "</head>" & VbCrLf & "<body>" & VbCrLf
'Variable description
Dim gbolGoProcedure
Dim strFromDir 'Source folder
Dim strTargetDir 'Target folder
Dim objFS
Dim objRootFolder
Dim objFile
Dim strFileNameLen
Dim strPrevFileName
Dim strFileExt 'File extension
Dim strFileNameCount
Dim strNewFileName
Dim strRealCount 'Number of files processed
gbolGoProcedure = False
'If the start button is clicked, perform the following processing
If (Request.Form("GoButton")) = "Start" then
'Specify the source folder and target folder
strFromDir = "D:test"
strTargetDir = "D:test1"
' Set the number of files to be processed to 0
strRealCount = 0
Set objFS = Server.CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFS.GetFolder(strTargetDir)
'The specific setting of the file name, set here to 100001, indicates that the file name will be changed from 100001
'Start, gradually increase, and can be set as needed;
strFileNameCount = 100001
For each objFile in objRootFolder.Files
'For specific files, no processing will be performed and can be set as needed;
If objFile.Name = "Thumbs.db" then strFileNameCount = StrFileNameCount - 1
strFileNameCount = strFileNameCount + 1
Next
Set objRootFolder = objFS.GetFolder(strFromDir)
For each objFile in objRootFolder.Files
strFileNameLen = Len (objFile.Name)
If Mid (objFile.Name,(strFileNameLen - 3),1) = "." then
strFileExt = right(objFile.Name, 4)
Else
strFileExt = right(objFile.Name, 5)
End If
strPrevFileName = objFile.Name
strNewFileName = strFileNameCount & strFileExt
objFile.Move strTargetDir & strNewFileName
Response.Write "Source file: " &strFromDir&strPrevFileName & " > Move and rename: " &strTargetDir& strNewFileName & "<br>" & vbCrLF
strFileNameCount = strFileNameCount + 1
strRealCount = strRealCount + 1
Next
Response.Write "<p><b> Processed in total: " & (strRealCount) & " files</B>" & vbCrLf
Set objRootFolder = Nothing
Set objFS = Nothing
gbolGoProcedure = True
End If
If gbolGoProcedure Then
Response.Write("<p><b>Batch file movement and rename</b>") & vbCrLf
Else
Response.Write("<center><br><form method=""post"" action=""FileNameConverter.asp"" ID=form1 name=""form1"">") & vbCrLf
Response.Write("<input type=""SUBMIT"" value="" Start"" ID=""GoButton"" name=""GoButton"">") & vbCrLf
Response.Write("</form>") & vbCrLf
Response.Write("<p><b>Click the button to batch move and rename files</b></center>") & VbCrLf
End If
Response.Write "</body>" & VbCrLf & "</html>"
%>