1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| Option Explicit
Dim StdIn: Set StdIn = WScript.StdIn
Dim StdOut: Set StdOut = WScript
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim FilesRenamed: FilesRenamed = 0
Dim FilesSkipped: FilesSkipped = 0
Main
set fso = nothing
Sub Main
'StdOut.Echo "The version of WSH on this computer is: " & WScript.Version
'get the parameter list
dim objArgs: Set objArgs = WScript.Arguments
if objArgs.Count > 2 then
dim path: path = objArgs(0) 'path
dim olds: olds = objArgs(1) 'string to replace
dim news: news = objArgs(2) 'new string
dim ext1: ext1 = ""
dim ext2: ext2 = ""
if objArgs.Count > 3 then ext1 = objArgs(3) 'old extension
if objArgs.Count > 4 then ext2 = objArgs(4) 'new extension
dim CurrentFolder: Set CurrentFolder = fso.GetFolder(path)
'StdOut.Echo "Warning: All files within the directory """ & CurrentFolder.Path & """ will be renamed."
ProcessFolder CurrentFolder , olds, news, ext1,ext2
else
StdOut.Echo "Usage: rename.vbs [folder path] [string to replace] [new string] [old extension] [new extension] "
end if
End Sub
'
'ProcessFolder
'
' Iterates thru the file list replacing patterns and the extension if available
'
Sub ProcessFolder (ByVal folder, ByVal oldTag, ByVal newTag, ByVal extOld, ByVal extNew)
Dim Files: Set Files = folder.Files
Dim File
For Each File In Files
If inStr(1,File.Name,oldTag) > 0 Then
'File.Move Replace(File.Path,Find,newTag)
'StdOut.Echo Replace(File.Path,oldTag,newTag) & vbCRLF
if (extOld <> "" and extNew <> "") then
'StdOut.Echo Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
File.Move Replace(Replace(File.Path,oldTag,newTag),extOld,extNew)
else
'StdOut.Echo Replace(File.Path,oldTag,newTag)
File.Move Replace(File.Path,oldTag,newTag)
end if
FilesRenamed = FilesRenamed + 1
Else
FilesSkipped = FilesSkipped + 1
End If
Next
End Sub |
Partager