如何遍历一个文件夹并将所有文件的名称添加到 Libreoffice 中一列的单元格中?使用 .csv 和 Anki

How to loop through a folder and add the names of all the files into cells in a column in Libreoffice? Using .csv and Anki

我有几个文件夹,每个文件夹包含几千个 .mp3 文件,里面有母语人士说的例句。我想按顺序遍历文件夹并将所有文件名添加到 LibreOffice 电子表格的第一列,因此在该列的每个单元格中它看起来像 [sound:name_of_file.mp3]。文件名是按顺序排列的,但它们是按节和章分开的,所以如果有办法获取文件名并使用它会更容易,而不是想出一个改变的函数基于 for 循环或类似内容的文本。

我想创建一个包含所有 [sound:name_of_file.mp3] 文本的 .csv 文件,我将把它作为一个卡片组上传到 Anki,然后从每个卡片中复制所有的 .mp3 文件将原始文件夹放入 Anki 的媒体文件夹中,以便卡片引用音频并播放它。

有没有办法做到这一点,如果有,怎么做?

谢谢!

如果您不想使用系统命令读取文件夹内容,如问题评论中所述,那么以下脚本将帮助您完成这项工作:

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, sFile As String
    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, False, False, False, , "mp3")
    nRow = UBound(aList)
    If nRow < 0 Then Exit Sub
    ReDim aRes(nRow)
    For i = LBound(aList) To UBound(aList)
        sFullName = ConvertFromURL(aList(i,0))
        sFile = FileNameOutOfPath(sFullName, GetPathSeparator())
        aRes(i) = "[sound:" & sFile & "]"
    Next i
    sFile = sStartFolder & GetPathSeparator() & "filelist.csv"
    sFile = InputBox("Specify the path and name for the output file:","Found " & (nRow + 1) & " files.", sFile)
    If sFile = "" Then Exit Sub
    SaveDataToFile(ConvertToURL(sFile),aRes)
End Sub