【问题标题】:Renaming pdf files with a batch file使用批处理文件重命名 pdf 文件
【发布时间】:2011-08-16 20:09:09
【问题描述】:

我需要编写一个批处理文件或一个用于重命名文件的 vbscript。我需要将文件名中的所有内容保留到第二个“。”但删除第二个点之后的内容。

这是文件名的示例:

nnnnnnnnnnnnnnnn.xxxxxxxx.dddddddddd.pdf 
  • n= 16 个数字 0-9
  • x= 日期格式为 ex:02232008
  • d= 10 个数字 0-9,这是我要删除的文件名部分

我需要删除上面示例中的 d,但保持文件名的其余部分相同。我需要能够在包含大约 3,000 个 pdf 文件的文件夹上运行此批处理文件。它可以直接放回同一个文件夹或输出到不同的文件夹。

【问题讨论】:

    标签: vbscript batch-file batch-processing batch-rename


    【解决方案1】:
    FOR /F "USEBACKQ delims=. tokens=1-4" %%F IN (`DIR /B /A-D "C:\Path\To\PDFs\"`) DO (
      REN "%%~fF.%%G.%%H.%%I" "%%F.%%G.%%I"
    )
    

    如果您的文件的句点数量不同,只需添加一个简单的参数来计算存在多少句点分隔符然后执行。

    【讨论】:

      【解决方案2】:

      在 VBScript 中,你可以使用类似的东西

      ' the file paths. hardcoded, but you could alternatively collect these via command line parameters
      Const IN_PATH = "path\to\directory"
      Const OUT_PATH = "path\to\another\directory"
      
      ' check that the directories exist. you could create them instead, but here
      ' it just throws an error as that's easier
      dim fso: set fso = CreateObject("Scripting.FileSystemObject")
      if not fso.FolderExists(IN_PATH) then
          err.raise 1,, "Path '" & IN_PATH & "' not found"
      end if
      if not fso.FolderExists(OUT_PATH) then
          err.raise 1,, "Path '" & OUT_PATH & "' not found"
      end if
      
      dim infolder: set infolder = fso.GetFolder(IN_PATH)
      dim file
      for each file in infolder.files
          dim name: name = file.name
          dim parts: parts = split(name, ".")
          ' we're expecting a file format of a.b.c.pdf
          ' so we should have 4 elements in the array (zero-indexed, highest bound is 3)
          if UBound(parts) = 3 then
              ' rebuild the name with the 0th, 1st and 3rd elements
              dim newname: newname = parts(0) & "." & parts(1) & "." & parts(3)
              ' use the move() method to effect the rename
              file.move fso.buildpath(OUT_PATH, newname)
          else
              ' log the fact that there's an erroneous file name
              WScript.Echo "Unexpected file format: '" & name & "'"
          end if
      next 'file
      

      您可以在批处理文件中运行它,从而将输出重定向到日志文件

      cscript rename-script.vbs > logfile.txt
      

      这假设您可以简单地依靠句点来分隔文件名的各个部分,而不是分隔部分的格式细节。


      要重新排列我认为在 parts(1) 数组元素中的日期,您可以简单地提取字符串的每一位,因为它采用特定格式:

      'date in format mmddyyyy
      dim month_, day_, year_, date_
      month_ = left(parts(1), 2)
      day_ = mid(parts(1), 3, 2)
      year_ = right(parts(1), 4)
      date_ = year_ & month_ & day_ ' now yyyymmdd
      

      所以在重建文件名时,您可以将parts(1) 替换为新的格式化日期

      dim newname: newname = parts(0) & "." & date_ & "." & parts(3)
      

      【讨论】:

      • 这个脚本完美运行!非常感谢帮忙。我唯一没有使用的是日志文件。这最后一块应该完成我的自动化。再次感谢。
      • 我可以再给你一个脚本变体吗?我需要能够根据日期对这些重命名的文件进行排序。这样做的唯一问题是文件扩展名的日期部分采用以下格式:nnnnnnnnnnnnnnnn.dddddddd.pdf 其中 dddddddd 类似于 01232009(2009 年 1 月 23 日)。如果有 2008 年 1 月 23 日,则不会按日期排序。为了对这些进行排序,我需要能够将 2009 年或后四个 d 移动到前 4 个 d 的前面。你看有没有这样做?也许通过另一个具有不同分隔符的脚本?
      • 哇!!!逆天。完美无瑕。该脚本将节省大量时间。非常感谢。
      【解决方案3】:

      使用半自动重命名工具StringSolver,只需重命名第一个文件,检查通用重命名是否正常,然后在所有其他文件上接受。

      > move 1234567890123456.02232008.1946738250.pdf 1234567890123456.02232008.pdf
      

      得到解释:

      > move --explain
      
      the file name until the end of the second number + the extension
      

      如果您满意,可以使用move --auto 或简洁版本运行半自动工具:

      > move
      

      免责声明:我是这个为学术目的而制作的免费软件的合著者。

      【讨论】:

        猜你喜欢
        • 2019-05-25
        • 2014-02-26
        • 2019-06-08
        • 2013-06-25
        • 2014-02-22
        • 2018-06-23
        • 1970-01-01
        • 2012-12-10
        相关资源
        最近更新 更多