Batch rename PDF files using metadata?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
highend
Posts: 14431
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Batch rename PDF files using metadata?

Post by highend »

@perseid

Code: Select all

	global $File, $FileName;
	$SelectedItems = get("SelectedItemsPathNames", "|");

	foreach($Item, $SelectedItems, "|") {
		$File = $Item;
		sub "_SplitFileName";
		$Title = property("DocTitle", "$Item");
		$Author = property("DocAuthor", "$Item");
		if($Title != "" && $Author != "") {
			$NewName = "$Author" . "_" . "$Title" . "_" . "$FileName";
			rename b, "$NewName", p, "$Item";
		}
	}
"_SplitFileName"
	global $File, $FileName;

	// Separate file components
	$FindBS = strpos($File, "\", -1);
	$FindExt = strpos($File, ".", -1);
	$FileName = substr($File, $FindBS +1, ($FindExt - $FindBS -1));
@serendipity

It's not that I don't trust you. I don't trust the property command (or better: the OS that returns back a value). The same script will fail miserably on my OS while it works for perseid and I like to prefer a solution that works on all systems.
One of my scripts helped you out? Please donate via Paypal

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Batch rename PDF files using metadata?

Post by serendipity »

highend wrote: It's not that I don't trust you. I don't trust the property command (or better: the OS that returns back a value). The same script will fail miserably on my OS while it works for perseid and I like to prefer a solution that works on all systems.
Hey! I was just joking, :biggrin: i understand fully how it is with properties. Don himself mentioned about the OS dependency thing when he gave us property command.
And that's precisely why I made the property script, so that users can read items and figure out property names.

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Batch rename PDF files using metadata?

Post by perseid »

highend wrote:@perseid

Code: Select all

	global $File, $FileName;
	$SelectedItems = get("SelectedItemsPathNames", "|");

	foreach($Item, $SelectedItems, "|") {
		$File = $Item;
		sub "_SplitFileName";
		$Title = property("DocTitle", "$Item");
		$Author = property("DocAuthor", "$Item");
		if($Title != "" && $Author != "") {
			$NewName = "$Author" . "_" . "$Title" . "_" . "$FileName";
			rename b, "$NewName", p, "$Item";
		}
	}
"_SplitFileName"
	global $File, $FileName;

	// Separate file components
	$FindBS = strpos($File, "\", -1);
	$FindExt = strpos($File, ".", -1);
	$FileName = substr($File, $FindBS +1, ($FindExt - $FindBS -1));
Thanks highend! The script works flawlessly, but as I wrote in my previous post I wanted the renaming to append as follows: "<Title>_<Author>_originalfilename.pdf" instead of "<Author>_<Title>_originalfilename.pdf". Also, is it possible that when I run the script the preview rename confirm window is not shown for each PDF and instead the batch renaming is done for the selected files with a single click? Thanks.

highend
Posts: 14431
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Batch rename PDF files using metadata?

Post by highend »

I wanted the renaming to append as follows: "<Title>_<Author>_originalfilename.pdf"
Just change this line to whatever you prefer:

Code: Select all

$NewName = "$Author" . "_" . "$Title" . "_" . "$FileName";
e.g.:

Code: Select all

$NewName = "$Title" . "_" . "$Author" . "_" . "$FileName";
Also, is it possible that when I run the script the preview rename confirm window is not shown for each PDF and instead the batch renaming is done for the selected files with a single click?

Code: Select all

rename b, "$NewName", , "$Item";
(just delete the "p" character on that line)
One of my scripts helped you out? Please donate via Paypal

perseid
Posts: 13
Joined: 25 Mar 2012 06:41

Re: Batch rename PDF files using metadata?

Post by perseid »

highend wrote:
I wanted the renaming to append as follows: "<Title>_<Author>_originalfilename.pdf"
Just change this line to whatever you prefer:

Code: Select all

$NewName = "$Author" . "_" . "$Title" . "_" . "$FileName";
e.g.:

Code: Select all

$NewName = "$Title" . "_" . "$Author" . "_" . "$FileName";
Also, is it possible that when I run the script the preview rename confirm window is not shown for each PDF and instead the batch renaming is done for the selected files with a single click?

Code: Select all

rename b, "$NewName", , "$Item";
(just delete the "p" character on that line)
Thanks highend, works flawlessly. Will the script work for any file extension or just for PDF's?
Also, I want to learn how to write these scripts myself. I have zero knowledge and experience. Can you recommend a book or link to start learning the basics? Thanks.

This script run in XYplorer will perfectly meet my needs. Thanks again!

I want to mention 3rd party utilities that I tried or searched before attempting XYplorer:

http://www.boxoft.com/pdf-renamer/
I tested the trial version, very unstable, it crashed before being able to use it

http://download.cnet.com/A-PDF-Rename/3 ... 23008.html
I have not tested it

http://www.mendeley.com/
They have a free desktop application, it will not always read the metadata. Handy though to rename PDF files without metadata.

j_c_hallgren
XY Blog Master
Posts: 5826
Joined: 02 Jan 2006 19:34
Location: So. Chatham MA/Clearwater FL
Contact:

Re: Batch rename PDF files using metadata?

Post by j_c_hallgren »

perseid wrote:Also, I want to learn how to write these scripts myself. I have zero knowledge and experience. Can you recommend a book or link to start learning the basics? Thanks.
Presume you've seen and read
http://www.xyplorer.com/tour/index.php?page=scripting and
http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=5750 and
http://www.xyplorer.com/xyfc/viewtopic. ... 436#p60436 already? Those are some starting threads/pages to read...

There's no book on it but it does follow a lot of conventions of similar langauges..
Last edited by j_c_hallgren on 26 Mar 2012 21:36, edited 1 time in total.
Still spending WAY TOO much time here! But it's such a pleasure helping XY be a treasure!
(XP on laptop with touchpad and thus NO mouse!) Using latest beta vers when possible.

highend
Posts: 14431
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Batch rename PDF files using metadata?

Post by highend »

Will the script work for any file extension or just for PDF's?
For any. But ofc you have to change the script if you want to use other properties than DocAuthor and DocTitle.
Can you recommend a book or link to start learning the basics?
Open the help file (F1) and read through Advanced Topics - Scripting / Scripting Commands Reference and visit the script exchange part of the board: http://www.xyplorer.com/xyfc/viewforum.php?f=7 to learn from what is posted there.

There aren't any books about XYplorer's scripting language (at least afaik) so it's practicing and reading the help file :)
One of my scripts helped you out? Please donate via Paypal

Post Reply