使用数字和前面的0预先添加所选目录中的所有文件

时间:2013-06-24 19:32:21

标签: shell vbscript directory rename

我发现了adobes bates编号工具的问题,其中文件名正在弄乱编号的顺序。我希望编写一个用户可以单击的脚本,并为所有文件添加文件夹扩展名。然后,该脚本将在文件夹中添加000001filename.pdf000002filename.pdf等所有文件名。

我以前从未合并过脚本,但是我发现了重命名OR prepend的脚本,而且我找不到任何可以用前面的0顺序重命名的脚本。

这是我到目前为止所做的:

Dim iloop As Integer
Dim iFileNumber As Integer

Dim sPrefix As String
Dim sNewFileName As String
Dim arr() As String
'Get array of all pdfs from the selected directory
arr = System.IO.Directory.GetFiles(strPath, "*.PDF")
'loop through the array
For iloop = 0 To UBound(arr)
  'Create a prefix for each file
  iFileNumber = iloop + 1
  Select Case iFileNumber
    Case 0 To 9 : sPrefix = "00000" & iFileNumber
    Case 10 To 99 : sPrefix = "0000" & iFileNumber
    Case 100 To 999 : sPrefix = "000" & iFileNumber
    Case 1000 To 9999 : sPrefix = "00" & iFileNumber
    Case 10000 To 99999 : sPrefix = "0" & iFileNumber
    Case Else : sPrefix = iFileNumber
  End Select
  Dim arr2() As String
  'split the path by the / symbol to get the filename
  arr2 = Split(arr(iloop),"\")
  'Add the prefix to the front of the filename, filename will be the last item in the array.
  arr2(uBound(arr2)) = sPrefix & arr2(uBound(arr2))
  'Put the new path and filename back together
  sNewFileName = Join(arr2,"\")
  'Rename the file with the new filename
  System.IO.File.Move(arr(iloop),sNewFileName)
Next

1 个答案:

答案 0 :(得分:0)

用于重命名给定文件夹中的PDF文件的VBScript解决方案,前缀为运行编号,左边用零填充,可能如下所示:

fldr = "..."

Set fso = CreateObject("Scripting.FileSystemObject")

i = -1
For Each f In fso.GetFolder(fldr).Files
  If LCase(fso.GetExtensionName(f)) = "pdf" Then
    Do
      i = i + 1
      newname = Right("0000" & i, 5) & f.Name
    Loop While fso.FileExists(fso.BuildPath(f.ParentFolder, newname))
    f.Name = newname
  End If
Next