For website designers, it is inevitable to process large batches of files frequently, especially pictures and some text files, which are even more frequent. Due to the large number of files on the website, the naming of files of the same type often directly uses incremental numbers with certain regularity as file names. For example, the naming of common image files often uses 1001.jpg and 1002.jpg. This advantage is that the file names will not be duplicated and are easy to manage. Here, we specifically introduce a simple and easy way to use ASP to simply use batch renames to rename all files. Of course, the file names after rename are incremented according to the needs of the website designer.
We use ASP to implement the above functions. It should be noted that because designing file operations and using FileSystemObject objects, the implementation of this function must be carried out on a website with file operation permissions. Generally, virtual attention is that considering the security requirements, the file may not be given permissions, which is what we need to pay attention to first; in the following program, we will operate all files in the specified folder strFromDir. As long as it is a file in this folder, no matter what the file type is, the program will rename it. Of course, the file type will not be changed. The files after the name will not be saved in the original folder, but will be moved to the new folder strTargetDir. Note that we are moving here, not copying, so after the operation, all files in the original folder will not exist; the program makes good use of the various attributes and features provided by the FileSystemObject object, and the implementation is simple and clear, and friends who use other languages may feel it deeply; now, let's look at the function implementation code:
<% @LANGUAGE = VBSCRIPT %>
<%Option Explicit%>
<%
'The following program batch renames the file names in the folder and moves all files to a new folder;
Response.Write "<html>" & VbCrLf & "<head>" & VbCrLf
Response.Write "<title>Batch file name change</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 the destination folder
strFromDir = "D:test/"
strTargetDir = "D:/test1/"
' Set the number of processing files to 0
strRealCount = 0
Set objFS = Server.CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFS.GetFolder(strTargetDir)
'The specific settings of the file name, set to 100001 here, indicating that the file name will be from 100001
'Start, increment gradually, can be set as needed;
strFileNameCount = 100001
For each objFile in objRootFolder.Files
'For specific files, they are not processed 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 change it to: " &strTargetDir& strNewFileName & "<br>" & vbCrLF
strFileNameCount = strFileNameCount + 1
strRealCount = strRealCount + 1
Next
Response.Write "<p><b> Total processing: " & (strRealCount) & " files </B>" & vbCrLf
Set objRootFolder = Nothing
Set objFS = Nothing
gbolGoProcedure = True
End If
If gbolGoProcedure Then
Response.Write("<p><b>Batch file batch move 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 the file </b></center>") & VbCrLf
End If
Response.Write "</body>" & VbCrLf & "</html>"
%>
The above is all the content of ASP simply implementing batch name renames of all files. More exciting content is in the Miaoxin Technology Channel.