試用一下這個VBScript中:
Option Explicit
Dim File,MyRootFolder,RootFolder,Prefix,Suffix
MyRootFolder = Browse4Folder
Call Scan4File(MyRootFolder)
MsgBox "Script Done !",VbInformation,"Script Done !"
'**************************************************************************
Function GetTheParent(DriveSpec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(Drivespec)
End Function
'**************************************************************************
Function StripPathFolder(Path)
Dim arrStr : arrStr = Split(Path,"\")
StripPathFolder = arrStr(UBound(arrStr))
End Function
'**************************************************************************
Function StripPathFile(Path)
Dim arrStr : arrStr = Split(Path,"-")
StripPathFile = Replace(arrStr(UBound(arrStr)),"_","-")
End Function
'**************************************************************************
Function Browse4Folder()
Dim objShell,objFolder,Message
Message = "Please select a folder in order to scan into it and its subfolders to rename files"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0,Message,0,0)
If objFolder Is Nothing Then
Wscript.Quit
End If
Browse4Folder = objFolder.self.path
End Function
'**********************************************************************************************
Function Scan4File(Folder)
Dim fso,objFolder,arrSubfolders,File,SubFolder,NewFileName
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(Folder)
Set arrSubfolders = objFolder.SubFolders
For Each File in objFolder.Files
RootFolder = GetTheParent(GetTheParent(File))
Prefix = StripPathFolder(RootFolder)
Suffix = StripPathFile(File)
NewFileName = Prefix & Suffix
'MsgBox Prefix,Vbinformation,Prefix
'MsgBox Suffix,Vbinformation,Suffix
'MsgBox "New File Name ==> " & NewFileName,Vbinformation,Prefix & Suffix
Call RenameFile(File,NewFileName)
Next
For Each SubFolder in objFolder.SubFolders
Call Scan4File(SubFolder)
Next
End Function
'**********************************************************************
Sub RenameFile(File1,File2)
Dim Ws,Command,Execution
Set Ws = CreateObject("WScript.Shell")
Command = "Cmd /c Ren "& DblQuote(File1) &" "& DblQuote(File2) &""
Execution = Ws.Run(Command,0,False)
End Sub
'**********************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************
VBScript是不是VB.NET通過公平保證金相同。 – Plutonix
這是一個建議的標籤。我道歉。感謝您的意見。 – Kr3pt