list of files inside folder #364
list of files inside folder
#364
Replies
|
Here is my snippet that does the following:
It is ugly but it works for me <%*
// ask user to select folder to generate MOC for all files in this folder
// all files in subfolders will be included
const folders = this.app.vault.getAllLoadedFiles().filter(i => i.children).map(folder => folder.path);
const folderChoicePath = await tp.system.suggester(folders, folders);
if (folderChoicePath != null) {
new Notice(`Folder selected: ${folderChoicePath}`, 5000);
const files = app.vault.getMarkdownFiles();
const filesInFolder = new Array();
console.log("folderChoicePath: " + `${folderChoicePath}`)
files.forEach((file) => {
//console.log("file.path: " + `${file.path}`)
if (file.path.includes(folderChoicePath)) {
filesInFolder.push(file.basename)
};
})
const filesList = new Array();
filesInFolder.forEach((file) => {
//console.log(file)
filesList.push('\n - [[' + file + ']]')
});
const folderChoice = folderChoicePath.split("/").slice(-1)
const fileName = folderChoice + " MOC"
//files sorted case insensitive
const content = "## " + folderChoice + " MOC " + "\n" + filesList.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())).join('')
// create new file with a list of files in folder
const tFolder = app.vault.getAbstractFileByPath(folderChoicePath)
// if file exist, do not create file
if (await app.vault.exists(`${folderChoicePath}/${fileName}.md`)){
new Notice(`Unable to create. File already exists: ${folderChoicePath}/${fileName}.md`, 5000)
} else {
await tp.file.create_new(content, fileName, true, tFolder)
new Notice(`Created new MOC: ${folderChoicePath}/${fileName}.md`, 5000);
}
} else {
new Notice(`No folder selected`, 5000);
}
_%>
|
3 replies
Answer selected by SilentVoid13
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it possible to get a bulleted list of all the files included in a specific folder in this format ?
From what I found, I can only get the name of the folder with this
<% tp.file.folder() %>The folder doesn't include subfolders, only text files.
Here is my snippet that does the following:
It is ugly but it works for me