Page 1 of 1
How to rename one file based on the name of another file

Posted:
Tue Oct 08, 2013 2:42 pm
by musecaptain
Hi,
I'm trying to create a rule that renames a file based on the name from another file. Actually the rule is pretty simple. There are two files:
1. this_is_the_first_file.xmf
2. this_is_the_second_file.xml
The rule should rename the .xmf file (eg. file 1) to the name of the .xml file (eg. file 2). The file extension should not be changed though..
So in the above example file 1 should be renamed to:
this_is_the_second_file.xmf
How can I do this with Hazel (maybe in conjuction with AppleScript)?
Thanks for any help
Re: How to rename one file based on the name of another file

Posted:
Wed Oct 09, 2013 12:26 pm
by Mr_Noodle
Is there only one xml file in that folder? If so, you can try something like:
- Code: Select all
If (all) are met for (enclosing folder)
If (all) are met for (any of its sub-files)
Extension is xml
Name matches (•xml file name)
Then
Rename (•xml file name)(extension)
You have to hold down the option key when adding a new condition to get the pop-up where you can target sub-files. Also, make (•xml file name) match (anything).
What you end up with is a rule which will match and capture the name of the xml file that is contained in the enclosing folder. I suggest giving that a shot.
Re: How to rename one file based on the name of another file

Posted:
Wed Oct 09, 2013 12:56 pm
by musecaptain
Mr_Noodle wrote:Is there only one xml file in that folder? If so, you can try something like:
- Code: Select all
If (all) are met for (enclosing folder)
If (all) are met for (any of its sub-files)
Extension is xml
Name matches (•xml file name)
Then
Rename (•xml file name)(extension)
You have to hold down the option key when adding a new condition to get the pop-up where you can target sub-files. Also, make (•xml file name) match (anything).
What you end up with is a rule which will match and capture the name of the xml file that is contained in the enclosing folder. I suggest giving that a shot.
Hi Mr_Noodle,
Thanks for the reply. Actually there are many files in the folder, so I'm wondering how the formula below could be tweaked for that purpose?
Re: How to rename one file based on the name of another file

Posted:
Fri Oct 11, 2013 3:38 pm
by Mr_Noodle
You'll have to add the sub-conditions to pick out that file. The way it is now, it will match the file with an xml extension but you can add additional conditions to narrow it down if needed.
Re: How to rename one file based on the name of another file

Posted:
Tue Oct 15, 2013 4:41 am
by musecaptain
Mr_Noodle wrote:You'll have to add the sub-conditions to pick out that file. The way it is now, it will match the file with an xml extension but you can add additional conditions to narrow it down if needed.
Thanks again for the reply! Maybe I should have been more specific. I can only make Hazel rename when I know the exact file names of the files (or has some kind of rule). Typically the .xml filename has some text prefixed eg. "Nordic_".
There are four files in the folder:
SW_DODA_13_15s_26827.mov
Nordic_SW_DODA_13_15s_26827.xml
SW_NODA_13_30s_26828.mov
Nordic_SW_NODA_13_30s_26828.xml
SW_DODA_13_15s_26827.mov should be renamed to Nordic_SW_DODA_13_15s_26827.mov
SW_NODA_13_30s_26828.mov should be renamed to Nordic_SW_NODA_13_30s_26828.mov
How can Hazel pick out the file using sub conditions, based on the above rules/examples? If this kind of advanced file pairing/matching and renaming is possible I should be able to persuade my company to buy some licenses as this could become a huge timesaver, in the daily workflow for all kinds of similar tasks.
If it's not possible, consider these wildcard matching features a feature request.
Re: How to rename one file based on the name of another file

Posted:
Tue Oct 15, 2013 3:33 pm
by Mr_Noodle
Well, from that example, I can pick several patterns. For instance, is the only difference that the file has Nordic_ tacked at the front? What is the exact correlation you are trying to use here?
Re: How to rename one file based on the name of another file

Posted:
Thu Oct 17, 2013 8:40 am
by musecaptain
Well, from that example, I can pick several patterns. For instance, is the only difference that the file has Nordic_ tacked at the front? What is the exact correlation you are trying to use here?
Sounds great that it seems solvable! At this moment the prefix (like "Nordic_") is the only thing that would be different for each matched .xml + .mxf set. However the prefix often changes from each project (to for instance "Yodu Nordic New_") so, it would be perfect if it would be possible to create one customizable master Hazel rule that works with all the prefixes - where you can simply hardcode all the prefix variations in the rule as they show up.
However having different Hazel rules for each type of prefix would also work, but would be less flexible if you have 30 different prefixes and want to change the rule a bit (to for instance label the xml files with "red").
To explain further, typically in one session we might have 8 xml files and 8 mxf files in the folder and in that folder we might have 1-3 prefixes depending on where the files are targeted at).
To make an example:
In the same folder, we might have these 4 files with the "Nordic_" prefix
SW_DODA_13_15s_26827.mxf
Nordic_SW_DODA_13_15s_26827.xml
SW_NODA_13_30s_26828.mxf
Nordic_SW_NODA_13_30s_26828.xml
and these 4 files with the "Yodu Nordic New_" prefix:
BC_ATTE_DROID_13_15s_26830.mxf
Yodu Nordic New_BV_ATTE_DROID_13_15s_26830.xml
SA_FIRE_13_20s_26829.mxf
Yodu Nordic New_SA_FIRE_13_20s_26829.xml
so in total 4 sets of xml+mxf files.
Re: How to rename one file based on the name of another file

Posted:
Thu Oct 17, 2013 12:43 pm
by Mr_Noodle
In this case, you might want to look into a script. In a future release, I am looking into having custom tokens be "re-usable" in conditions. In such a case you could have a custom token match the name of the first file then use that in another pattern in another condition on a different file where it keeps the value from the first condition. Not sure if that makes sense but if not, just trust me when I say that it would probably help in this case.
Re: How to rename one file based on the name of another file

Posted:
Fri Oct 18, 2013 7:02 am
by musecaptain
Mr_Noodle wrote:In this case, you might want to look into a script. In a future release, I am looking into having custom tokens be "re-usable" in conditions. In such a case you could have a custom token match the name of the first file then use that in another pattern in another condition on a different file where it keeps the value from the first condition. Not sure if that makes sense but if not, just trust me when I say that it would probably help in this case.
Ok, thanks. Sounds like a great feature, that really opens the door to much of new uses for Hazel. It's not necessary though to be able to turn it into
one rule - multiple rules works as well. I assume from your message though that both would need a custom script?
Re: How to rename one file based on the name of another file

Posted:
Fri Oct 18, 2013 2:41 pm
by Mr_Noodle
You could do a rule for each case (which may not require a script) but my point was directed more towards condensing it into a single rule.
Re: How to rename one file based on the name of another file

Posted:
Thu May 21, 2015 2:23 pm
by iMatt
I've been trying to accomplish something similar using data contained inside xml files, and finding that having a global variable would be VERY useful. Was this ever implemented, can we expect it to be implemented? THANKS!
Re: How to rename one file based on the name of another file

Posted:
Thu May 21, 2015 4:52 pm
by Mr_Noodle
This was implemented. Can you go into more detail what you are trying to do?
Re: How to rename one file based on the name of another file

Posted:
Thu May 21, 2015 5:33 pm
by iMatt
We have an internal project management tool that generates Campaign Numbers and job numbers for each image that we work on, and uses an unreliable shell script to rename each file with those numbers eg. "11111_22222_myFile.tif". These files are then sorted in folders on the server following that same structure 11111/22222/TIFs. The project management software creates an xml file containing the original file name, and the new file name with the campaign and job numbers on it. I would like to be able to extract the new file name from the XML file, and apply it to the original tif file.
Currently I'm using a rule that searches the contents of the XML for:
<currentname><![CDATA[• Original File Name.• EXTENSION]]></currentname>
<newname><![CDATA[• Campaign Number_• Job Number….• EXTENSION]]></newname>
And I can get Hazel to rename the xml file with the Campaign Number and Job Number without a problem, but being able to use those token variables to rename the TIF has proven to be an issue for me. My thought was I would need to use these tokens defined in this rule in a separate rule to rename the Tiff File. I throw myself unto the mercy of Mr. Noodle to show me the error of my thinking.
Re: How to rename one file based on the name of another file

Posted:
Fri May 22, 2015 11:22 am
by Mr_Noodle
That's a bit tricky. If the XML file is in the same folder, you
might be able to pull it off by doing the following:
- Code: Select all
Name matches (• original file name)
Extension is TIF
If (all) match for (enclosing folder)
If (all) match for (any sub-file)
Name is <name of xml file>
Contents contain match <current name><![CDATA[(• original file name)]]></currentname> <newname><!CDATA[(•campaign number)_(•job number)...(•extension)]]></newname>
Do...
I believe that will do what you want but you will have to tweak it a bit.
Re: How to rename one file based on the name of another file

Posted:
Fri May 22, 2015 1:34 pm
by iMatt
Boom. Got it to work with some fiddling, but wouldn't had thought to grab the original file name token from the image file if it hadn't been for your suggestion. Thanks for your guidance! This is where I ended up:
If all:
Kind is: Image
Full Name Matches •Originalfilename.•EXTENSION
If all its enclosing folder
If all any of its subfiles or folders
Extension is: .xml
Contents contain match: <newname><![CDATA[• Campaign Number_• Job Number_• Originalfilename.• EXTENSION]]></newname>
Rename with pattern: •Campaign Number_•Job Number_•Originalfilename.•EXTENSION