VBScript ファイル名一括変更

VBScriptでファイル名から数字を一括で削除するサンプルです。

制限事項があり、ファイル名を変更した際に既に同一のファイル名があるとエラーとなります。

当方は新旧プログラムの差分を比較したいときに、割とファイル名に時刻が入っているパターンがあり
比較ファイルが100とかあると変更が大変な為、使ってます。

Const ForReading = 1 '読み込み
Const ForWriting = 2 '書きこみ(上書きモード)
Const ForAppending = 8 '書きこみ(追記モード)

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\sample\test")
 
Set FSO = Nothing

Sub ShowSubFolders(Folder)
    For Each File in Folder.Files 'Foler内のファイルを列挙する
        Fname = File.name
            Dim fs
            Dim fn
            Set fs = WScript.CreateObject("Scripting.FileSystemObject")
            Set fn = fs.GetFile(Folder & "\" & Fname)
            Dim rename
            rename = Fname
            rename = Replace(rename,"1","")
            rename = Replace(rename,"2","")
            rename = Replace(rename,"3","")
            rename = Replace(rename,"4","")
            rename = Replace(rename,"5","")
            rename = Replace(rename,"6","")
            rename = Replace(rename,"7","")
            rename = Replace(rename,"8","")
            rename = Replace(rename,"9","")
            rename = Replace(rename,"0","")
            fn.Name = rename
            Set fs = Nothing
            Set fn = Nothing
    Next
 
    For Each Subfolder in Folder.SubFolders 'Foler内のフォルダを列挙する
        ShowSubFolders Subfolder '再帰呼び出し
    Next
End Sub

■実行イメージ
・変更前
SampleA112233.txt
4455SampleB.txt
66SampleC7788.txt

・変更後
SampleA.txt
SampleB.txt
SampleC.txt


Bookmark this on Yahoo Bookmark
Bookmark this on Google Bookmarks
Share on LinkedIn
LINEで送る
Pocket

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>