填充文件名中的索引为零并接受通配符

时间:2017-02-13 07:21:58

标签: batch-file vbscript windows-10

我有一些文件名长的文件

long filename with spaces 1.jpg
long filename with spaces 1.bmp
long filename with spaces 2.jpg
long filename with spaces 2.bmp
long filename with spaces 3.jpg
long filename with spaces 3.bmp
...
long filename with spaces 10.jpg
long filename with spaces 10.bmp
long filename with spaces 11.jpg
long filename with spaces 11.bmp
...
long filename with spaces 124.jpg
long filename with spaces 124.bmp
long filename with spaces 125.jpg
long filename with spaces 125.bmp

我想用零填充它们看起来像

long filename with spaces 0001.jpg
long filename with spaces 0001.bmp
long filename with spaces 0002.jpg
long filename with spaces 0002.bmp
long filename with spaces 0003.jpg
long filename with spaces 0003.bmp
...
long filename with spaces 0010.jpg
long filename with spaces 0010.bmp
long filename with spaces 0011.jpg
long filename with spaces 0011.bmp
...
long filename with spaces 0124.jpg
long filename with spaces 0124.bmp
long filename with spaces 0125.jpg
long filename with spaces 0125.bmp

并且可以使用通配符作为文件名。

我一直在使用这个脚本,但它只添加了我输入的零,并且不接受通配符:

Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("C:\MyPictures\")

For Each File In Folder.Files
    sNewFile = File.Name
    sNewFile = Replace(sNewFile, "long filename with spaces ", "long filename with spaces 000")
    If (sNewFile <> File.Name) Then 
        File.Move(File.ParentFolder + "\" + sNewFile)
    End If
Next

因此,使用该脚本,long filename with spaces 1.jpg变为long filename with spaces 0001.jpg,这就是我想要的,但long filename with spaces 125.jpg变为long filename with spaces 000125.jpg,这不是我正在寻找的。

我正在使用Windows 10,我也会接受批处理文件。

2 个答案:

答案 0 :(得分:1)

使用regular expression replacement function来调用自定义padding function

Function LPad(s, l, c)
  Dim n : n = 0
  If l > Len(s) Then n = l - Len(s)
  LPad = String(n, c) & s
End Function

Function PadIndex(m, m1, m2, pos, src)
  PadIndex = m1 & LPad(m2, 4, "0")
End Function

Set re = New RegExp
re.Pattern = "^(.*?)(\d+)$"

Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("C:\MyPictures").Files
  newName = re.Replace(fso.GetBaseName(f), GetRef("PadIndex")) & "." & _
            fso.GetExtensionName(f)
  If newName <> f.Name Then f.Name = newName
Next

正则表达式^(.*?)(\d+)$匹配以一个或多个数字结尾的任何字符串。替换函数填充第二个捕获组((\d+))的值,并将其附加到第一个捕获组((.*?),非贪婪匹配)的值。

答案 1 :(得分:0)

Option Explicit

' Required object to iterate filesystem
Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' Regular expression to match and tokenize the names of files to process
Dim re
    Set re = New RegExp 
    re.IgnoreCase = True 
    re.Pattern = "(long filename with spaces )0*([0-9]+)\.(jpg|bmp)"
    ' submatches:  ^0                            ^1        ^2

Dim file, match, newName
    ' For each file in the indicated folder
    For Each file In fso.GetFolder("w:\42198563").Files

        ' If the file name matches the regular expression
        For Each match In re.Execute( file.Name )
            ' Determine the new name for the file by joining the submatches
            ' (capture groups) retrieved by the regular expression
            newName = fso.BuildPath( _ 
                file.ParentFolder.Path _ 
                , match.SubMatches(0) _ 
                  & LeftZeroPad( 4, match.SubMatches(1) ) _ 
                  & "." _ 
                  & match.SubMatches(2) _ 
            )

            ' If the file name changes and there is not name collision, rename
            If file.Path <> newName Then 
                If Not fso.FileExists( newName ) Then
                    WScript.Echo file.Path & " => " & newName
                    file.Move newName
                Else 
                    WScript.Echo "SKIPPED " & file.Path
                End If 
            End If 
        Next ' match 
    Next ' file

' Helper function to pad the ending digits in the file name    
Function LeftZeroPad( length, data )
    If Len(data) < length Then 
        LeftZeroPad = Right( String(length, "0") & data, length )
    Else
        LeftZeroPad = data 
    End If 
End Function