使用DOS批处理脚本或vbscript重命名批处理文件

使用DOS批处理脚本或vbscript重命名批处理文件,vbscript,batch-file,batch-processing,batch-rename,Vbscript,Batch File,Batch Processing,Batch Rename,我正在寻找一个批处理脚本或vbscript(最好是vbscript),这将帮助我重命名约5000个文件的整个文件夹 要重命名的文件具有以下格式的名称:nnnnnnnnnnnnnnnnnnnn.dddddddd.pdf,其中n是编号0-9,d是示例格式01232009(2009年1月23日)中的日期 我需要能够将文件名的最后4个d移动到文件名的前四个d的前面。所有文件名都是一致的,并且使用相同的精确格式。以下是我试图实现的一个示例: nnnnnnnnnnnnnnnnnn.01232009.pdf=

我正在寻找一个批处理脚本或vbscript(最好是vbscript),这将帮助我重命名约5000个文件的整个文件夹

要重命名的文件具有以下格式的名称:nnnnnnnnnnnnnnnnnnnn.dddddddd.pdf,其中n是编号0-9,d是示例格式01232009(2009年1月23日)中的日期

我需要能够将文件名的最后4个d移动到文件名的前四个d的前面。所有文件名都是一致的,并且使用相同的精确格式。以下是我试图实现的一个示例:

nnnnnnnnnnnnnnnnnn.01232009.pdf=>nnnnnnnnnnnnnnn.20090123.pdf


这样,当我对文件排序时,它们可以按日期升序或降序排序。你知道怎么做吗

这与您上一个问题的答案基本相同。
它只是对日期做了一些不同的处理。
此外,它会回显文件名,以便您可以看到它正在做什么

如果目标文件已经存在,它将失败

Const IN_PATH = "path_to\directory"
Const OUT_PATH = "path_to\directory"

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

    ' Look for a file format of nnnnnnnnnnnnnnnn.dddddddd.pdf
    ' where the first segment is (I don't care) and dddddddd is
    ' a date, encoded.  for example 01232009 implies (January
    ' 23, 2009).
    If UBound(parts) = 2 And parts(2) = "pdf" And Len(parts(1)) = 8 Then
        ' build a new name, re-arranging the date
        dim newname: newname = parts(0) & "." & _
            Mid(parts(1), 5, 4) & Mid(parts(1), 1, 4) & "." & parts(2)
        ' use the move() method to effect the rename
        WScript.Echo Name & " ==> " & newname
        file.move fso.buildpath(OUT_PATH, newname)
    Else
        WScript.Echo "Not renaming file: '" & name & "'"
    End If
Next 'file

对于纯.BAT解决方案,请尝试以下方法

@echo off
SETLOCAL enabledelayedexpansion
FOR %%a IN (*.*) DO (
  CALL :cn %%a
  echo REN %%a !a!
)
GOTO :eof
:cn
SET a=%1
SET a=%a:~0,-13%.%a:~-8,4%%a:~-12,4%.pdf
GOTO :eof
仔细测试后,删除
echo
命令