2017-02-13 69 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