Want to show how many PDF files in a folder - vba

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet
Dim lngFileCount As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = ActiveSheet
Dim folderpath
folderpath = InputBox("N:\Files", "Folder Path")
Set objFolder = objFSO.GetFolder(folderpath)
ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & " are:"
For Each objFile In objFolder.Files
If UCase$(Right$(objFile.Name, 4)) = ".PDF" Then
ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = Replace$(UCase$(objFile.Name), ".PDF", "")
Z = Z + 1
MsgBox objFolder.Count.Z & ": The total number file in folder: "
End If
Next
If Z = 0 Then
MsgBox "No PDF Files found"
End If
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
End Sub

Try this:
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet
Dim lngFileCount As Long
Dim Z as Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = ActiveSheet
Dim folderpath As String
folderpath = InputBox("N:\Files", "Folder Path")
Set objFolder = objFSO.GetFolder(folderpath)
ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & " are:"
For Each objFile In objFolder.Files
If UCase$(Right$(objFile.Name, 4)) = ".PDF" Then
ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = Replace$(UCase$(objFile.Name), ".PDF", "")
Z = Z + 1
End If
Next
If Z = 0 Then
MsgBox "No PDF Files found"
Else
MsgBox "The total number of files in folder: " & Z
End If
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
I moved the MsgBox outside the loop so you only see it once with the total.
Also the property objFolder.Count.Z doesn't exist, you just want the variable Z which stores the count of your PDF files.

Use the following function to get the number of files
Function numberOfFiles(ByVal dirName As String, ByVal mask As String) As Long
On Error GoTo Catch
Dim maskName As String
maskName = dirName & mask
With CreateObject("wscript.shell")
numberOfFiles = UBound(Split(.exec("cmd /c Dir """ & maskName & """ /b/a").stdout.readall, vbCrLf)) - 1
End With
Exit Function
Catch:
numberOfFiles = 0
End Function
Test it with
Sub testIt()
Debug.Print numberOfFiles("D:\user\examples\", "*.pdf")
End Sub
Your code changes to
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet
Dim lngFileCount As Long
Dim Z As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = ActiveSheet
Dim folderpath As String
folderpath = InputBox("N:\Files", "Folder Path")
Set objFolder = objFSO.GetFolder(folderpath)
ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & " are:"
Z = numberOfFiles(folderpath, "*.PDF")
If Z = 0 Then
MsgBox "No PDF Files found"
Else
MsgBox "The total number of files in folder: " & Z
End If
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
and simply include the numberOfFiles function just below it.

Related

opening html file with vba

I have some .html files which I want to read with vba. I wrote this codes to do what I want but I get
object variable or with block variable not set
error.
Dim arrListATA() As String
Dim arrListTaskNo() As String
Dim arrListDesc() As String
Dim arrIssueNo() As String
Dim arrIssueDate() As String
Dim arrPartNo() As String
Dim arrDMC() As String
Dim arrApplicability() As String
Dim arrDMCModelCode() As String
Dim DMCs As String
Dim arrSubTask() As String
Dim subTasks As String
Dim subs() As Variant
Dim subs1 As String
k = 0
Dim objFile As Scripting.File
Dim objFolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
w = 0
m = 0
b = 0
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
fd.Filters.Clear
If fd.Show = -1 Then
myTopFolderPath = fd.SelectedItems(1)
Set objFolder = objFSO.GetFolder(myTopFolderPath)
Dim arrSplitedDMC As Variant
Dim arrSubTasks As Variant
For Each objFile In objFolder.Files
Debug.Print myTopFolderPath & "\" & objFile.Name
If Right(objFile.Name, 4) = "html" And Len(objFile.Name) = 33 And Left(objFile.Name, 8) <> "V2500-00" Then
Debug.Print myTopFolderPath & "\" & objFile.Name
Workbooks.Open Filename:=myTopFolderPath & "\" & objFile.Name
Debug.Print "Opened"
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
taskCheckFlag = False
myTemp = ""
partNoFlag = False
mySubTask = ""
For i = 1 To lastrow
txt = Cells(i, 1)
Next i
My folder path and my object names like this
C:\Users\ftk1187\Desktop\V2500 - Copy\V2500-00-70-72-02-00A-363A-D.html
It's not opening my .html files. How can I solve this problem?
The code below actually runs.
Option Explicit
Private Sub Test()
Dim arrListATA() As String
Dim arrListTaskNo() As String
Dim arrListDesc() As String
Dim arrIssueNo() As String
Dim arrIssueDate() As String
Dim arrPartNo() As String
Dim arrDMC() As String
Dim arrApplicability() As String
Dim arrDMCModelCode() As String
Dim DMCs As String
Dim arrSubTask() As String
Dim subTasks As String
Dim subs() As Variant
Dim subs1 As String
Dim objFSO As FileSystemObject
Dim Fd As FileDialog
Dim objFile As Scripting.File
Dim objFolder As Scripting.Folder
Dim arrSplitedDMC As Variant
Dim arrSubTasks As Variant
Dim myTopFolderPath As String
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Fd = Application.FileDialog(msoFileDialogFolderPicker)
' k = 0
' w = 0
' m = 0
' b = 0
With Fd
.Filters.Clear
If .Show = -1 Then
myTopFolderPath = .SelectedItems(1)
Set objFolder = objFSO.GetFolder(myTopFolderPath)
For Each objFile In objFolder.Files
Debug.Print myTopFolderPath & "\" & objFile.Name
Debug.Print myTopFolderPath
Debug.Print objFile.Name
Debug.Print Right(objFile.Name, 4), Len(objFile.Name), Left(objFile.Name, 8)
' If Right(objFile.Name, 4) = "html" And Len(objFile.Name) = 33 And Left(objFile.Name, 8) <> "V2500-00" Then
' Debug.Print myTopFolderPath & "\" & objFile.Name
' Workbooks.Open Filename:=myTopFolderPath & "\" & objFile.Name
' Debug.Print "Opened"
'
' lastrow = Cells(Rows.Count, 1).End(xlUp).Row
' taskCheckFlag = False
' myTemp = ""
' partNoFlag = False
' mySubTask = ""
'
' For i = 1 To lastrow
' txt = Cells(i, 1)
' Next i
Next objFile
End If
End With
End Sub
You will see that I added Option Explicit at the top and a few declarations that were missing. The variables k, w, m and b are also not declared but if they are numbers their value should already be 0 at that point of the code. According to my research, Excel should be able to open an HTML file but I wonder what it might show.
As a general piece of advice, I would recommend that you construct your code as one Main subroutine which calls other subs and functions, each of them no larger than 10 to 25 lines of code. In your code you already exceed that number in your declarations. The effect is a construct that you can't control.

VBA: How to open most recent two excel files in the folder

I have trying to open the most recent two excel file in the folder so far i did open the latest file in folder but i have to open 2nd latest file in folder. refer below code. please suggest how to open 2nd most recent file?
Sub findingdiff()
Dim FileSys, objFile, myFolder, c As Object
Dim wb1 As Workbook
Dim wb2 As Workbook
FolderName = ("C:\Users\ashokkumar.d\Desktop\Test\do\")
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set myFolder = FileSys.GetFolder(FolderName)
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If InStr(1, objFile.Name, ".xls") > 0 Then
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename = objFile.Name
End If
End If
Next objFile
'opening of latest file in the folder
Set wb2 = Workbooks.Open(FolderName & Application.PathSeparator & strFilename)
End Sub
Here's another way to tackle the problem. Create a sorted list and then process the first 2 files:
Sub Lastest2Files()
Dim rs As ADODB.Recordset
Dim fs As FileSystemObject
Dim Folder As Folder
Dim File As File
'create a recordset to store file info
Set rs = New ADODB.Recordset
rs.fields.Append "FileName", adVarChar, 100
rs.fields.Append "Modified", adDate
rs.Open
'build the list of files and sort
Set fs = New FileSystemObject
Set Folder = fs.GetFolder("C:\aatemp")
For Each File In Folder.Files
rs.AddNew
rs("FileName") = File.Path
rs("Modified") = File.DateLastModified
Next
rs.Sort = "Modified DESC"
'process the first 2 files
rs.MoveFirst
Set wb2 = Workbooks.Open(rs.fields("FileName").value)
rs.MoveNext
Set wb2 = Workbooks.Open(rs.fields("FileName").value)
End Sub
You can do it in one pass
Sub findingdiff()
Dim FileSys, objFile, myFolder, c As Object
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim strFilename, strFilename2
FolderName = ("C:\Users\ashokkumar.d\Desktop\Test\do\")
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set myFolder = FileSys.GetFolder(FolderName)
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If InStr(1, objFile.Name, ".xls") > 0 Then
If objFile.DateLastModified > dteFile Then
dteFile = objFile.DateLastModified
strFilename2 = strFilename
strFilename = objFile.Name
End If
End If
Next objFile
'opening of latest file in the folder
Set wb1 = Workbooks.Open(FolderName & Application.PathSeparator & strFilename)
Set wb2 = Workbooks.Open(FolderName & Application.PathSeparator & strFilename2)
End Sub
I modified findingdiff when the first file it encounter is most recent; Otherwise findingdiff don't get the second most recent.
Hope this helps...
Private Sub SortDictionaryByKey() '220926
' http://www.xl-central.com/sort-a-dictionary-by-key.html
Dim ProcName As String: ProcName = Mod_Name & "SortDictionaryByKey" & Debug_Output_Seperator '220926
Debug.Print TimeStamp & ProcName
'Set a reference to Microsoft Scripting Runtime by using
'Tools > References in the Visual Basic Editor (Alt+F11)
'Declare the variables
Dim Dict As Scripting.Dictionary
Dim TempDict As Scripting.Dictionary
Dim KeyVal As Variant
Dim Arr() As Variant
Dim Temp As Variant
Dim Txt As String
Dim i As Long
Dim j As Long
'Create an instance of the Dictionary
Set Dict = New Dictionary
'Set the comparison mode to perform a textual comparison
Dict.CompareMode = TextCompare
Dim FileSys, objFile, myFolder, c As Object
Dim FolderName As Variant
Dim dteLatest As Variant
''''''''''''''''''''''''''''''''
FolderName = FolderSelect_Source_Destination '220922
Set FileSys = CreateObject("Scripting.FileSystemObject")
Set myFolder = FileSys.GetFolder(FolderName)
With myFolder
End With
dteLatest = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
'220921
With objFile
If InStr(1, .name, PPT_Extension) > 0 Then
Dict.Add .DateLastModified, .Path
Debug.Print TimeStamp & ProcName & .Path
dteLatest = .DateLastModified
End If
End With
Next objFile
'Allocate storage space for the dynamic array
ReDim Arr(0 To Dict.Count - 1)
'Fill the array with the keys from the Dictionary
For i = 0 To Dict.Count - 1
Arr(i) = Dict.Keys(i)
Next i
'Sort the array using the bubble sort method
For i = LBound(Arr) To UBound(Arr) - 1
For j = i + 1 To UBound(Arr)
If Arr(i) > Arr(j) Then
Temp = Arr(j)
Arr(j) = Arr(i)
Arr(i) = Temp
End If
Next j
Next i
'Create an instance of the temporary Dictionary
Set TempDict = New Dictionary
'Add the keys and items to the temporary Dictionary,
'using the sorted keys from the array
For i = LBound(Arr) To UBound(Arr)
KeyVal = Arr(i)
TempDict.Add Key:=KeyVal, Item:=Dict.Item(KeyVal)
Next i
'Set the Dict object to the TempDict object
Set Dict = TempDict
'Build a list of keys and items from the original Dictionary
For i = 0 To Dict.Count - 1
Txt = Txt & Dict.Keys(i) & vbTab & Dict.Items(i) & vbCrLf
Next i
With Dict
str_Recent_FileFullName(1) = .Items(.Count - 1)
str_Recent_FileFullName(2) = .Items(.Count - 2)
Stop
'Display the list in a message box
End With
MsgBox Txt, vbInformation
Set Dict = Nothing
Set TempDict = Nothing
Set KeyVal = Nothing
Erase Arr()
Set Temp = Nothing
Set FileSys = Nothing
End Sub

Query user to choose path

I have a code that reads a folder contents (only other folders) and lists them into excel in a certain range.
The problem is that the path where the code reads contents (/CtrExtrase) is given in the code.
I need the path to be choosen by the user. Tried and failed totally.
My code:
Sub distribuire_foldere()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer
'CLEARS ALL PREVIOUS CONTENT
Sheets("DISTRIBUIRE foldere").Range("A2:A2000").ClearContents
'INSERTS IN CELL THE PATH WHERE THE SCRIPT IS READING
Sheets("DISTRIBUIRE foldere").Range("$E$1").Value = ThisWorkbook.path & "\CtrExtrase"
' LISTS THE CONTENT OF THE CHOOSEN FOLDER
Application.StatusBar = ""
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
On Error GoTo nuexistafolderul
'THIS IS THE PROBLEM, AS I NEED THE USER TO CHOOSE THE PATH:
Set objFolder = objFSO.GetFolder(Application.ThisWorkbook.path & "\CtrExtrase")
i = 1
'loops through each folder in the directory and prints their names
On Error GoTo handleCancel
Application.EnableCancelKey = xlErrorHandler
For Each objSubFolder In objFolder.subfolders
Application.StatusBar = objSubFolder.path & " " & objSubFolder.Name
'OUTPUTS THE FOLDERS NAME
Cells(i + 1, 1) = objSubFolder.Name
i = i + 1
Next objSubFolder
handleCancel:
If Err = 18 Then
MsgBox "Ai anulat procesul inainte de finalizare! Reia procedura!"
nuexistafolderul:
MsgBox "Nu exista folderul pentru extractia contractelor! Extrage intai contractele!"
End If
'CALLS A MODULE THAT INSERTS CERTAIN TEXT INTO A BATCH FILE
Call Module1.batchfile2
End Sub
Use FileDialog with FolderPicker, here it's wrap in a function :
Function GetFolder(Optional strPath As String = "C:\") As String
Dim fldr As FileDialog
Dim sItem As String
GetFolder = vbNullString
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
And your code, you can set the default path in GetFolder(ThisWorkbook.Path & "\") :
Sub distribuire_foldere()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer
'CLEARS ALL PREVIOUS CONTENT
Sheets("DISTRIBUIRE foldere").Range("A2:A2000").ClearContents
'INSERTS IN CELL THE PATH WHERE THE SCRIPT IS READING
Sheets("DISTRIBUIRE foldere").Range("$E$1").Value = ThisWorkbook.Path & "\CtrExtrase"
' LISTS THE CONTENT OF THE CHOOSEN FOLDER
Application.StatusBar = ""
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
On Error GoTo nuexistafolderul
'THIS IS THE PROBLEM, AS I NEED THE USER TO CHOOSE THE PATH:
Set objFolder = objFSO.GetFolder(GetFolder(ThisWorkbook.Path & "\"))
i = 1
'loops through each folder in the directory and prints their names
On Error GoTo handleCancel
Application.EnableCancelKey = xlErrorHandler
For Each objSubFolder In objFolder.SubFolders
Application.StatusBar = objSubFolder.Path & " " & objSubFolder.Name
'OUTPUTS THE FOLDERS NAME
Cells(i + 1, 1) = objSubFolder.Name
i = i + 1
Next objSubFolder
handleCancel:
If Err = 18 Then
MsgBox "Ai anulat procesul inainte de finalizare! Reia procedura!"
nuexistafolderul:
MsgBox "Nu exista folderul pentru extractia contractelor! Extrage intai contractele!"
End If
'CALLS A MODULE THAT INSERTS CERTAIN TEXT INTO A BATCH FILE
Call Module1.batchfile2
End Sub

Excel VBA List Files in Folder with Owner/Author Properties

This works, but is painfully slow:
Option Explicit
Sub GetDetails()
Dim oShell As Object
Dim oFile As Object
Dim oFldr As Object
Dim lRow As Long
Dim iCol As Integer
Dim vArray As Variant
vArray = Array(0, 3, 10, 20)
Set oShell = CreateObject("Shell.Application")
lRow = 1
Set oFldr = oShell.Namespace("\\mysite\www\docs\f150\group\IDL\collection\")
With oFldr
For iCol = LBound(vArray) To UBound(vArray)
Cells(lRow, iCol + 1) = .getdetailsof(.items, vArray(iCol))
Next iCol
For Each oFile In .items
lRow = lRow + 1
For iCol = LBound(vArray) To UBound(vArray)
On Error Resume Next
Cells(lRow, iCol + 1) = .getdetailsof(oFile, vArray(iCol))
Next iCol
Next oFile
End With
End Sub
I have the code below working, but I still cant get the Owner/Author or the specific file types.
Sub getFiles()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("\\mysite\www\docs\f150\group\IDL\collection")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name
With Cells(i + 1, 1)
Cells(i + 1, 1).Select
Selection.Hyperlinks.Add Anchor:=Selection, Address:=objFile.Path
End With
'print file path
Cells(i + 1, 2) = objFile.DateLastModified
i = i + 1
Next objFile
Columns.AutoFit
End Sub
I am trying to get a list of certain files and attributes into an Excel document, but the code keeps causing Excel to crash.
The code below may have some redundancy because I've been fiddling with it all day. Ultimately I would like to get the .pptx and .pdf file names, DateLastModified, and the owner or author
Sub ListAllFile()
Application.ScreenUpdating = False
Dim objFSO As Object
Dim objFolder As Object
Dim objFolderItem As Object
Dim objFile As Object
Dim ws As Worksheet
Dim myExt1 As String
Dim myExt2 As String
myExt1 = "*.pptx"
myExt2 = "*.pdf"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = Worksheets.Add
'Get the folder object associated with the directory
Set objFolder = objFSO.GetFolder("\\mysite\www\docs\f150\group\IDL\collection")
ws.Cells(1, 1).Value = "The current files found in " & objFolder.Name & "are:"
Set objFile = objFile
'Loop through the Files collection
For Each objFile In objFolder.Files
If StrComp(objFile.Name, myExt1) = 1 Or StrComp(objFile.Name, myExt2) = 1 Then
Dim strFilePath As Object
Dim arrHeaders(35)
Dim i As Integer
Dim objShell As Object
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("\\mysite\www\docs\f150\group\IDL\collection")
Set objFileName = objFolder.ParseName(objFile.Name)
For Each objFile In objFolder.Items
ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
ws.Cells(ws.UsedRange.Rows.Count + 0, 2).Value = objFile.DateLastModified
'This returns the "Owner" as the value for every file (not what I want)
ws.Cells(ws.UsedRange.Rows.Count + 0, 3).Value = objFolder.GetDetailsOf(objFile, 10)
'This returns the "Author" as the value for every file (not what I want)
ws.Cells(ws.UsedRange.Rows.Count + 0, 4).Value = objFolder.GetDetailsOf(objFile, 20)
'This returns the actual owner
ws.Cells(ws.UsedRange.Rows.Count + 0, 5).Value = objFolder.GetDetailsOf(strFileName, 10)
'This returns the actual author
ws.Cells(ws.UsedRange.Rows.Count + 0, 6).Value = objFolder.GetDetailsOf(strFileName, 20)
Next
End If
Next
Columns.AutoFit
'Clean up
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
Set objShell = Nothing
Set objFileName = Nothing
Set ws = Nothing
Application.ScreenUpdating = True
End Sub
I changed the second for each loop variable name to objfile1 and made appropriate modifications below it:
For Each objfile1 In objFolder.Items
ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objfile1.Name
ws.Cells(ws.UsedRange.Rows.Count + 0, 2).Value = objFile.DateLastModified
Note that DateLastModified is a property of objFile while Name belongs to objfile1.
Hope this helps.
You could try and use the CMD.exe DIR command to optimize it a bit, as well as a couple of other tweaks:
Sub Foo()
Dim myExt1 As String
Dim myExt2 As String
Dim searchFolder As Variant
Dim finalArray As Object
Dim shellObj As Object
searchFolder = "\\mysite\www\docs\f150\group\IDL\collection"
myExt1 = "*.pptx"
myExt2 = "*.pdf"
Set finalArray = CreateObject("System.Collections.ArrayList")
Set shellObj = CreateObject("Shell.Application").Namespace(searchFolder)
For Each file In Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR " & searchFolder & "\" & myExt1 & " /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
finalArray.Add CStr(file)
Next
For Each file In Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR " & searchFolder & "\" & myExt2 & " /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
finalArray.Add CStr(file)
Next
For Each file In finalArray.ToArray()
With Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Value = CStr(file)
.Offset(0, 1).Value = shellObj.GetDetailsOf(CStr(file), 10)
.Offset(0, 2).Value = shellObj.GetDetailsOf(CStr(file), 20)
End With
Next
End Sub

Using FileSystemObject to list files getting error

I have Excel-2007. I am using File System Object VBA code to list files in a directory. I have also set up reference to Microsoft Scriptlet Library in excel.
I am getting:
Compiler error:User-defined type not defined
on this very first code line
Dim FSO As Scripting.FileSystemObject
Code used by me as follows:
Sub ListFilesinFolder()
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder
Dim FileItem As Scripting.File
SourceFolderName = "C:\mydir"
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
Range("A1:C1") = Array("text file", "path", "Date Last Modified")
i = 2
For Each FileItem In SourceFolder.Files
Cells(i, 1) = FileItem.Name
Cells(i, 2) = FileItem
Cells(i, 3) = FileItem.DateLastModified
i = i + 1
Next FileItem
Set FSO = Nothing
End Sub
Can someone point out where am I going wrong?
**UPDATE -03-09-2015**
I have updated my program based on #brettdj program and some research to list all files including sub-folder files. It works for me. I look forward to suggestions to further improve it.
Sub ListFilesinFolder()
Dim objFSO As Object
Dim ws As Worksheet
Dim cl As Range
Dim objFolderName As String
objFolderName = "C:\FY_2015-2016\sunil"
Set objFSO = New Scripting.FileSystemObject
Set ws = ActiveSheet
With Range("A1:C1")
.Value2 = Array("File", "path", "Date Last Modified")
.Font.Bold = True
End With
Set cl = ws.Cells(2, 1)
ListFolders cl, objFSO.GetFolder(objFolderName)
Set objFSO = Nothing
End Sub
Sub ListFolders(rng As Range, Fol As Scripting.Folder)
Dim SubFol As Scripting.Folder
Dim FileItem As Scripting.File
' List Files
For Each FileItem In Fol.Files
rng.Cells(1, 1) = FileItem.Name
rng.Cells(1, 2) = FileItem.ParentFolder.Path
rng.Cells(1, 3) = FileItem.DateLastModified
Set rng = rng.Offset(1, 0)
Next
' Proces subfolders
For Each SubFol In Fol.SubFolders
ListFolders rng, SubFol
Next
With ActiveSheet
.Columns.EntireColumn.AutoFit
End With
End Sub
I am posting another update which is not cell by cell filling.
REVISED UPDATE ON 3-09-2015
Sub GetFileList()
Dim strFolder As String
Dim objFSO As Object
Dim objFolder As Object
Dim myResults As Variant
Dim lCount As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the directory from the user
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
If .SelectedItems.Count = 0 Then Exit Sub
'user cancelled
strFolder = .SelectedItems(1)
End With
Set objFolder = objFSO.GetFolder(strFolder)
'the variable dimension has to be the second one
ReDim myResults(0 To 5, 0 To 0)
' place make some headers in the array
myResults(0, 0) = "Filename"
myResults(1, 0) = "Size"
myResults(2, 0) = "Created"
myResults(3, 0) = "Modified"
myResults(4, 0) = "Accessed"
myResults(5, 0) = "Full path"
'Send the folder to the recursive function
FillFileList objFolder, myResults, lCount
' Dump these to a worksheet
fcnDumpToWorksheet myResults
'tidy up
Set objFSO = Nothing
End Sub
Private Sub FillFileList(objFolder As Object, ByRef myResults As Variant, ByRef lCount As Long, Optional strFilter As String)
Dim i As Integer
Dim objFile As Object
Dim fsoSubFolder As Object
Dim fsoSubFolders As Object
'load the array with all the files
For Each objFile In objFolder.Files
lCount = lCount + 1
ReDim Preserve myResults(0 To 5, 0 To lCount)
myResults(0, lCount) = objFile.Name
myResults(1, lCount) = objFile.Size
myResults(2, lCount) = objFile.DateCreated
myResults(3, lCount) = objFile.DateLastModified
myResults(4, lCount) = objFile.DateLastAccessed
myResults(5, lCount) = objFile.Path
Next objFile
'recursively call this function with any subfolders
Set fsoSubFolders = objFolder.SubFolders
For Each fsoSubFolder In fsoSubFolders
FillFileList fsoSubFolder, myResults, lCount
Next fsoSubFolder
End Sub
Private Sub fcnDumpToWorksheet(varData As Variant, Optional mySh As Worksheet)
Dim iSheetsInNew As Integer
Dim sh As Worksheet, wb As Workbook
Dim myColumnHeaders() As String
Dim l As Long, NoOfRows As Long
If mySh Is Nothing Then
'make a workbook if we didn't get a worksheet
iSheetsInNew = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = 1
Set wb = Application.Workbooks.Add
Application.SheetsInNewWorkbook = iSheetsInNew
Set sh = wb.Sheets(1)
Else
Set mySh = sh
End If
'since we switched the array dimensions, have to transpose
With sh
Range(.Cells(1, 1), .Cells(UBound(varData, 2) + 1, UBound(varData, 1) + 1)) = _
Application.WorksheetFunction.Transpose(varData)
.UsedRange.Columns.AutoFit
End With
Set sh = Nothing
Set wb = Nothing
End Sub
Would recommend using an array approach for speed
Sub ListFilesinFolder()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim lngCnt As Long
Dim X
objFolderName = "C:\temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objFolderName)
ReDim X(1 To objFolder.Files.Count, 1 To 3)
For Each objFile In objFolder.Files
lngCnt = lngCnt + 1
X(lngCnt, 1) = objFile.Name
X(lngCnt, 2) = objFile.Path
X(lngCnt, 3) = Format(objFile.DateLastModified, "dd-mmm-yyyy")
Next
[a2].Resize(UBound(X, 1), 3).Value2 = X
With Range("A1:C1")
.Value2 = Array("text file", "path", "Date Last Modified")
.Font.Bold = True
.Columns.EntireColumn.AutoFit
End With
End Sub
You're referencing Microsoft Scriptlet Library; should be Microsoft Scripting Runtime.
Try this:
Sub ListFilesinFolder()
Dim FSO
Dim SourceFolder
Dim FileItem
SourceFolderName = "C:\mydir"
Set FSO = CreateObject("Scripting.FileSystemObject") '<-- New change
Set SourceFolder = FSO.GetFolder(SourceFolderName)
Range("A1:C1") = Array("text file", "path", "Date Last Modified")
i = 2
For Each FileItem In SourceFolder.Files
Cells(i, 1) = FileItem.Name
Cells(i, 2) = FileItem
Cells(i, 3) = FileItem.DateLastModified
i = i + 1
Next FileItem
Set FSO = Nothing
End Sub