PC Review


Reply
Thread Tools Rate Thread

Adapt this Directory Listing working code to insert a file namepattern prompt.

 
 
u473
Guest
Posts: n/a
 
      29th Jul 2010
The following code is a perfectly working Directory Listing.
I adapted it to see all the properties I may need to filter.
I do not know if it could be improved but I am happy with it.
Now, I would like now to insert separate prompts for the file name
pattern like *Cost*
and extension type like mdb or accdb for instance.
I do not know exactly where I would insert the prompts and process the
If tests.
Help appreciated,
J.P.
..
Public X()
Public i As Long
Public objShell, objFolder, objFolderItem
Public FSO, oFolder, Fil

Sub MainExtractData()
'Code source : http://www.vbaexpress.com/kb/getarticle.php?kb_id=405
Dim NewSht As Worksheet
Dim MainFolderName As String
Dim TimeLimit As Long, StartTime As Double
ReDim X(1 To 65536, 1 To 7)
Set objShell = CreateObject("Shell.Application")
TimeLimit = Application.InputBox("Please enter the maximum time
that you wish this code to run for in minutes" & vbNewLine & vbNewLine
& _
"Leave this at zero for unlimited runtime", "Time Check box", 0)
StartTime = Timer
Application.ScreenUpdating = False
MainFolderName = BrowseForFolder()
Set NewSht = ThisWorkbook.Sheets.Add
X(1, 1) = "Path"
X(1, 2) = "File Name"
X(1, 3) = "Size"
X(1, 4) = "Type"
X(1, 5) = "Modified"
X(1, 6) = "Created"
X(1, 7) = "Age" ' File Age in Days from Modified Date to Now
i = 1
Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(MainFolderName)
'Error handling to stop the obscure error that occurs at time when
retrieving DateLastAccessed
On Error Resume Next
For Each Fil In oFolder.Files
Set objFolder = objShell.Namespace(oFolder.Path)
Set objFolderItem = objFolder.ParseName(Fil.Name)
i = i + 1
If i Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60
+ StartTime) Then
GoTo FastExit
End If
If i Mod 50 = 0 Then
Application.StatusBar = "Processing File " & i
DoEvents
End If
X(i, 1) = oFolder.Path
X(i, 2) = Left(Fil.Name, InStrRev(Fil.Name, ".") - 1)
X(i, 3) = Fil.Size
X(i, 4) = Mid(Fil.Name, InStrRev(Fil.Name, ".") + 1)
X(i, 5) = Fil.DateLastModified
X(i, 6) = Fil.DateCreated
X(i, 7) = DateDiff("d", Fil.DateLastModified, Now)
Next
'Get subdirectories
If TimeLimit = 0 Then
Call RecursiveFolder(oFolder, 0)
Else
If Timer < (TimeLimit * 60 + StartTime) Then Call
RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
End If

FastExit:
Range("A:G") = X
If i < 65535 Then Range(Cells(i + 1, "A"), Cells(65536,
"A")).EntireRow.Delete
Range("1:1").Font.Bold = True
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Range("a1").Activate
Set FSO = Nothing
Set objShell = Nothing
Set oFolder = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set Fil = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub
Sub RecursiveFolder(xFolder, TimeTest As Long)
Dim SubFld
For Each SubFld In xFolder.SubFolders
Set oFolder = FSO.GetFolder(SubFld)
Set objFolder = objShell.Namespace(SubFld.Path)
On Error Resume Next
For Each Fil In SubFld.Files
Set objFolder = objShell.Namespace(oFolder.Path)
'Problem with objFolder at times
If Not objFolder Is Nothing Then
Set objFolderItem = objFolder.ParseName(Fil.Name)
i = i + 1
If i Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest
Then
Exit Sub
End If
If i Mod 50 = 0 Then
Application.StatusBar = "Processing File " & i
DoEvents
End If
X(i, 1) = SubFld.Path
X(i, 2) = Left(Fil.Name, InStrRev(Fil.Name, ".") - 1)
X(i, 3) = Fil.Size
X(i, 4) = Mid(Fil.Name, InStrRev(Fil.Name, ".") + 1)
X(i, 5) = Fil.DateLastModified
X(i, 6) = Fil.DateCreated
X(i, 7) = DateDiff("d", Fil.DateLastModified, Now)
Else
Debug.Print Fil.Path & " " & Fil.Name
End If
Next
Call RecursiveFolder(SubFld, TimeTest)
Next
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that
directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to
False
BrowseForFolder = False
End Function

 
Reply With Quote
 
 
 
 
Jim Rech
Guest
Posts: n/a
 
      30th Jul 2010
You have to do something like this after getting the file spec from the user
maybe using Inputbox:

For Each Fil In oFolder.Files
If Fil.Name Like "*.xl*" Then



"u473" <(E-Mail Removed)> wrote in message
news:62a090f1-8fb8-49a0-9147-(E-Mail Removed)...
> The following code is a perfectly working Directory Listing.
> I adapted it to see all the properties I may need to filter.
> I do not know if it could be improved but I am happy with it.
> Now, I would like now to insert separate prompts for the file name
> pattern like *Cost*
> and extension type like mdb or accdb for instance.
> I do not know exactly where I would insert the prompts and process the
> If tests.
> Help appreciated,
> J.P.
> .
> Public X()
> Public i As Long
> Public objShell, objFolder, objFolderItem
> Public FSO, oFolder, Fil
>
> Sub MainExtractData()
> 'Code source : http://www.vbaexpress.com/kb/getarticle.php?kb_id=405
> Dim NewSht As Worksheet
> Dim MainFolderName As String
> Dim TimeLimit As Long, StartTime As Double
> ReDim X(1 To 65536, 1 To 7)
> Set objShell = CreateObject("Shell.Application")
> TimeLimit = Application.InputBox("Please enter the maximum time
> that you wish this code to run for in minutes" & vbNewLine & vbNewLine
> & _
> "Leave this at zero for unlimited runtime", "Time Check box", 0)
> StartTime = Timer
> Application.ScreenUpdating = False
> MainFolderName = BrowseForFolder()
> Set NewSht = ThisWorkbook.Sheets.Add
> X(1, 1) = "Path"
> X(1, 2) = "File Name"
> X(1, 3) = "Size"
> X(1, 4) = "Type"
> X(1, 5) = "Modified"
> X(1, 6) = "Created"
> X(1, 7) = "Age" ' File Age in Days from Modified Date to Now
> i = 1
> Set FSO = CreateObject("scripting.FileSystemObject")
> Set oFolder = FSO.GetFolder(MainFolderName)
> 'Error handling to stop the obscure error that occurs at time when
> retrieving DateLastAccessed
> On Error Resume Next
> For Each Fil In oFolder.Files
> Set objFolder = objShell.Namespace(oFolder.Path)
> Set objFolderItem = objFolder.ParseName(Fil.Name)
> i = i + 1
> If i Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60
> + StartTime) Then
> GoTo FastExit
> End If
> If i Mod 50 = 0 Then
> Application.StatusBar = "Processing File " & i
> DoEvents
> End If
> X(i, 1) = oFolder.Path
> X(i, 2) = Left(Fil.Name, InStrRev(Fil.Name, ".") - 1)
> X(i, 3) = Fil.Size
> X(i, 4) = Mid(Fil.Name, InStrRev(Fil.Name, ".") + 1)
> X(i, 5) = Fil.DateLastModified
> X(i, 6) = Fil.DateCreated
> X(i, 7) = DateDiff("d", Fil.DateLastModified, Now)
> Next
> 'Get subdirectories
> If TimeLimit = 0 Then
> Call RecursiveFolder(oFolder, 0)
> Else
> If Timer < (TimeLimit * 60 + StartTime) Then Call
> RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
> End If
>
> FastExit:
> Range("A:G") = X
> If i < 65535 Then Range(Cells(i + 1, "A"), Cells(65536,
> "A")).EntireRow.Delete
> Range("1:1").Font.Bold = True
> Rows("2:2").Select
> ActiveWindow.FreezePanes = True
> Range("a1").Activate
> Set FSO = Nothing
> Set objShell = Nothing
> Set oFolder = Nothing
> Set objFolder = Nothing
> Set objFolderItem = Nothing
> Set Fil = Nothing
> Application.StatusBar = ""
> Application.ScreenUpdating = True
> End Sub
> Sub RecursiveFolder(xFolder, TimeTest As Long)
> Dim SubFld
> For Each SubFld In xFolder.SubFolders
> Set oFolder = FSO.GetFolder(SubFld)
> Set objFolder = objShell.Namespace(SubFld.Path)
> On Error Resume Next
> For Each Fil In SubFld.Files
> Set objFolder = objShell.Namespace(oFolder.Path)
> 'Problem with objFolder at times
> If Not objFolder Is Nothing Then
> Set objFolderItem = objFolder.ParseName(Fil.Name)
> i = i + 1
> If i Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest
> Then
> Exit Sub
> End If
> If i Mod 50 = 0 Then
> Application.StatusBar = "Processing File " & i
> DoEvents
> End If
> X(i, 1) = SubFld.Path
> X(i, 2) = Left(Fil.Name, InStrRev(Fil.Name, ".") - 1)
> X(i, 3) = Fil.Size
> X(i, 4) = Mid(Fil.Name, InStrRev(Fil.Name, ".") + 1)
> X(i, 5) = Fil.DateLastModified
> X(i, 6) = Fil.DateCreated
> X(i, 7) = DateDiff("d", Fil.DateLastModified, Now)
> Else
> Debug.Print Fil.Path & " " & Fil.Name
> End If
> Next
> Call RecursiveFolder(SubFld, TimeTest)
> Next
> End Sub
> Function BrowseForFolder(Optional OpenAt As Variant) As Variant
> 'Function purpose: To Browser for a user selected folder.
> 'If the "OpenAt" path is provided, open the browser at that
> directory
> 'NOTE: If invalid, it will open at the Desktop level
> Dim ShellApp As Object
> 'Create a file browser window at the default folder
> Set ShellApp = CreateObject("Shell.Application"). _
> BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
> 'Set the folder to that selected. (On error in case cancelled)
> On Error Resume Next
> BrowseForFolder = ShellApp.self.Path
> On Error GoTo 0
> 'Destroy the Shell Application
> Set ShellApp = Nothing
> 'Check for invalid or non-entries and send to the Invalid error
> 'handler if found
> 'Valid selections can begin L: (where L is a letter) or
> '\\ (as in \\servername\sharename. All others are invalid
> Select Case Mid(BrowseForFolder, 2, 1)
> Case Is = ":"
> If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
> Case Is = "\"
> If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
> Case Else
> GoTo Invalid
> End Select
> Exit Function
> Invalid:
> 'If it was determined that the selection was invalid, set to
> False
> BrowseForFolder = False
> End Function
>


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File Listing in a Directory Varne Microsoft Excel Programming 15 22nd May 2009 05:59 PM
Directory listing into a txt file. How? SliqM Windows XP General 9 3rd Mar 2008 01:23 AM
directory and file listing =?Utf-8?B?RG9uIElyZWxhbmQ=?= Microsoft Access Forms 1 15th Feb 2006 03:07 PM
Re: printing directory listing using Dos Command prompt Bob I Microsoft Windows 2000 0 6th Aug 2003 08:40 PM
Code for picking out file names in directory listing Sandy Microsoft Access VBA Modules 5 23rd Jul 2003 02:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:14 PM.