Using ASP to realize remote batch file renaming _asp Foundation

Source: Internet
Author: User
Tags file permissions
For web designers, it is unavoidable to deal with large quantities of documents at times, especially pictures and text files, which are often handled. And because of the Web site a large number of documents, for the same type of file naming, often directly with a certain number of incremental numbers to use as a file name, such as our common picture file naming, often used 1001.jpg, 1002.jpg this way, the advantage is that the file name will not repeat, easy to manage. Here, we specifically introduce a simple and easy way to achieve all files under any folder for batch renaming, of course, renamed after the file name, according to the needs of the Web site designers, according to the number of incremental.

We use ASP to achieve the above functions, it is important to note that, because the design to file operations, the use of FileSystemObject objects, so the implementation of the function to have file operations permissions of the Web site. The general virtual attention, considering the security requirements, may not give file permissions, this is the first thing we need to pay attention to; In addition, in the following program, we will manipulate all files in the specified folder Strfromdir, as long as the files in this folder, regardless of the file type, The program will change its file name operation, of course, file types will not be changed, renamed after the file will not be saved in the original folder, but moved to the new folder Strtargetdir, note that we are here to move, not copy, 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, it is simple to realize, and friends who are programming in other languages may feel deep; now, let's look at the functional implementation code:

<% @LANGUAGE = VBSCRIPT%>
<%option explicit%>
<%
' Rename the file name in the folder and move all the files to the new folder in the following batch;
Response.Write "Response.Write "<title> Batch file Rename </title>" & VbCrLf
Response.Write "' Variable description
Dim gbolgoprocedure
Dim Strfromdir ' source folder
Dim Strtargetdir ' destination folder
Dim OBJFS
Dim Objrootfolder
Dim objfile
Dim Strfilenamelen
Dim Strprevfilename
Dim strfileext ' file name extension
Dim Strfilenamecount
Dim Strnewfilename
Dim strrealcount ' Number of files processed

Gbolgoprocedure = False

' If you click on the Start button, do the following
If (Request.Form ("Gobutton")) = "Start" Then

' Specify source folder, destination folder

Strfromdir = "D:test\"
Strtargetdir = "D:\test1\"

' Set the number of processed files to 0

Strrealcount = 0

Set OBJFS = Server.CreateObject ("Scripting.FileSystemObject")

Set Objrootfolder = Objfs.getfolder (Strtargetdir)
' The filename is set to 100001, indicating that the filename will be from 100001
' Start, gradually increase, can be set according to need;
Strfilenamecount = 100001

For each objfile in Objrootfolder.files
' For specific files, no processing, can be set according to need;
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 & "> moved and renamed as:" &strTargetDir& strnewfilename ; "<br>" & VbCrLF
Strfilenamecount = Strfilenamecount + 1
Strrealcount = Strrealcount + 1

Next

Response.Write "<p> <b> is handled altogether:" & (Strrealcount) & "Files </B>" & VbCrLf

Set Objrootfolder = Nothing
Set OBJFS = Nothing

Gbolgoprocedure = True

End If

If gbolgoprocedure Then
Response.Write ("<p> <b> Bulk file 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 bulk move and rename the file </b> </center>") & VbCrLf
End If

Response.Write "</body>" & VbCrLf & "
%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.