重命名PDF文件,批处理文件 [英] Renaming pdf files with a batch file
问题描述
我需要或者编写一个批处理文件或将重命名文件一个VBScript。我需要保持在文件名中的一切到第二个。但删除第二个点后会发生什么。
I need to either write a batch file or a vbscript that will rename files. I need to keep everything in the file name up to the second "." but delete what comes after the second dot.
这是的文件名是什么样子的例子:
This is a sample of what the file names look like:
nnnnnnnnnnnnnnnn.xxxxxxxx.dddddddddd.pdf
-
N
= 16号0-9 -
X
=这种格式除息日:02232008 -
D
= 10数字0-9的这是我要删除的文件名部分的 n
= 16 numbers 0-9x
= date in this format ex:02232008d
= 10 numbers 0-9, this is the part of the file name that I want to delete.
我需要的D的从样品上方被删除,但保留的文件名相同的其余部分。我需要能够包含约3000 PDF文件的文件夹上运行该批处理文件。它可以放在右后卫到同一个文件夹或输出到不同的文件夹。
I need the d's from the sample above to be deleted but keep the rest of the file name the same. I need to be able to run this batch file on a folder that contains about 3,000 pdf files. It can either be put right back into the same folder or outputted into a different folder.
推荐答案
在VBScript中,您可以使用像
In VBScript, you can use something like
' 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
您会在这样一个批处理文件运行,输出重定向到一个日志文件
You would run it in a batch file thus, redirecting output to a log file
cscript rename-script.vbs > logfile.txt
这假定你可以简单地依靠时期分隔的文件名的部分,而不是划定的部分格式的细节。
This assumes that you can simply rely on the period to delimit the parts of the file name rather than the specifics of the format of the delimited parts.
要重新安排的日期,我认为这是在部分(1)
数组元素,你可以简单地提取字符串中的每一位,因为它是在一个特定的格式:
To rearrange the date, which I think is in the parts(1)
array element, you can simply extract each bit of the string because it's in a specific format:
'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
所以重建文件名时,可以更换部分(1)
与新格式化的日期
dim newname: newname = parts(0) & "." & date_ & "." & parts(3)
这篇关于重命名PDF文件,批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!