0
votes

Basically I am trying to write a script to delete files after a certain filename, so based on the below file list

FILE_000001_FULL.ZIP
FILE_000002_FULL.ZIP
FILE_000003_FULL.ZIP
FILE_000004_FULL.ZIP
FILE_000005_FULL.ZIP
FILE_000006_DELTA.ZIP
FILE_000007_DELTA.ZIP
FILE_000008_FULL.ZIP

Everything up until FILE_000005_FULL.ZIP would be deleted. The files are created using a tool and will be sorted by file name, so highest number first. Basically need the 2 latest FULL files kept and the DELTA's (if any) between them. I hope that makes sense.

So far, this is what I have, but just loops constantly, not just until it finds the 2 latest fulls.

Dim fso, folder, files, ToDel, sfolder
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = ("C:\MDS")
Set ToDel = fso.CreateTextFile ("C:\MDS\FileList.txt", True)
Set folder = fso.GetFolder(sFolder)
set files = folder.files


For each folderIDX In files
    ToDel.WriteLine(folderidx.Name)
Next

ToDel.close


Dim arrFileLines()
i = 0
Set ObjFile = FSO.OpenTextFile("C:\MDS\FileList.txt", 1)
Do Until objFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objFile.ReadLine
    i = i + 1
Loop

ObjFile.Close
s = 0

Do While s < 2
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
    For Each strLine in arrFileLines
        IF InStr(strLine, "FULL") <> 0 Then
        wscript.echo "Found Full!!!!"
        wscript.echo strLine, s
        s = S + 1
        End If

Next
Next
LooP

My thoughts was to delete the lines from the text file, then use this text file to delete the files from the directory.

Hopefully that all makes sense and someone can pass some advice on!

1

1 Answers

0
votes

You should be able to do this with two iterations through your folder and without the need/use of a text file. During the first pass, record the numbers assigned to the two latest FULL's. Then, in your second pass, delete any files that are less than your second-highest FULL.

Here's how it might look:

' First pass: Find the two latest FULLs...
For Each File In FSO.GetFolder("c:\mds").Files

    ' Is this a FULL?
    If Right(File.Name, 8) = "FULL.ZIP" Then

        ' Get the numeric value from the file name (6 digits starting as pos 6)...
        intNum = CLng(Mid(File.Name, 6, 6))

        ' Maintain the two latest FULLs...
        If intNum > intMax1 Then
            intMax2 = intMax1
            intMax1 = intNum
        ElseIf intNum > intMax2 Then
            intMax2 = intNum
        End If

    End If

Next

' Second pass: Delete anything prior to the second-latest FULL...
For Each File In FSO.GetFolder("c:\mds").Files

    intNum = CLng(Mid(File.Name, 6, 6))
    If intNum < intMax2 Then File.Delete

Next