Skip to content

list of files inside folder #364

Answered by MaksimZinovev
imeed166 asked this question in Help
list of files inside folder #364
Sep 11, 2021 · 1 answers · 3 replies

Is it possible to get a bulleted list of all the files included in a specific folder in this format ?

- [[name of file 1]]
- [[name of file 2]]

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:

  1. ask user to select folder to generate MOC with bulleted list (create new file) of all files in this folder
  2. all files in subfolders will be included
  3. if file exist, do not create file

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 

Replies

1 suggested answer
·
3 replies

Here is my snippet that does the following:

  1. ask user to select folder to generate MOC with bulleted list (create new file) of all files in this folder
  2. all files in subfolders will be included
  3. if file exist, do not create file

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
@imeed166

Hi, thank you for your response. Is there a way to get the list in the active file without creating a new file, and to replace something in the code so that it always checks the same folder. Thank you again.

@MaksimZinovev

Modify folderChoicePath and try this snippet

// Check the folder specified below and build a list of all markdown files
// all files in subfolders will be included

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// MacOS example
folderChoicePath = "resources/health/"
// If you are on Windows, try: "resources\health\"
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

if (folderChoicePath != null) {
	new Notice(`Folder: ${folderChoicePath}`, 5000);
	let filesInFolder = new Array();
	console.log("folderChoicePath: " + `${folderChoicePath}`)
	filesInFolder =  app.vault.getMarkdownFiles().filter(file => file.path.includes(folderChoicePath)).map(tFile=>tFile.basename)
	const filesList = new Array();
	filesInFolder.forEach((file) => {
		filesList.push('\n - [[' + file + ']]')
	});
	const folderChoice = folderChoicePath.split("/").slice(-2).join("")
	const fileName = folderChoice + " MOC"
	//files sorted case insensitive
	const heading = "## " + folderChoice.charAt(0).toUpperCase() + folderChoice.slice(1)+ " MOC"
	const content = heading + "\n" + filesList.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())).join('')

	// insert the list of files in the active file
	tR+=content
	new Notice(`Created new MOC`, 5000);	
} else {
	new Notice(`No folder selected`, 5000);
}
_%>
@imeed166

Thank you so much, you're amazing.

Answer selected by SilentVoid13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants