<% @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>"
%>