Tp.file.move() file_to_move? parameter

The Templater docs here seem to indicate that tp.file.move() takes a second parameter (in bold). I’m hoping this parameter would make it possible to find and move a file that isn’t the current file but I’ve never seen any examples of this parameter being used. Maybe I’m misunderstanding the documentation.

tp.file.move(new_path: string, file_to_move?: TFile)

My use case involves moving pages returned by a Dataview.js query. The below results in a “TypeError: The “path” argument must be of type string. Received undefined” error.

const dv = this.app.plugins.plugins["dataview"].api
const lastIcons = dv.pages('"FOLDER/Subfolder"')
	.where(p => 
		p.file.path.includes("subfolder1") != true && 
		p.file.path.includes("subfolder2") != true)
	.forEach(async p => p.file.name.includes("Something here") == true ? 
		await tp.file.move("/FOLDER/Subfolder/subfolder1/" + p.file.name, p.file.path) :
		await tp.file.move("/FOLDER/Subfolder/subfolder2/" + p.file.name, p.file.path))

Would anyone be able to show me how this second parameter in tp.file.move() can be used?

1 Like

I know of two methods to get the TFile object for a page:

  • app.vault.getAbstractFileByPath(p.file.path)
  • tp.file.find_tfile("PageName")

The first one is useful in a context like yours, and the second is more useful if you just have the file name within a Templater template.

So with any luck, you should be able to replace p.file.path with the first line above, and the world is a happy place! :smiley:

1 Like

That worked! Thanks holroy

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.