Page 1 of 2

"Linking" file extensions together

Posted: 06 Mar 2010 17:58
by rhoelzl
Hi,

I often have set of files that have the same filename, but a different extension. For example this often appears in photography, where I have a set of files like

DSC_1234.NEF
DSC_1234.XMP
DSC_1234.PNG
DSC_1234.JPG
DSC_1234.PSD

It would be really, really cool if XYplorer would allow to define sets of extensions (in this case the set NEF,XMP,PNG,JPG,PSD) that are "linked together" that is when one of the files is renamed, all the others will be renamed automatically while keeping their extension.

Other sets that would make sense would (BIN,CUE) or (TEX,BIB,PS,DVI,PDF) etc.

Currently, I use the batch rename feature for this, but of course that is much more complicated, especially when you have to rename many such sets of files.

Just a suggestion, but I think it would simplify not only my life...

Regards,
rh

Re: "Linking" file extensions together

Posted: 06 Mar 2010 18:10
by calude
would also be useful for Audio
example: wavelab files
.wav (the audio file)
.gpk (the peak display)
.MRK (markers)


Calude

Re: "Linking" file extensions together

Posted: 06 Mar 2010 18:36
by Stefan
I don't understood what is complicated to
* select all files (Ctrl+A)
* right click and choose "Rename Special > Search&Replace"
* enter dsc_1234/NewName
* press preview?

Re: "Linking" file extensions together

Posted: 06 Mar 2010 18:50
by rhoelzl
Stefan wrote:I don't understood what is complicated to
* select all files (Ctrl+A)
* right click and choose "Rename Special > Search&Replace"
* enter dsc_1234/NewName
* press preview?
I don't understand this suggestion. Wouldn't this rename all files that contain the "dsc_1234" part anywhere in their name, even if these files are not part of the "set"? Also it's more complicated than using batch rename.

Re: "Linking" file extensions together

Posted: 06 Mar 2010 21:34
by Stefan
rhoelzl wrote:Wouldn't this rename all files that contain the "dsc_1234" part anywhere in their name,
even if these files are not part of the "set"?
If your other file name could contain the whole file name of the file to rename , then yes, it will.


But then you could use "RegEx rename" to match the whole string.
Just modify the parts in blue color:
^dsc_1234(\.\w{3,})$ > New Name$1

RegEx online tester: http://rereplace.com/



To make this more easy you could use an script which do this for you in the back ground.
Here is an example script (if one like to test, test with test files first)

Save this script as "Rename_FileSet.xys" into your XY\Scripts folder:
Rename_FileSet.xys

Code: Select all

/*
XYSName: Rename_FileSet.xys
Purpose: Rename all file with same base name, regardless the file extension, 
        to an new base name, leaving the original extension in place.
License: Free for all, but without any warranty - use with tryout copies of your real files first!!!

Date        Version   Description
2010.03.05  001      First test
2010.03.05  002      Added Preview function
*/


//"Rename Set"
$Files = getinfo("SelectedItemsNames", "|");
  $CurBase = "<curbase>";
 
  $HTML = utf8decode(urldecode(html('<HTML>
    <BODY>
    Purpose: Rename all file with same base name, regardless the file extension.<BR>
    1. Select first file with name to replace &nbsp; (or type base name yourself)<BR>
    2. Press Ctrl+A to select ALL files<BR>
    3. Execute this script<BR>
    4. Modify New Name<BR>
    5. Press Rename button<BR>    
    <FORM method="GET" action="xys:">
     &nbsp;Base name: <input type="text" name="bname" size="50" Value="'. $CurBase .'">
     &nbsp; (without extension) <BR>
     &nbsp;New name: <input type="text" name="nname" size="50" Value="'. $CurBase .'">
     &nbsp; (without extension) <BR> <BR><BR>
    <INPUT type="submit" name="Submit" value="Rename" > &nbsp; &nbsp; 
    <INPUT type="submit" name="Preview728" value="Preview" > &nbsp; &nbsp; 
    <input type="submit" name="Cancel728" value="Cancel">
    </FORM>
    </BODY>
    </HTML>',"700", "375", "Rename File sets")));

     $Cancel = strpos($HTML, "Cancel728=Cancel");
     end ($Cancel > 0) ;
     end ("$HTML"==""); 
     substr $HTML, $HTML, 1;
     //text $HTML;
 
     $CurBase = gettoken($HTML, "1", "&");
     $CurBase = gettoken($CurBase, 2, "=");    
     end ("$CurBase"==""), "Base name pattern missing"; 
     $NewName = gettoken($HTML, "2", "&");
     $NewName = gettoken($NewName, 2, "=");
     end ("$NewName"==""), "New name pattern missing"; 
     end ("$CurBase"=="$NewName"), "Using same pattern for both makes no sense"; 
     
     $Preview = "";
     $count = 1;
     while (true){  
        $file = gettoken($Files, $count, "|");
        If ($file=="") {break;}
     
        $filebase = regexreplace($file,"^(.+[^.])(\..+)$","$1");
        If ($filebase!=$CurBase) {incr $count; continue;}     
        $fileext = regexreplace($file,"^(.+[^.])(\..+)$","$2");
        
        IF (strpos($HTML, "Preview728=Preview") > 0){
        $Preview = $Preview . $NewName$fileext . "<crlf>";
        }else{
        rename r, "$file > $NewName$fileext";
        }
     incr $count;
     }
     
     IF (strlen($Preview)>0){
     text $Preview;
     setting AllowRecursion;
     self $ScriptFile, file;
     Load $ScriptFile;}

Script Preview
Rename_FileSet001.PNG

HTH? :D

Thanks to Don for implementing HTML();

Re: "Linking" file extensions together

Posted: 25 Aug 2010 11:57
by rhoelzl
Hi Stefan,

Sorry that I haven't answered much earlier. The script appeared to be a rather clumsy solution compared to really ALWAYS linking extensions together.

But since that latter thing does not seem to be coming I would like to work with your script to make it as user friendly as possible.

What I couldn't figure out: Is there any way to avoid having to press CTRL-A? It should just apply to all files (with the right basename) in the current directory. Could you tell me what needs to be modified?

Thanks!

Re: "Linking" file extensions together

Posted: 25 Aug 2010 12:45
by Stefan
Since you talk about renaming:
rhoelzl> "when one of the files is renamed, all the others will be renamed automatically while keeping their extension."
rhoelzl> "Currently, I use the batch rename feature for this,"

then batch renaming seams to be an good solution. (and my script is nonsense)

FROM:
DSC_1234.NEF
DSC_1234.XMP
DSC_1234.PNG
DSC_1234.JPG
DSC_1234.PSD
TO:
New Name DSC_1234.JPG
New Name DSC_1234.NEF
New Name DSC_1234.PNG
New Name DSC_1234.PSD
New Name DSC_1234.XMP
DO:
- Press Ctrl+A (To select all files in your case)
- press Shift+F2 to call "batch rename" (depends on XY version and settings)
- enter new name (and "*" if wanted to keep the old name)
- done

Or would you want to use this "FileSet"-feature for other purposes too?
Then you may post (IMO) more usage examples so Don can see the benefit.



rhoelzl wrote: What I couldn't figure out: Is there any way to avoid having to press CTRL-A? It should just apply to all files (with the right basename) in the current directory. Could you tell me what needs to be modified?

Thanks!
DELETED (see below)

Did that help?

Re: "Linking" file extensions together

Posted: 25 Aug 2010 12:49
by Stefan
Stefan wrote:
rhoelzl wrote: What I couldn't figure out: Is there any way to avoid having to press CTRL-A? It should just apply to all files (with the right basename) in the current directory. Could you tell me what needs to be modified?

Thanks!
Now i see and understood. Yes, this should be possible... i will update this later.

Re: "Linking" file extensions together

Posted: 25 Aug 2010 13:03
by Stefan
Stefan wrote:Since you talk about renaming:
rhoelzl> "when one of the files is renamed, all the others will be renamed automatically while keeping their extension."
rhoelzl> "Currently, I use the batch rename feature for this,"

then batch renaming seams to be an good solution. (and my script is nonsense)

FROM:
DSC_1234.NEF
DSC_1234.XMP
DSC_1234.PNG
DSC_1234.JPG
DSC_1234.PSD
TO:
New Name DSC_1234.JPG
New Name DSC_1234.NEF
New Name DSC_1234.PNG
New Name DSC_1234.PSD
New Name DSC_1234.XMP
DO:
- Press Ctrl+A (To select all files in your case)
- press Shift+F2 to call "batch rename" (depends on XY version and settings)
- enter new name (and "*" if wanted to keep the old name)
- done
Please try if this is what you want:

Code: Select all

//1) select one of the files with the common base name
//2) execute this script
$BaseName = "<curbase>"; //get current basename into an var
     selfilter """$BaseName*"""; //select all files with the same base name + dot + any extension
     #121; //call native XY -Batch rename feature
//3) enter an renaming pattern and press OK
(For How_to_execute_an_Script see our wiki >>> http://88.191.26.34/XYwiki/index.php/Sc ... Script_.3F)

Re: "Linking" file extensions together

Posted: 25 Aug 2010 13:24
by rhoelzl
Excellent, thank you.

I made this into a very easy to use "portable open with" command:

Code: Select all

|"Rename file set ''<curbase>.*''" *>::$BaseName = "<curbase>";selfilter """$BaseName*""";#121;

Re: "Linking" file extensions together

Posted: 25 Aug 2010 13:39
by Stefan
OK, fine! :D


Note: since we didn't need to use this $Var in this simple case, we can drop it too:

Code: Select all

|"Rename file set ''<curbase>.*''" *>::selfilter """<curbase>*""";#121;


And since it is an file set *I* would drop this ".*" random extension of the description and use

Code: Select all

|"Rename file set ''<curbase>''" *>::selfilter """<curbase>*""";#121;
But this belongs to you, since it's your script.

Re: "Linking" file extensions together

Posted: 25 Aug 2010 13:41
by rhoelzl
Hmm, I tried to improve the filtering by changing it into

Code: Select all

selfilter """>" . $BaseName . "\\.[^\\.]*""";
(aim: really only ignore extension), but it does not select anything... any ideas?

EDIT: changed the first "." into "\\." Also, $Basename would have to be escaped...

Re: "Linking" file extensions together

Posted: 25 Aug 2010 15:24
by Stefan
rhoelzl wrote:Hmm, I tried to improve the filtering by changing it into

Code: Select all

selfilter """>" . $BaseName . "\\.[^\\.]*""";
(aim: really only ignore extension), but it does not select anything... any ideas?

EDIT: changed the first "." into "\\." Also, $Basename would have to be escaped...
1.) I don't understood what you want:
"<curbase>" is already the file name without the-last-dot-and-following-signs ("the extension")
2.) please take an look into the help "Adv Topics > Scripting Commands Ref > selfilter", selfilter has no RegEx feature as far as i see.

Re: "Linking" file extensions together

Posted: 25 Aug 2010 15:53
by rhoelzl
1.) Yes but if Blabla.txt is selected, then the filter "<curbase>*" would, e.g., also match Blabla.Blublu.txt

2.) Hmm okay, thanks. I assumed that it should have that feature, because the "Edit -> Select -> Selection Filter" feature DOES have regexp.

Re: "Linking" file extensions together

Posted: 25 Aug 2010 16:09
by TheQwerty
I've always found SelFilter to be extremely frustrating since it lacks regex support.
I suggest using the single character wildcard '?' as in:

Code: Select all

selfilter "$BaseName.???";
Now the new problem is matching extensions which are not 3 characters, so you might want to add a 4 character pattern to this as well using:

Code: Select all

selfilter "$BaseName.???|$BaseName.????";
That should cover most cases, but you could always use a while loop and StrRepeat to generate a pattern that would handle 0 through n-character extensions.

Hope that helps!