0

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

这是文件名的示例:

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

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

4

3 回答 3

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"
)

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

于 2011-08-17T13:56:14.480 回答
0

在 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)
于 2011-08-16T21:02:56.670 回答
0

使用半自动重命名工具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

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

于 2014-02-14T10:40:13.817 回答