Rename several files using a basename

Discuss and share scripts and script files...
Post Reply
simabo
Posts: 4
Joined: 18 Jan 2010 10:29

Rename several files using a basename

Post by simabo »

Hi everyone,

Maybe someone can help me with this one. I've been testing variations for hours and reading numerous posts here for inspiration but can't figure out how to get what I want.

I have some folders, each one containing four files, each having a different extension. In other words, whatever the folder, I'm sure to find in it the four file types. I want to read the name of the type 1 file (let's say .doc) to have a basename, that I would use to rename the three other files.

Sounds simple but I'm stuck. So far, I have :

Code: Select all

selfilter "*.jpg";
$base = substr(GetInfo("SelectedItemsNames"), 0, -2);
$b = RegExReplace($base, ".doc", "");
I still need the rename part... Can anyone help me, please ?

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Rename several files using a basename

Post by TheQwerty »

I'm not sure how you were going about this with your script, but give this a try:

Code: Select all

"Name all after .doc."
	//The name of the file with this extension will be used.
	//Notice that it must not include the dot.
	$ext = "doc";

	//Select all files that match our extension.
	SelFilter("$ext", "f", "ext");

	//Abort if no file or multiple files are selected.
	End(GetInfo("CountSelected") != 1, "Zero or multiple '$ext' files found.");

	//Get the selected file's base.
	$base = Report("{BaseName}",1);

	//Invert the selection.
	Sel("i");

	//Rename the other items.
	Rename("b", "$base", "p");

simabo
Posts: 4
Joined: 18 Jan 2010 10:29

Re: Rename several files using a basename

Post by simabo »

You're my hero ! Thanks a lot !

Nice trick on inverting the selection. I'm a little puzzled by your syntax, though : my help file (latest), in Advanced Topics/Scripting Reference, shows commands without capitalization nor parenthesis. Am I looking for reference in the wrong place ?

Seriously, thanks again, you saved my hair ! :D

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Rename several files using a basename

Post by TheQwerty »

simabo wrote:I'm a little puzzled by your syntax, though : my help file (latest), in Advanced Topics/Scripting Reference, shows commands without capitalization nor parenthesis. Am I looking for reference in the wrong place ?
Nope you're in the right place, it's just that the syntax is a bit flexible. The commands are not case-sensitive, and the parenthesis are optional. I think I add them mostly out of habit, but I do feel they make things a little easier to follow.

Glad to have helped!

Post Reply