Solution for Loop through files on Mac VBA – change of existing code
is Given Below:
I found this thread on here – How to loop through files in a folder Excel Mac 2016 – and it does mostly what I need it to do.
I’ve tried editing it myself, but no luck. I need three columns in my sheet – directory, filename and file extension. So I need to remove the date/time and size part of the code and replace it. I’d also like it to start putting in the entries into row B instead of wiping what is in row A. Any help is appreciated. 🙂
Option Explicit Sub GetFileNames() 'Modified from http://www.rondebruin.nl/mac/mac013.htm Dim folderPath As String Dim FileNameFilter As String Dim ScriptToRun As String Dim MyFiles As String Dim Extensions As String Dim Level As String Dim MySplit As Variant Dim FileInMyFiles As Long Dim Fstr As String Dim LastSep As String 'mac excel 2016 'Get the directory On Error Resume Next 'MJN folderPath = MacScript("choose folder as string") 'MJN If folderPath = "" Then Exit Sub 'MJN On Error GoTo 0 'MJN 'Set up default parameters to get one level of Folders 'All files Level = "1" Extensions = ".*" 'Set up filter for all file types FileNameFilter = "'.*/[^~][^/]*\." & Extensions & "$' " 'No Filter 'Set up the folder path to allow to work in script folderPath = MacScript("tell text 1 thru -2 of " & Chr(34) & folderPath & _ Chr(34) & " to return quoted form of it's POSIX Path") folderPath = Replace(folderPath, "'''", "'\''") 'Run the script ScriptToRun = ScriptToRun & "do shell script """ & "find -E " & _ folderPath & " -iregex " & FileNameFilter & "-maxdepth " & _ Level & """ " 'Set the String MyFiles to the result of the script for processing On Error Resume Next MyFiles = MacScript(ScriptToRun) On Error GoTo 0 'Clear the fist four columns of the current 1st sheet on the workbook Sheets(1).Columns("A:D").Cells.Clear 'Split MyFiles and loop through all the files MySplit = Split(MyFiles, Chr(13)) For FileInMyFiles = LBound(MySplit) To UBound(MySplit) On Error Resume Next Fstr = MySplit(FileInMyFiles) LastSep = InStrRev(Fstr, Application.PathSeparator, , 1) Sheets(1).Cells(FileInMyFiles + 1, 1).Value = Left(Fstr, LastSep - 1) 'Column A - Directory Sheets(1).Cells(FileInMyFiles + 1, 2).Value = Mid(Fstr, LastSep + 1, Len(Fstr) - LastSep) 'Column B - file name Sheets(1).Cells(FileInMyFiles + 1, 3).Value = FileDateTime(MySplit(FileInMyFiles)) 'Column C - Date Sheets(1).Cells(FileInMyFiles + 1, 4).Value = FileLen(MySplit(FileInMyFiles)) 'Column D - size On Error GoTo 0 Next FileInMyFiles 'Fit the contents Sheets(1).Columns("A:D").AutoFit End Sub