2017-02-13 71 views
0

我有一个长文件名填充索引并接受通配符

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,我也接受批处理文件。

回答

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 
+0

两个很好的答案和我喜欢的评论.. – joetex72

+0

如果这个脚本没有提示每次我都会喜欢,所以我可以从批处理文件@ mc-nd调用它。 – joetex72

+0

@ joetex72,如果您不需要输出,请删除'WScript.Echo'这行代码,或者如果您要从批处理文件中使用它,请将其作为'cscript.exe // nologo myScript.vbs'运行。如果不想删除'Echo's,但不希望输出使用'cscript.exe // nologo // b myScript.vbs'(控制台模式)或'wscript // b myScript.vbs'(窗口模式)。 '// b'将以*“批处理”*模式运行脚本(在批处理的意义上,而不是批处理文件),压缩所有输出。 –

0

使用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+))并将其附加到第一个捕获组的值((.*?),非贪婪匹配)。

+0

这个答案很简短,很好。我可以同时接受吗?他们都工作。 – joetex72

+0

@ joetex72不,只能接受一个答案。你应该选择一个你个人认为对解决你的问题最有帮助的方法([另见](http://meta.stackoverflow.com/a/5235))。 –

+0

这可以修改为抓住特定的扩展而不是所有@ ansgar-wiechers? Thant有一件事我喜欢关于另一个剧本。 – joetex72