Re: Find newest file in a series of folders by John
John
Sun Sep 09 10:44:16 PDT 2007
"Stever" <Stever@hotmail.com> wrote in message news:OQVqXwb8HHA.2752@TK2MSFTNGP06.phx.gbl...
> "Stever" <Stever@hotmail.com> wrote in message news:emeaTxX8HHA.4736@TK2MSFTNGP06.phx.gbl...
>> Hello All,
>>
>> I need to create a VB script that will start in a folder(Team) then walk the tree and find the newest file based on date
>> modified in each folder(Project and it's subfolders) The goal here is to provide each team with a list of their Projects and
>> the latest activity on each project to help them decide which projects are ready to be archived off our main server.
>>
>> Team
>> Project1
>> Project2
>> Project 3
>>
>> Return results to a .txt file that would look like:
>>
>> Team Project Newest File
>> Harry Project1 10/17/2005
>> Harry Project2 11/25/2006
>>
>>
>> I'm looking for some Code ideas as well as concepts. I've never been very good at creating efficient algorithms / searches.
>> Each Project may have up to 10k files and subfolders 10 + deep.
>>
>>
>>
>> Thanks in advance
>>
>>
>>
>> Steve
>>
>
> Almost there.... This is only going 1 subfolder deep in the tree. Unfortunately my users like to burry things pretty deep in the
> tree. Also is there a way to put all of the returned dates into yet another record set and sort that by date? That would
> complete the goal of returning just the Project plus the date of the newest file in it. I realize that I am takign advantage
> of your time but... do you have any quick fixes?
>
> Team
> Project1 Works
> Sub1 Works
> Sub2 Not working.txt 02/10/2001
> Sub3 Not working.
>
> Project2 Works
> Sub1 Works
> Sub2 Not working.txt 01/20/2007
> Sub3 Not working.
>
> Desired Output.
>
> Project1 02/10/2001
> Project2 01/20/2007
>
>
> Thanks for your time
>
I used Ekkehard's idea of the dir command and a regular expression to find the newest file, and then an array and a sort routine,
rather than an ADO recordset. To run: script.vbs //nologo > results.txt
Option Explicit
Dim sStartFolder
sStartFolder = "C:\temp\"
Const ForWriting = 2
Dim dtNewestFile, nWidth, i
Dim aProjects() 'Dynamic array to hold projects and dates of newest file
Dim objFso, objFolder, objSubFolder
Dim oWSH : Set oWSH = CreateObject( "WScript.Shell" )
Dim sCmd : sCmd = "%comspec% /c dir /s /o:-d /a-d "
Dim reFile : Set reFile = New RegExp
reFile.Global = True
reFile.Multiline = True
reFile.Pattern = "^\s+Directory\sof\s(.+)$" _
+ "(?:\r\n)+\s+((\d{2}.\d{2}.\d{4})\s+" _
+ "(\d{2}:\d{2})\s+[^ ]+\s" _
+ "(.*))$"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(sStartFolder)
'Process subfolders in start folder
Dim numProjects : numProjects = 0
For Each objSubFolder In objFolder.SubFolders
If Len(objSubFolder.Name) > nWidth Then nWidth = Len(objSubFolder.Name)
Dim oExec : Set oExec = oWSH.Exec( sCmd & Chr(34) & objSubFolder.Path & Chr(34) )
Dim sTxt : sTxt = oExec.Stdout.ReadAll
Dim oMTS : Set oMTS = reFile.Execute( sTxt )
If 0 < oMTS.Count Then
Dim oMT, dtThis
Dim dtNewest : dtNewest = CDate("01/01/1970")
For Each oMT In oMTS
dtThis = CDate( oMT.SubMatches( 2 ) + " " + oMT.SubMatches( 3 ) )
If dtThis > dtNewest Then dtNewest = dtThis
Next
'Add project and its date to array
ReDim Preserve aProjects(1,numProjects)
aProjects(0,numProjects) = objSubFolder.Name
aProjects(1,numProjects) = dtNewest
numProjects = numProjects + 1
End If
Next
'Sort and output
Insertion_Sort aProjects
WScript.Echo "Team", _
Left( "Project" & Space( nWidth ), nWidth ), _
"Newest File"
For i = 0 To UBound(aProjects,2)
WScript.Echo objFolder.Name, _
Left( aProjects(0,i) & Space( nWidth ), nWidth ), _
aProjects(1,i)
Next
'---------------------------------
Sub Insertion_Sort( aProjects )
'Sort aProjects array by descending date
Dim i, j, dtNewest, sProject
For i = 1 To UBound(aProjects,2)
dtNewest = aProjects(1,i)
sProject = aProjects(0,i)
j = i
Do While aProjects(1,j-1) < dtNewest
aProjects(1,j) = aProjects(1,j-1)
aProjects(0,j) = aProjects(0,j-1)
j = j - 1
If j = 0 Then Exit Do
Loop
aProjects(1,j) = dtNewest
aProjects(0,j) = sProject
Next
End Sub