2017年8月12日土曜日

VBS to rename multiple files


Option Explicit

' +-------------------------------------------------------------------
' | Basically this statemants are Non-Sense.
' | The filename(s) is(are) passed Via FileDrop or Via [SendTo] context menu
' +-------------------------------------------------------------------
if WScript.Arguments.Count< 1 then
        WScript.echo("Please assign File(s), One or more")
        WScript.Quit(-1)
end if

' +-------------------------------------------------------------------
' | Specify the character(s) you want to rename
' +-------------------------------------------------------------------
Dim    Char_Before

Char_Before = InputBox("Enter character(s) you want to rename")

' +-------------------------------------------------------------------
' | Specifiy new character(s)
' +-------------------------------------------------------------------
Dim        Char_After

Char_After = InputBox("Enter New character(s)" & vbCrLf & "! If NONE, Means character(s) deletion")

Dim        objFSO
Dim        Char_FileName_Before
Dim        Char_FileName_After

Set obJFSO = CreateObject("Scripting.FileSystemObject")

' +-------------------------------------------------------------------
' | If processing filename does not include character(s) to be renamed,
' | Then skip rename process
' +-------------------------------------------------------------------
Dim        Cnt
Dim        ReplaceCounter: ReplaceCounter = 0

For Cnt=0 To WScript.Arguments.Count-1:Do
        Char_FileName_Before = objFSO.GetFileName(WScript.Arguments(Cnt))
        Char_FileName_After  = Replace( Char_FileName_Before, Char_Before, Char_After )

        if Char_FileName_After = Char_FileName_Before Then Exit Do

        objFSO.GetFile(WScript.Arguments(Cnt)).name = Char_FileName_After
        ReplaceCounter = ReplaceCounter +1
Loop Until 1:Next

' +-------------------------------------------------------------------

WScript.echo( ReplaceCounter & "_File(s) is(are) Renamed" )
Set objFSO = nothing