Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vbscript 使用批处理文件重命名pdf文件_Vbscript_Batch File_Batch Processing_Batch Rename - Fatal编程技术网

Vbscript 使用批处理文件重命名pdf文件

Vbscript 使用批处理文件重命名pdf文件,vbscript,batch-file,batch-processing,batch-rename,Vbscript,Batch File,Batch Processing,Batch Rename,我需要编写一个批处理文件或vbscript来重命名文件。我需要将文件名中的所有内容保留到第二个“.”,但删除第二个点之后的内容 这是文件名的示例: nnnnnnnnnnnnnnnn.xxxxxxxx.dddddddddd.pdf n=16个数字0-9 x=此格式的日期例如:02232008 d=10个数字0-9,这是我要删除的文件名部分 我需要删除上面示例中的d,但保持文件名的其余部分不变。我需要能够在一个包含大约3000个pdf文件的文件夹上运行此批处理文件。它可以直接放回同一个文件夹

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

这是文件名的示例:

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

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

在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
这假设您可以简单地依赖句点来分隔文件名的各个部分,而不是分隔部分格式的细节


要重新排列日期(我认为是在
部分(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
因此,在重建文件名时,可以用新的格式化日期替换
部分(1)

dim newname: newname = parts(0) & "." & date_ & "." & parts(3)
如果文件的句点数量不同,只需添加一个简单参数来计算存在的句点分隔符数量,然后执行。

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

> 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

免责声明:我是此免费软件的合著者,此免费软件用于学术目的。

此脚本工作完美!非常感谢你的帮助。我唯一没有使用的是日志文件。这最后一部分应该完成我的自动化。再次感谢。我可以再给你一个剧本的变体吗?我需要能够根据日期对这些重命名的文件进行排序。这样做的唯一问题是文件扩展名的日期部分采用以下格式:nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn.dddddddd.pdf,其中dddddd类似于01232009(2009年1月23日)。如果是2008年1月23日,则不会根据日期按顺序排序。为了对这些进行排序,我需要能够将2009或最后四个d移动到前四个d的前面。你认为这样做有什么好处吗?也许通过另一个用不同分隔符的脚本?哇!!!难以置信的完美无瑕的这个脚本将节省很多时间。非常感谢。
> move