Page 2 of 2

Re: Help on renaming files

Posted: 30 Jul 2015 18:08
by unoentremil
Dear Sammay,

Well, yes you're right (partially, in my opinion). Yes, I made a mistake redacting that sentence: I should have written "but is does "nothing" on some of the files separated by dots and other cases that contain the suggested sources". If you take a look at the previously provided examples, some contain dots but I didn't specifically mentioned that circumstance.

Regarding the use of the verb "FIX", I thought it was clearly enough that I was referring to the instances where the script works on some files and not others sharing the same exact structure, like those using BD or TV instead DVD. The source (BD, TV, VHS, etc...) is the only apparent change and, since Highend considered my requested to refine the script to work with these variations, I provided some examples where it doesn't.

Regarding NEWER name structures, you're right too, I have extended the requirements on the go because I have found new very close occurrences (mostly separators) where, in my opinion, the same script is a better solution for everyone than starting a new thread. Please, correct me if you think I'm wrong.

Best,
Diego

Re: Help on renaming files

Posted: 30 Jul 2015 18:24
by unoentremil
Stef123 wrote: For unoentremil the objective may have been a bit different, not so much to get a solution for one specific example down to the last dot, but rather how to break it up in delimited parts, how to get started on "positional" renaming or swapping. I completely gave up on doing this in XY, simply because it turned out too time-consuming.

I bet it's a lot of fun once you've got the expertise, like all advanced skills that are highly rewarding, but it takes time to tinker with it. For fast results I revert back to other tools that offer (relatively) easy input dialogs or other means for that end.
Thanks for your mediation, Stef.

Believe me, I have spent countless hours testing lots of tools and procedures to accomplish this task, including renamers and movie scrappers. XY is not easy for me but I have recently learned how to use Paper folders (thanks Don) which in conjunction with this script and the swapping RegEx are going to be of great help.
For fast results I revert back to other tools that offer (relatively) easy input dialogs or other means for that end.
Of course, I'm open to suggestions :)

Thanks a lot to all you guys for your invaluable help.

Re: Help on renaming files

Posted: 30 Jul 2015 18:36
by unoentremil
highend wrote:Won't be catched...:
La alegre divorciada (1934).The Gay Divorcee.Mark Sandrich.HD.ES.EN.Divx5.AC3.txt
La alegre divorciada (1934).The Gay Divorcee.Mark Sandrich.VHS.ES.EN.Divx5.AC3.txt
OK. No problem. Thanks for trying :)
Rest should work:
Yes, they does :appl:

I'm especially delighted with this solution because it not only renames cached files but also skips already renamed ones.

Thanks a lot for your help, Highend

Re: Help on renaming files

Posted: 30 Jul 2015 18:47
by bdeshi
@diego: er, okay, I'm sorry if that felt offensive. But the thread was a bit, well, annoying back there. Frankly. :whistle:

RegExpressing is not always easy (for me, I'm sure highend and friends make them while half-asleep), and it doesn't create joy to have to go in and make, and keep making, small avoidable changes.

Again, take no offense. Just try to provide clearer, cleaner objectives. Then it's fun for both parties! :D



edit: duh, I could've said "expressing regular expressions isn't always an express task" :P

Re: Help on renaming files

Posted: 30 Jul 2015 20:58
by Stef123
unoentremil wrote: Of course, I'm open to suggestions :)
If you don't need to adapt to changing conditions - as I had assumed because of your several variations which triggered Sammay's remark - then your best bet is to stick with the RegEx solutions provided by Sammay and highend.

My scenarios are different, they involve the swapping of groups of digits in product numbers and other codes. This forum here provides great help but it's just not feasible to come here every time I need to rename, and the RegEx solutions rarely ever work at the first shot.

So anyway, I found it much more efficient to swap via the "tempVariable" of RenameMaster or to use AdvancedRenamer. To keep it easy, I often do it in 2 or even 3 simple steps, one after another. For me it works because it allows me to handle all kinds of variations. For a while I tried it with XY and RegEx - with massive support of RegEX Buddy - but it turned out too time-consuming. But again, your case is different, no recommendations other than stick with the ready-made solutions if you don't need to modify often.

Re: Help on renaming files

Posted: 30 Jul 2015 21:28
by unoentremil
SammaySarkar wrote:@diego: er, okay, I'm sorry if that felt offensive. But the thread was a bit, well, annoying back there. Frankly. :whistle:

RegExpressing is not always easy (for me, I'm sure highend and friends make them while half-asleep), and it doesn't create joy to have to go in and make, and keep making, small avoidable changes.

Again, take no offense. Just try to provide clearer, cleaner objectives. Then it's fun for both parties! :D
No offenses, Sammay.

I really think that those tweaks could also help to many other people. For what I have seen, using dots is a common practise and that was another important reason why I asked for it. I just thought that a much more "universal" solution was there... just a step ahead (even more considering the skills of Highend). In the end we've got a really practical solution to rename the movies to a format that Media Centers can index.

Sincerely, I really try to make 'good' questions while being as concise as I can (English is not easy to me, either), but I listen and appreciate your suggestions.

Best wishes.

Re: Help on renaming files

Posted: 30 Jul 2015 21:45
by highend
and the RegEx solutions rarely ever work at the first shot
It depends. If enough examples are provided with all possible variations...
I just thought that a much more "universal" solution was there
You can catch a lot of patterns even with a single regex but it's not always the best solution if you want to shuffle things around. Capturing groups are necessary and if you need multiple ones (director, english name of a movie and sometimes all of them are present, on other occasions some are missing), you need to adapt the pattern.

Examples like:
La alegre divorciada (1934).The Gay Divorcee.Mark Sandrich.HD.ES.EN.Divx5.AC3.txt

Can be tracked as well but you need another separate regex for it (because there isn't a delimiter after the spanish name like in all other examples)...

Re: Help on renaming files

Posted: 30 Jul 2015 22:44
by Stef123
highend wrote:
and the RegEx solutions rarely ever work at the first shot
It depends. If enough examples are provided with all possible variations...
Absolutely true. But that's exactly the catch I was hinting at. It's essential to be as nitpickingly precise as possible when asking for help. And even then it's still a challenge to code it right. Admitted, my opinion is very biased and based on very few instances, but when you're in a team - just regular users - and everyone gets it renamed with freeware, file managers, even photo browsers, with just a few mouse clicks - whereas you sit there and brood and finally get some working RegEx from the forum, it makes you wonder: this crazy complicated line is the ONLY way to get this thing renamed in XY? That was the moment when I knew I had to dig out old tools again for my renaming tasks.

Re: Help on renaming files

Posted: 30 Jul 2015 23:25
by highend
And even then it's still a challenge to code it right
Only when the requirements change with (almost every) posting. Regex is about pattern matching. So: The real task is to identify the correct patterns. The rest is just building the necessary regex (which isn't as complicated as it may look like).
this crazy complicated line is the ONLY way to get this thing renamed in XY?
I never said that :) You can combine them (and therefore make them easier) with other methods as well. It all depends on the actual case.
Crazy complicated? Mh. If you read such a line carefully you'll notice that it consists of patterns as well. Repeating patterns! The first three major parts of it could be put into a single pattern as well (but I was too lazy to think about it...) Should I rewrite it and build all $p<x> variables with small blocks that are easier to understand (and read)? *g*

Let me show you what the regex does:

Code: Select all

^([^,.-]+?)\s?(?:-|,|\.)\s?([^,.-]+?)\s?(?:,|\.)\s?([^,.-]+?)\s?(?:,|\.)\s?(\d{4})(.*?)(\[(DVD|BD|WEB-DL|WEB|HD|SAT|TV|VHS).*?])\.(.*)$
^ = at the beginning of the line
([^,.-]+?) = first capturing group
-> capture everything that is NOT a comma, a dot or a minus. At least one character must be present. be lazy (only capture as much as necessary)
\s?(?:-|,|\.)\s?
-> followed by an optional space
-> followed (?: = a non capturing group) by a minus, comma OR dot, followed by an optional space
These two capturing patterns are repeated two times after the first (to capture the spanish name, english name, director)
(\d{4})
-> capture the year
(.*?)
-> Optional stuff like "F. Astaire,G. Rogers,E. E. Horton"
(\[(DVD|BD|WEB-DL|WEB|HD|SAT|TV|VHS).*?])
-> Surrounded by []. Either of the leading types (DVD, HD, etc.)
\.(.*)
-> The last dot and the captured extension of the file
$
-> end of line

The other regexes are nearly the same only with small differences (e.g. no [] around the types, missing directory, missing english movie name)...

If you break it up into atomic steps it doesn't look that frightening, right?
That was the moment when I knew I had to dig out old tools again for my renaming tasks.
Show me one of your tools that renames all (at least 90% of all) posted examples with just one click. Some people don't want to do things in 3 - 5 steps per variation of the pattern -> I'm one of them :biggrin: Don't get me wrong. I've used renamers too (before I got into scripting with XY). But nowadays scripting a regex to let me do everything in one step (even if it takes a bit of time) is what I prefer.

Re: Help on renaming files

Posted: 31 Jul 2015 00:23
by Stef123
Don't get me wrong, either, highend
I do know - and appreciate - the value of RegEx. I am NOT saying it's a bad idea XY offers RegEX, but unlike with other tools it's not an advanced option here in XY, but the only option you have. Well maybe not always, you're right, it depends on the case at hand, in my case it was something like "delete position 4 - 8 from base name and put the string THIS WAS EASY at the end".

It completely threw me off about a year ago, last fall, I can't find the thread right now. But I remember it vividly, very embarrassing situation, I am the consultant brought in to optimize their workflow, and then I finish last with my renaming assignment. Stupid me had suggested we split up into groups, because I knew I had to look it up in XY help. The others searched the web or consulted help pdfs of image browsers.

I ended up doing it in SpeedCommander, although I hate it hate it hate it, but have to admit it saved my butt that time, it took just a few mouse clicks, no script, no RegEx, no lengthy research. Later I realized it's just as easy with TotalCommander or any of my old tools like BulkRename. Self-explanatory.

Thanks for the info. I still think it is VERY complicated, not to understand, but to get it right syntax-wise, and second, even more so when bug-tracking that line for mistakes. I know it makes sense when broken down into atomic segments, and I understand the rationale, and yet it takes lookups and many trials and dummy data to come up with that from scratch - for someone like me, that is.
highend wrote:Show me one of your tools that renames all (at least 90% of all) posted examples with just one click. Some people don't want to do things in 3 - 5 steps per variation of the pattern -> I'm one of them :biggrin:
There is no way I could have pulled this off with just one click, with none of my tools. But let me ask you for an honest answer as well: Are you able to type that line with just one attempt?

Re: Help on renaming files

Posted: 31 Jul 2015 00:43
by highend
But let me ask you for an honest answer as well: Are you able to type that line with just one attempt?
If I have the necessary samples to find the patterns (and deduct the logic from them)? Yes, sorry... But before you put it together you need a bit of time to think about a good way to build the capturing groups. So it's not: I write it down in 5 seconds. I have to think about it.

Really. It may look like hell but it isn't. Regex is putting simple things in context and build more complicated pattern matches from basic ones but in the end everything is (rather) logical. That stands for simple regexes (like the ones that were used for this case). Regexes can be far more complicated than that...

Do you speak more languages? English is not my mother language. Many people think in their mother language and translate every word inside their brain and then try to say the complete sentence in the new language. Imho it's easier to try to think "english", not by translating everything word by word. Hard to describe what I mean... Building patterns in regex works the same, or better: It get's easier the more you try to think "regex".

To show how the patterns work I've rewrote the script a tiny bit... Words are easier to read and understand (in this case: variable names) than the "wtf" regular expression notation.

Code: Select all

    $bol      = "^";                                 // Beginning of line
    $eol      = "$";                                 // End of line
    $ext      = "\.(.*)";                            // File extension
    $trash    = "(.*?)";                             // Optionally capture superflous infos
    $year     = "(\d{4})";                           // Year only
    $cBlock   = "([^,.-]+?)";                        // Capture block
    $delims   = "\s?(?:-|,|\.)\s?";                  // Not captured delimiters between blocks
    $types    = "(DVD|BD|WEB-DL|WEB|HD|SAT|TV|VHS)"; // Types of recording
    $typesWB  = "(\[$types.*?])";                    // In between square brackets
    $typesWoB = "($types.*)";                        // Without square brackets

    foreach($item, get("selectedItemsPathNames", "|")) {
        $nameDir = "";
        $path = getpathcomponent($item, "path");
        $fileName = regexreplace(getpathcomponent($item, "file"), "(\(|\))"); // Remove all parentheses

        // All components + format in []
        $p1   = $bol.$cBlock.$delims.$cBlock.$delims.$cBlock.$delims.$year.$trash.$typesWB.$ext.$eol;
        // All components + format (but not in [])
        $p2   = $bol.$cBlock.$delims.$cBlock.$delims.$cBlock.$delims.$year.$trash.$typesWoB.$ext.$eol;
        // Missing format
        $p3   = $bol.$cBlock.$delims.$cBlock.$delims.$cBlock.$delims.$year.$trash.$ext.$eol;
        // Year earlier, format in []
        $p4   = $bol.$cBlock.$delims.$year.$delims.$cBlock.$delims.$cBlock.$delims.$trash.$typesWB.$ext.$eol;
        // Year earlier, format (but not in [])
        $p5   = $bol.$cBlock.$delims.$year.$delims.$cBlock.$delims.$cBlock.$delims.$trash.$typesWoB.$ext.$eol;
        // Missing director, otherwise same as $p1
        $p1_1 = $bol.$cBlock.$delims.$cBlock.$delims.$year.$trash.$typesWB.$ext.$eol;
        // Missing director, otherwise same as $p2
        $p2_1 = $bol.$cBlock.$delims.$cBlock.$delims.$year.$trash.$typesWoB.$ext.$eol;

        if (regexmatches($fileName, $p1)) {
            $nameDir  = regexreplace($fileName, $p1, "$1 ($4)");
            $nameItem = regexreplace($fileName, $p1, "$1 ($4).($2).($3).$6.$8");
        } elseif (regexmatches($fileName, $p2)) {
            $nameDir  = regexreplace($fileName, $p2, "$1 ($4)");
            $nameItem = regexreplace($fileName, $p2, "$1 ($4).($2).($3).[$6].$8");
        } elseif (regexmatches($fileName, $p3)) {
            $nameDir  = regexreplace($fileName, $p3, "$1 ($4)");
            $nameItem = regexreplace($fileName, $p3, "$1 ($4).($2).($3).$6");
        } elseif (regexmatches($fileName, $p1_1)) {
            $nameDir  = regexreplace($fileName, $p1_1, "$1 ($3)");
            $nameItem = regexreplace($fileName, $p1_1, "$1 ($3).($2).$5.$7");
        } elseif (regexmatches($fileName, $p2_1)) {
            $nameDir  = regexreplace($fileName, $p2_1, "$1 ($3)");
            $nameItem = regexreplace($fileName, $p2_1, "$1 ($3).($2).[$5].$7");
        } elseif (regexmatches($fileName, $p4)) {
            $nameDir  = regexreplace($fileName, $p4, "$1 ($2)");
            $nameItem = regexreplace($fileName, $p4, "$1 ($2).($3).($4).$6.$8");
        } elseif (regexmatches($fileName, $p5)) {
            $nameDir  = regexreplace($fileName, $p5, "$1 ($2)");
            $nameItem = regexreplace($fileName, $p5, "$1 ($2).($3).($4).[$6].$8");
        }
        if ($nameDir) { moveas $nameItem, "$path\$nameDir", $item; }
    }

Re: Help on renaming files

Posted: 31 Jul 2015 22:35
by Stef123
And once again - thanks a ton highend,
there 's something irresistably motivating about your style of help teasers. I will give it a shot over the weekend. Renames seem to be a fitting subject for hands-on for RegEx lessons, I'll start with easy ones that I often use in dialog-operated managers and will try to re-create them in XY.

To be honest, in the past I would have filed it away under "reinventing the wheel", why bother when the same can be accomplished already with a few mouse clicks in checkboxes. But I am starting to see the value of these manual labors, RegEx belongs to the few topics I keep running into every so often.

Will keep you posted. Have a nice weekend.