Vbscript 删除上次修订之前的文件

Vbscript 删除上次修订之前的文件,vbscript,Vbscript,在一个文件夹中,我有10000个文件,文件名的结构如下: File1_0, File1_1, File1_2, File1_3 File2_0, File2_1, File2_2 File3_0, File3_1, File3_2, File3_3 ... File1000_0, File1000_1 File1_3 File2_2 File3_3 .... File1000_1 解决问题的一种方法是构建一个字典,将文件的基本名称映射到最高版本号: Set d = CreateObject("S

在一个文件夹中,我有10000个文件,文件名的结构如下:

File1_0, File1_1, File1_2, File1_3 File2_0, File2_1, File2_2 File3_0, File3_1, File3_2, File3_3 ... File1000_0, File1000_1 File1_3 File2_2 File3_3 .... File1000_1
解决问题的一种方法是构建一个字典,将文件的基本名称映射到最高版本号:

Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare

For Each f In fso.GetFolder(sourcefolder)
    basename = fso.GetBaseName(f.Name)
    a = Split(basename, "_")
    revision = Int(a(UBound(a)))
    prefix   = a(0)
    if d(prefix) < revision Then
        d(prefix) = revision
    End If
Next
Set d=CreateObject(“Scripting.Dictionary”)
d、 CompareMode=vbTextCompare
对于fso.GetFolder(sourcefolder)中的每个f
basename=fso.GetBaseName(f.Name)
a=拆分(基本名称“389;”)
修订版=Int(a(UBound(a)))
前缀=a(0)
如果d(前缀)<修订,则
d(前缀)=修订
如果结束
下一个
然后运行第二个循环以删除basename没有该版本的所有文件:

For Each f In fso.GetFolder(sourcefolder)
    basename = fso.GetBaseName(f.Name)
    a = Split(basename, "_")
    revision = Int(a(UBound(a)))
    prefix   = a(0)
    If d.Exists(prefix) And revision < d(prefix) Then
        f.Delete
    End If
Next
fso.GetFolder(sourcefolder)中每个f的

basename=fso.GetBaseName(f.Name)
a=拆分(基本名称“389;”)
修订版=Int(a(UBound(a)))
前缀=a(0)
如果d.存在(前缀)且版本
请注意,此代码假定分隔前缀和修订的下划线是basename中唯一的下划线。如果文件名包含多个下划线(如
foo\u bar\u 1.txt
),则需要调整前缀和修订的提取以解决此问题


话虽如此,我强烈建议不要在文件名中进行修订管理。使用版本控制系统(Git、Mercurial、Subversion等)。这就是它们被发明的目的。

Option Explicit
Option Explicit

' Folder to process
Dim sourceFolder
    sourceFolder = "."

Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' Regular expresion used to separate base name and sequence  
Dim re 
    Set re = New RegExp
    re.Pattern = "^(.*)_([0-9]+)$"

' Dictionary to store data for each sequence
Dim fileSequences
    Set fileSequences = WScript.CreateObject("Scripting.Dictionary")
    ' fileSequences will use the base name of the files as key and 
    ' hold as data an array with the higher sequence number and the
    ' associated file full path.

Dim f, colMatches, baseName, sequence    

    For Each f In fso.GetFolder(sourceFolder).Files
        ' Try to separate base name and sequence
        Set colMatches = re.Execute( fso.GetBaseName(f.Name) )
        ' Only handle serialized files, those whose name match the regular expresion
        If colMatches.Count > 0 Then 
            ' base name and sequence are stored in the Submatches collection
            ' file extension is included in the base name to avoid handling separate series as one
            baseName = LCase( colMatches.Item(0).SubMatches(0) & "." & fso.GetExtensionName( f.Name ) ) 
            ' Get the numeric sequence value - This should also handle zero prefixed sequence numbers
            sequence = CLng( colMatches.Item(0).SubMatches(1) )

            Select Case True
                Case Not fileSequences.Exists( baseName )
                    ' New sequence found - store current sequence value and the associated file path
                    fileSequences.Add baseName, Array( sequence, f.Path )

                Case sequence < fileSequences.Item( baseName )(0)
                    ' File with a lower sequence number found - Remove
                    f.Delete

                Case sequence > fileSequences.Item( baseName )(0)
                    ' File with a higher sequence number found - Remove previous one
                    fso.DeleteFile fileSequences.Item( baseName )(1)
                    ' Update sequence information with new higher value and the associated file path
                    fileSequences.Item(baseName) = Array( sequence, f.Path )
            End Select

        End If 
    Next 
'要处理的文件夹 Dim sourceFolder sourceFolder=“” 模糊fso 设置fso=WScript.CreateObject(“Scripting.FileSystemObject”) '用于分隔基名称和序列的常规表达式 暗淡的 Set re=New RegExp re.Pattern=“^(.*)_uz([0-9]+)$” '字典以存储每个序列的数据 模糊文件序列 Set fileSequences=WScript.CreateObject(“Scripting.Dictionary”) 'fileSequences将使用文件的基名称作为键和 '将具有较高序列号和 '关联的文件完整路径。 Dim f,colMatches,baseName,sequence 对于fso.GetFolder(sourceFolder).Files中的每个f '尝试将基名称和序列分开 Set colMatches=re.Execute(fso.GetBaseName(f.Name)) '仅处理序列化文件,即名称与常规表达式匹配的文件 如果colMatches.Count>0,则 '基名称和序列存储在Submatches集合中 '基本名称中包含文件扩展名,以避免将单独的系列作为一个系列处理 baseName=LCase(colMatches.Item(0).子匹配(0)&“&&fso.GetExtensionName(f.Name)) '获取数字序列值-这还应处理零前缀序列号 序列=CLng(colMatches.Item(0).子匹配(1)) 选择Case True 大小写不是fileSequences.Exists(baseName) '找到新序列-存储当前序列值和关联的文件路径 添加基名称,数组(序列,f.路径) 大小写顺序文件顺序。项(基本名称)(0) '找到序列号较高的文件-删除上一个文件 fso.DeleteFile fileSequences.Item(baseName)(1) '使用新的更高值和关联的文件路径更新序列信息 fileSequences.Item(baseName)=数组(序列,f.路径) 结束选择 如果结束 下一个
您已经发布了一些代码,但实际问题是什么?在从数组中删除名称后,我如何用上一个修订版重新生成名称,并删除其余的名称?Ansgar,谢谢您的建议!(但就目前而言,我是最终用户,因此修订控制系统的功能过于强大)。如何,您的代码在我的机器上不工作(可能是我)“代码不工作”不是问题描述。你希望我如何调试它?我不是坐在你的电脑前。另外,不,即使你是唯一的用户,设置RCS也不过分。很抱歉,我想说的是,你的代码运行时没有任何错误,但它没有删除任何内容。我没有调试/编程工具(我不能在这台机器上安装任何东西)来准确地告诉我刚才在notepade-
WScript.Echo
等中使用的问题是什么……但是这一行
d.Exists(basename)和revision
的操作不正确。@white\u标记我的错误。字典的键必须是basename的前缀部分,而不是整个basename。固定的。
Option Explicit

' Folder to process
Dim sourceFolder
    sourceFolder = "."

Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

' Regular expresion used to separate base name and sequence  
Dim re 
    Set re = New RegExp
    re.Pattern = "^(.*)_([0-9]+)$"

' Dictionary to store data for each sequence
Dim fileSequences
    Set fileSequences = WScript.CreateObject("Scripting.Dictionary")
    ' fileSequences will use the base name of the files as key and 
    ' hold as data an array with the higher sequence number and the
    ' associated file full path.

Dim f, colMatches, baseName, sequence    

    For Each f In fso.GetFolder(sourceFolder).Files
        ' Try to separate base name and sequence
        Set colMatches = re.Execute( fso.GetBaseName(f.Name) )
        ' Only handle serialized files, those whose name match the regular expresion
        If colMatches.Count > 0 Then 
            ' base name and sequence are stored in the Submatches collection
            ' file extension is included in the base name to avoid handling separate series as one
            baseName = LCase( colMatches.Item(0).SubMatches(0) & "." & fso.GetExtensionName( f.Name ) ) 
            ' Get the numeric sequence value - This should also handle zero prefixed sequence numbers
            sequence = CLng( colMatches.Item(0).SubMatches(1) )

            Select Case True
                Case Not fileSequences.Exists( baseName )
                    ' New sequence found - store current sequence value and the associated file path
                    fileSequences.Add baseName, Array( sequence, f.Path )

                Case sequence < fileSequences.Item( baseName )(0)
                    ' File with a lower sequence number found - Remove
                    f.Delete

                Case sequence > fileSequences.Item( baseName )(0)
                    ' File with a higher sequence number found - Remove previous one
                    fso.DeleteFile fileSequences.Item( baseName )(1)
                    ' Update sequence information with new higher value and the associated file path
                    fileSequences.Item(baseName) = Array( sequence, f.Path )
            End Select

        End If 
    Next