Rename remote batch files using ASP

Source: Internet
Author: User

For website designers, it is inevitable to process a large number of files, especially images and text files. The advantage is that the file names are not repeated and easy to manage. Here, we specifically introduce a simple and easy way to rename all files in any folder in batches. Of course, the renamed file names are based on the needs of website designers, increments by number.

We use ASP to implement the above functions. Note that, because FileSystemObject objects are designed for file operations, the implementation of this function must be performed on websites with file operation permissions. In general, due to security requirements, you may not be given the permission to the file. This is the first thing you need to pay attention.Program, We will operate on all the files in the specified folder strfromdir, as long as it is a file in this folder, regardless of the file type, the program will rename the file, of course, the file type will not be changed. The renamed files will not be saved in the original folder, but will be moved to the new folder strtargetdir. Note that we are moving, not copying, so, after the operation, all the 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, users who program in other languages may be deeply impressed. Now let's look at the function implementation.Code:

<% @ Language = VBScript %>
<% Option explicit %>
<%
'The following program batch rename the file names in the folder and move all the files to the new folder;
Response. Write "<HTML>" & vbcrlf & "Response. Write "<title> batch file renaming </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 extension
Dim strfilenamecount
Dim strnewfilename
Dim strrealcount 'number of processed files

Gbolgoprocedure = false

'If you click the start button, perform the following operations:
If (request. Form ("gobutton") = "start" then

'Specify the source folder and target 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)
'Specific settings of the file name. Here it is set to 100001, indicating that the file name will be from 100001
'Starts and increments gradually. You can set it as needed;
Strfilenamecount = 100001

For each objfile in objrootfolder. Files
'The specific files 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 rename 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 movement and renaming </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 move and rename the file in batches </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.