Set FSO = CreateObject("Scripting.FileSystemObject") Set regEx = New RegExp sPath = Wscript.Arguments(0) Set tsR = FSO.OpenTextFile(sPath) sLines = Split(tsR.ReadAll, vbNewLine) tsR.Close Set tsW = FSO.CreateTextFile(sPath) For l = 0 To Ubound(sLines) - 1 tsW.WriteLine AddNum(sLines(l)) Next tsW.Close Set FSO = Nothing Wscript.Quit
Set arg = Wscript.Arguments If arg.Count > 0 Then Set FSO = CreateObject("Scripting.FileSystemObject") Set regEx = New RegExp For Each oItm In arg If FSO.FileExists(oItm) Then Call FileRename(oItm) ElseIf FSO.FolderExists(oItm) Then Select Case FolderRename Case 1 Set oFolder = FSO.GetFolder(oItm) oFolder.Name = AddNum(oFolder.Name) Case 2 For Each oFile In FSO.GetFolder(oItm).Files Call FileRename(oFile.Path) Next End Select End If Next Set FSO = Nothing End If Wscript.Quit
Sub FileRename(sPath) FSO.GetFile(sPath).Name = AddNum(FSO.GetBaseName(sPath)) & "." & FSO.GetExtensionName(sPath) End Sub
Function AddNum(sName) Dim cMatch, i, j With regEx .Pattern = "\d+" 'マッチパターン .Global = True '全体を検索する {True|False} Set cMatch = .Execute(sName) End With If cMatch.Count = 0 Then AddNum = sName Else i = 0 '最初にマッチしたものに加算 'i = cMatch.Count - 1 '最後にマッチしたものに加算 With cMatch(i) j = .Value + k AddNum = Left(sName, .FirstIndex) & _ Right(String(.Length, "0") & j, .Length - (Len(j) > Len(.Value))) & _ Mid(sName, .FirstIndex + .Length + 1) End With End If End Function