For website designers, it is inevitable to process large quantities 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 is often used in this way. The advantage of this is that the file names will not be duplicated and are easy to manage. Here, we specifically introduce a simple and easy way to batch rename all files in any folder. 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;
"<html>" & VbCrLf & "<head>" & VbCrLf
"<title>Batch file name change</title>" & VbCrLf
"</head>" & VbCrLf & "<body>" & VbCrLf
' Variable description
Dim gbolGoProcedure
Dim strFromDir'Source folder
Dim strTargetDir'Dir's Destination 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 (("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 = ("")
Set objRootFolder = (strTargetDir)
'The specific settings of the file name, set to 100001 here, indicating that the file name will be from 100001
'Start, increment gradually, and can be set as needed;
strFileNameCount = 100001
For each objFile in
'For specific files, they are not processed and can be set as needed;
If = "" then strFileNameCount = StrFileNameCount - 1
strFileNameCount = strFileNameCount + 1
Next
Set objRootFolder = (strFromDir)
For each objFile in
strFileNameLen = Len ()
If Mid (,(strFileNameLen - 3),1) = "." then
strFileExt = right(, 4)
Else
strFileExt = right(, 5)
End If
strPrevFileName =
strNewFileName = strFileNameCount & strFileExt
strTargetDir & strNewFileName
"Source File: " &strFromDir&strPrevFileName & " > Move and change it to: " &strTargetDir& strNewFileName & "<br>" & vbCrLF
strFileNameCount = strFileNameCount + 1
strRealCount = strRealCount + 1
Next
"<p><b> Total processing: " & (strRealCount) & " files </B>" & vbCrLf
Set objRootFolder = Nothing
Set objFS = Nothing
gbolGoProcedure = True
End If
If gbolGoProcedure Then
("<p><b>Batch file batch movement and name change </b>") & vbCrLf
Else
("<center><br><form method=""post"" action="""" ID=form1 name=""form1"">") & vbCrLf
("<input type=""SUBMIT"" value="" Start "" ID=""GoButton"" name=""GoButton""") & vbCrLf
("</form>") & vbCrLf
("<p><b>Click the button to batch move and rename the file </b></center>") & VbCrLf
End If
"</body>" & VbCrLf & "</html>"
%>
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;
"<html>" & VbCrLf & "<head>" & VbCrLf
"<title>Batch file name change</title>" & VbCrLf
"</head>" & VbCrLf & "<body>" & VbCrLf
' Variable description
Dim gbolGoProcedure
Dim strFromDir'Source folder
Dim strTargetDir'Dir's Destination 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 (("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 = ("")
Set objRootFolder = (strTargetDir)
'The specific settings of the file name, set to 100001 here, indicating that the file name will be from 100001
'Start, increment gradually, and can be set as needed;
strFileNameCount = 100001
For each objFile in
'For specific files, they are not processed and can be set as needed;
If = "" then strFileNameCount = StrFileNameCount - 1
strFileNameCount = strFileNameCount + 1
Next
Set objRootFolder = (strFromDir)
For each objFile in
strFileNameLen = Len ()
If Mid (,(strFileNameLen - 3),1) = "." then
strFileExt = right(, 4)
Else
strFileExt = right(, 5)
End If
strPrevFileName =
strNewFileName = strFileNameCount & strFileExt
strTargetDir & strNewFileName
"Source File: " &strFromDir&strPrevFileName & " > Move and change it to: " &strTargetDir& strNewFileName & "<br>" & vbCrLF
strFileNameCount = strFileNameCount + 1
strRealCount = strRealCount + 1
Next
"<p><b> Total processing: " & (strRealCount) & " files </B>" & vbCrLf
Set objRootFolder = Nothing
Set objFS = Nothing
gbolGoProcedure = True
End If
If gbolGoProcedure Then
("<p><b>Batch file batch movement and name change </b>") & vbCrLf
Else
("<center><br><form method=""post"" action="""" ID=form1 name=""form1"">") & vbCrLf
("<input type=""SUBMIT"" value="" Start "" ID=""GoButton"" name=""GoButton""") & vbCrLf
("</form>") & vbCrLf
("<p><b>Click the button to batch move and rename the file </b></center>") & VbCrLf
End If
"</body>" & VbCrLf & "</html>"
%>