<% @LANGUAGE = VBSCRIPT %>
<%Option Explicit%>
<%
'以下程序批量改名文件夾中的文件名,並將所有文件移動到新的文件夾;
Response.Write "<html>" & VbCrLf & "<head>" & VbCrLf
Response.Write "<title>批量文件改名</title>" & VbCrLf
Response.Write "</head>" & VbCrLf & "<body>" & VbCrLf
' 變量說明
Dim gbolGoProcedure
Dim strFromDir '源文件夾
Dim strTargetDir '目標文件夾
Dim objFS
Dim objRootFolder
Dim objFile
Dim strFileNameLen
Dim strPrevFileName
Dim strFileExt '文件擴展名
Dim strFileNameCount
Dim strNewFileName
Dim strRealCount '處理的文件數量
gbolGoProcedure = False
' 如果點擊了開始按鈕,進行以下處理
If (Request.Form("GoButton")) = " 開 始 " then
' 指定源文件夾、目標文件夾
strFromDir = "D:test\"
strTargetDir = "D:\test1\"
' 將處理文件數量設置為0
strRealCount = 0
Set objFS = Server.CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFS.GetFolder(strTargetDir)
'文件名的具體設置,這裡設置為100001,表明文件名將從100001
'開始,逐步遞增,可以根據需要設置;
strFileNameCount = 100001
For each objFile in objRootFolder.Files
'對於特定的文件,不進行處理,可以根據需要設置;
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 "源文件: " &strFromDir&strPrevFileName & " > 移動並改名為: " &strTargetDir& strNewFileName & "<br>" & vbCrLF
strFileNameCount = strFileNameCount + 1
strRealCount = strRealCount + 1
Next
Response.Write "<p><b>一共處理: " & (strRealCount) & " 個文件</B>" & vbCrLf
Set objRootFolder = Nothing
Set objFS = Nothing
gbolGoProcedure = True
End If
If gbolGoProcedure Then
Response.Write("<p><b>批量文件批量移動和改名</b>") & vbCrLf
Else
Response.Write("<center><br><form method=""post"" action=""FileNameConverter.asp"" ID=form1 name=""form1"">") & vbCrLf
Response.Write("<input type=""SUBMIT"" value="" 開 始 "" ID=""GoButton"" name=""GoButton"">") & vbCrLf
Response.Write("</form>") & vbCrLf
Response.Write("<p><b>點擊按鈕對文件進行批量移動和改名</b></center>") & VbCrLf
End If
Response.Write "</body>" & VbCrLf & "</html>"
%>