Get A List Of File Names From A Folder And All Subfolders

It appears to me Libre Office can do this (Excel example link below). My best efforts to recreate this in Libre Office always ends in a hung application (crashed). I suspect this is already explained or has available info but I can’t find the answer or I’m failing to understand the “how to” within Libre Office. I’ve been able to duplicate a similar process to get some info from directories but this example appears to provide much more useful available options that would be really handy.

Thanks in advance.

1 Like

You can easy get same result with simple macro:

Option Explicit 

Sub createFileList
Dim oFolderDialog As Object, oUcb As Object 
Dim sStartFolder As String 
Dim aList As Variant, aRes As Variant 
Dim nRow As Long, i As Long 
Dim sFullName As String, sExt As String, sFile As String, sPath As String, lLen As Long, _
    sDateTime As String, iAttr As Integer, isRO As String, isHidden As String
Dim oDoc As Variant, oSheet As Variant, oRange As Variant 
	Globalscope.BasicLibraries.LoadLibrary("Tools")
	sStartFolder = ""
	oFolderDialog = CreateUnoService("com.sun.star.ui.dialogs.FolderPicker")
	oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
	If oUcb.Exists(GetPathSettings("Work")) Then _
        oFolderDialog.SetDisplayDirectory(GetPathSettings("Work"))
	If oFolderDialog.Execute() = 1 Then
		sStartFolder = oFolderDialog.GetDirectory()
		If oUcb.Exists(sStartFolder) Then sStartFolder = ConvertFromUrl(sStartFolder)
	End If
	If sStartFolder = "" Then Exit Sub
	aList = ReadDirectories(sStartFolder, True, True, False)
	nRow = UBound(aList)
	If nRow < 0 Then Exit Sub
	ReDim aRes(nRow)
	For i = LBound(aList) To UBound(aList)
		sFullName = ConvertFromURL(aList(i,0))
		sPath = DirectoryNameOutOfPath(sFullName, GetPathSeparator())
		sFile = FileNameOutOfPath(sFullName, GetPathSeparator())
		sExt = LCase(GetFileNameExtension(sFullName))
		lLen = FileLen(sFullName)
		sDateTime = Format(FileDateTime(sFullName),"YYYY-MM-DD HH:mm")
		iAttr = GetAttr(sFullName)
		isRO = Format((iAttr And 1)>0,"BOOLEAN")
		isHidden = Format(CBool((iAttr And 2)>0),"BOOLEAN")
		aRes(i) = Array(sPath, sFile, sExt, sDateTime, aList(i,1), lLen, isRO, isHidden) 
	Next i
	oDoc =  CreateNewDocument("scalc")
	oSheet = oDoc.getSheets().getByIndex(0)
	oSheet.getCellRangeByPosition(0,0,7,0).setDataArray(Array(Array _
        ("Folder Path","Name","Extention","Date","Kind","Size","ReadOnly","Hidden")))
	oRange = oSheet.getCellRangeByPosition(0,1,UBound(aRes(0)),UBound(aRes)+1)
	oRange.setDataArray(aRes)
	oRange.getColumns().OptimalWidth = True
End Sub
2 Likes

Simple macro. Thanks. I see I’m going to need to block off 6 weeks for training. I did manage to get it to open a file directory and select a folder. I’m certain after the training I’ll be able to make it work. Sincerely, thanks again. I’ve copied and saved it for reference.

very nice to share this macro, but I have one question. Due to external hard disk that fell I tried to recover all my mp3’s, he recovered the file names but they were empty, thus with your script he only writes like the jpgs in the folder and not the mp3s with no length, I tried to find it in the code but couldn’t. Would you be so kind to help me with this?
I love to play music and dj

I’m sorry, @Eduardoooo, but this particular macro will not help solve your problem. The fact is that this code, like all general-purpose programs, was created and intended to work with absolutely working equipment (hard disk). If you really love music and value your collection of audio recordings, it will be better if you turn to the data recovery specialists at the Service Center for help.

hey John, thank you for your reply. But I only would need a list of the files in the folders. Your macro only takes the .wavs & other file types but not the mp3’s. Could that be possible? I’ll take a screenshot

If you are using Linux, you can use the tree command or ls -R in a terminal. For Windows try dir /A-D /S /B .

Of course, if the files no longer exist none of the solutions will find them.

2 Likes

Well, just replace line
aList = ReadDirectories(sStartFolder, True, True, False)
with line
aList = ReadDirectories(sStartFolder, True, False, False)
(set param bcheckFileType “get file type by ext” instead “get file type by its content” - indeed, a file of zero length has no content, the macro cannot fulfill this requirement, so it skips empty files)

1 Like

you sir are a hero! I am very gratefull for sharing your knoweledge and helped me with this. I wish you all the best

and I’ll show you why, i has 10430 files found, so I have some work to do :slight_smile: But you saved me a lot of time and space
mp3listrecovery.ods (403.9 KB)