#1 2009-02-24 22:41

dinosaur-jr
Member
Registered: 2009-02-24
Posts: 5

Rename "the man.jpg" to "Man, The.jpg"

Hey PascalScript Guru's,

First of all have to say: This program rocks a million and still it's free. You guys rock!
Need some help with 2 things.

1st: Can someone please tell me how to make a script to:

1) rename"the Negotiator.jpg" to "Negotiator, The.jpg" (" , The "after last character, but before extension)
2) rename file: "The Negotiator (1998).avi" to "Negotiator, The (1998).avi" (" , The "before year)
3) rename folder: "The Negotiator (1998)" to "Negotiator, The (1998)"

Or a renamer script that covers all of the above at once.
I don't know anything about pascalscript but i thought if the script could check if the first 4 characters are "the " Then delete them and add ", The" right after the last character before the ".fileextension", but only if the last 7 characters of the filename or foldername do not consist of " (xxxx)" else ", The" should be placed before those last 7 characters.

2nd: Can somewone tell me how to make a script to create a folder from a filename and afterwards move the file(s) into the corresponding folder(s)?
Example:
I have the following files in a rootfolder: 1.avi ,2.avi ,3.avi ,4.avi ,5.avi ,6.avi
I need the script to create subfolders in the rootfolder like: 1, 2, 3, 4, 5, 6
Afterwards the script needs to: move "1.avi" to subfolder "1", move "2.avi" to subfolder "2", etc.

Any help would be greatly appreciated.
Any Ideas?

Last edited by dinosaur-jr (2009-02-24 22:46)

Offline

#2 2009-02-25 03:08

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Rename "the man.jpg" to "Man, The.jpg"

Re. your 1st problem, PascalScript should not be used everywhere. If the pattern is fixed, RegEx can help easily.

Here replace
The (.*)( \d*)
with
$1, The$2
(for folders, in Filter settings, select 'Add folders as files').

However, this works only if a year exists within brackets. For just "The Negotiator.jpg", this doesn't work. I tried a string like
The (.*)(\( \d*\))?
to escape and make the brackets optional, but that didn't work. This page says (?:Value)? makes the brackets optional ("The question mark and the colon after the opening round bracket are the special syntax that you can use to tell the regex engine that this pair of brackets should not create a backreference.") But apparently this syntax doesn't work in ReNamer. sad Hope someone with RegEx experience can help...

For the 2nd, I think something similar was discussed sometime back in the forum. Try searching, and if nothing is found, we can solve this issue here as well.

Last edited by Andrew (2009-02-25 03:10)

Offline

#3 2009-02-25 03:16

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Rename "the man.jpg" to "Man, The.jpg"

Hi and welcome to our community wink

1st: Can someone please tell me how to make a script to:

1) rename"the Negotiator.jpg" to "Negotiator, The.jpg" (" , The "after last character, but before extension)
2) rename file: "The Negotiator (1998).avi" to "Negotiator, The (1998).avi" (" , The "before year)
3) rename folder: "The Negotiator (1998)" to "Negotiator, The (1998)"

Perhaps script will be better but here is 'temporary' RegEx solution:

1) Expression: "(.+)\s(.+)" [x] skip extension
   Replace: "$2, $1"

2) Expression: "(.+)\s(.+)\s(.+)" [x] skip extension
   Replace: "$2, $1 $3"

3) Expression: "(.+)\s(.+)\s(.+)"
   Replace: "$2, $1 $3"

Note: Tested for submitted examples.

Look here for RegEx examples and more help how to use them.
Maybe krtek's explanation about switching words can clear some thoughts.

About your 2nd: question I cannot help you because I don't know Pascal...

Cheers wink


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#4 2009-02-25 04:01

prologician
Member
Registered: 2009-01-30
Posts: 84

Re: Rename "the man.jpg" to "Man, The.jpg"

After some fiddling with er@ser's answers, I cooked up a unified answer for all three of your test examples:

Expression: (\w+)\s(\w+)\s?(\((\d+)\))?
Replace: $2, $1 $3
(Skip extension is checked)

Note, it's not 100% perfect.... you'll want to put on a Clean Up filter after this, to remove the space-character at the end, in certain situations.

As far as your second question.... that seems quirky, and somehow dangerous.... a folder for every file? hmm I can't envision why you'd want this.....

Take a look at the functions WideExtractBaseName() and WideCreateDir(). Mind you, that's not everything that you need, you still need to actually move the file. But I leave this as an exercise to the reader. wink

Offline

#5 2009-02-25 07:56

dinosaur-jr
Member
Registered: 2009-02-24
Posts: 5

Re: Rename "the man.jpg" to "Man, The.jpg"

First of all thank you for all of your speedy replies.

To explain why i need to create seperate folders, here's my situation:
My computer is set up as streaming media server for XBox Media Center (greatest media center in the world).
XBMC uses smb shares to access the shared media files on my computer (TV Series, Movies, etc.)
My movies (avi files, Subtitle files and Thumbnail files) are in a seperate folders for each movie.

For those not familiar:
XBMC when browsing these folders, takes the folder.jpg (per movie subfolder) and displays only the thumbnail and title for that movie. When clicking the thumbnail it displays the movie title without the extension, and if there multiple avi's it automaticaly stacks the files.
Example:
Negotiator, The (1998) - CD1 and Negotiator, The (1998) - CD2 are displayed as just : Negotiator, The (1998)
If there are subtitles (.srt, .idx,.sub) in that folder (with the same name as the avi file) it automatically plays subtitle when starting the movie. Although only video files are displayed in the media center video library.
So in short each subfolder represents a movie title, thus the need to create seperate folders.

As for my 1st problem: Moviestitles starting with "The", "A" & "An", i always put those words at the end for easy indexing.

I have tons of movies which need to be sorted out like that and ReNamer had already helped me a lot with most movies, but recently i came accross the pascalscript rule, but i'm stuck because i have absolutly no experience with that.

Have to go to work now, but will try suggestions when i get home and let you know.

Thanks again, all

PS: Don't know if any exists, but wouldn't it be great if someone created a repository page for all standard ReNamer pascal scripts or regular expressions discussed in the forum over all this time?

Offline

#6 2009-02-25 18:29

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Rename "the man.jpg" to "Man, The.jpg"

Hi, dinosaur-jr
Regarding your first problem...
You can use this regex with option "skip extention" enabled and 'Add folders as files' selected in filter settings.

Expression: "(the|an|a)\s(.+)(\s\(\d{4}\))|(the|an|a)\s(.+)"
Replace: "$5$2, $4$1$3"

It should work for all your needs. And it does everything in one step. Except setting up "case" of the|an|a...
You can't change letter case with regex (at least in ReNamer), so if you wan't to have the case specified you have to change it first, eg. adding regex rules like this:
Expression: "^the "
Replace: "The "
[x] case sensitive


Edited by krtek on 25.02.2009 at 20:27
It might be also achieved with a bit simpler regex:
Expression: "(the|an|a)\s(.+?)($|\s\(\d{4}\))"
Replace: "$2, $1$3"
[x] skip extention



Andrew wrote:

(?:Value)? makes the brackets optional

Unfortunately, third party Regex engine, which is used in ReNamer is limited to the standard functionality and does not support any of "(?:" tags or any other fancy options. You can find a bit more about this in this topic.
But it is not the lack of the "non-capturing grouping" that made your regex "The (.*)(\( \d*\))?" fail. It's the concept of making brackets optional (cause they are optional in that expression) combined with greediness of the "*" (and "+") signs.
Regex tries to match "The (.*)" and (as * is greedy) it MATCHES full filename. Then regex check's if the rest of expression is also matched and as (\(\d*\))? is optional.... it is MATCHED (in "no present" state). And then regex happily finishes its work. I hope you'll understand what I mean...




Regarding your second problem.
You don't need PascalScript here as well.
Use regex rule:

Expression: "(.+)"
Replace:  "$1\\$1"
[x] skip extention

This will move all the files with the same basename to the subfolder of that name (and create that folder if necessary), eg.
1.avi, 1.txt,    to   \1\1.avi, \1\1.txt

If you want to move only files with specific extentions (eg. avi and mpg) use that regex:
Expression: "(.+)\.(avi|mpg)"
Replace: "$1\\$1.$2"
[ ] skip extention (DISABLED)

Voila! Done!


Ps. PascalScript would be nessecary if you would rename folder according to the filename of the file inside that folder, eg.
C:\Folder\1.avi     to become    C:\1\1.avi
Firstly, I thought that's the problem and I've even rearranged the code given by Dennis in here.
If you would need something like that, just check that topic.



Enjoy

Last edited by krtek (2009-02-26 16:37)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#7 2009-02-25 20:14

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Rename "the man.jpg" to "Man, The.jpg"

krtek, thanks for the explanation! I got a little of what you wrote about greedy quantifiers... but have to read up to understand more! BTW, if (.*) eats up the whole string till the end, then why doesn't (.*?) work? Is that not non-greedy?

I see that you've given an OR condition to deal with the specific case of there being no year specified. So there's no way with Renamer's RegEx engine to say that if the year exists, then (.*) or (.*?) should eat till before the year, but if the year doesn't exist, then it should eat till the end of the string?

Offline

#8 2009-02-25 20:23

dinosaur-jr
Member
Registered: 2009-02-24
Posts: 5

Re: Rename "the man.jpg" to "Man, The.jpg"

Though i really appreciate everyone's sollutions and tested all the above solutions i actualy got exacty what i wanted from the regex code from krtek.

Thank you krtek! wink

Have to say all the other solutions, also came close but not quite what i wanted.
e.g. prologican's solution:

Expression: (\w+)\s(\w+)\s?(\((\d+)\))?
Replace: $2, $1 $3
(Skip extension is checked)

Did the following:
The - "The Negotiator" would become "Negotiator, The" but "The Red Baron" would become "Red, The Baron" and not "Red Baron, The"
and
An - "An Officer And A Gentleman" becomes "Officer, An A, And Gentleman"

krtek's solution:

Expression: "(the|an|a)\s(.+)(\s\(\d{4}\))|(the|an|a)\s(.+)"
Replace: "$5$2, $4$1$3"

Does exactly what i wanted with the characters "The, An & A" at the beginning of a file/folder name and in my case doesn't add it to the end, but before the year (This code should be on the regex example scriptlet page! or maybe even a standard rule in a future versions of the program if you ask me).

krtek's sollution does the following:
An - "An Officer and a Gentleman (1982)"  becomes "Officer and a Gentleman, An (1982)"
and
The - "The Negotiator (1998)" becomes "Negotiator, The (1998)"
and
A - "A Few Good Men (1992)" becomes "Few Good Men, A (1992)"

As for the 2nd problem
I'd like to apoligize to everyone for even considering that a renaming program should be able to create folders and even move corresponding files into each folder.

Anyways for anyone interested in a solution to the second problem, i found 2:
1) There's a free program called: "Folder Creator" by Tony Reinli

  • Creates folders from a list of files found in a folder

  • Creates folders from a text file (list of name)

*2) Found a .bat script solution that does exactly what i want at http://www.experts-exchange.com/Program … 12235.html

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET FilesPath=C:\My Movies
IF NOT EXIST !FilesPath! ECHO Invalid Path "!FilesPath!" &EXIT /B 0
FOR /F "delims=" %%f IN ('DIR /A-D /B /ON "!FilesPath!"') DO (
    @ECHO Processing: %%f
    SET FolderName=
    SET FolderName=!FilesPath!\%%~nf
    IF NOT EXIST "!FolderName!" MD "!FolderName!"
    IF EXIST "!FilesPath!\%%f" MOVE /Y "!FilesPath!\%%f" "!FolderName!")
 
EXIT /B 0

Copy and paste following code into notepad and save it with any name having .cmd extension.
You need to set path (FilesPath variable) to the folder where all movies exist.

Hope this helps!
Farhan

So my problems are herby SOLVED!!:cool:

A BIG THANKS AGAIN TO EVERYONE:)

Dino

Last edited by dinosaur-jr (2009-02-25 20:29)

Offline

#9 2009-02-25 20:58

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Rename "the man.jpg" to "Man, The.jpg"

dinosaur-jr wrote:

As for the 2nd problem
I'd like to apoligize to everyone for even considering that a renaming program should be able to create folders and even move corresponding files into each folder.

Well, ReNamer DOES that. And it does that in very simple way. See my post above. One regex and it's done.


Andrew wrote:

I see that you've given an OR condition to deal with the specific case of there being no year specified. So there's no way with Renamer's RegEx engine to say that if the year exists, then (.*) or (.*?) should eat till before the year, but if the year doesn't exist, then it should eat till the end of the string?

There is another solution. I was wondering for how to do that for almost the whole evening, and ended up with these simple regexes:
Expression: "(the|an|a)\s(.+?)($|\s\(\d{4}\))"
Replace: "$2, $1$3"
[x] skip extention
If it will find a year it will use it, otherwise it will end only when it finds end of string ($).

or even (this one was considered in the explanation below):
Expression: "(the|an|a) (.+?)(\s\(\d{4}\))?$"
Replace:"$2, $1$3"
[x] skip extention
This one will try to match as little as it is possible with +, but it have to end match only when the end of filename ($) is reached.

I guess they both do what you wanted.


Andrew wrote:

if (.*) eats up the whole string till the end, then why doesn't (.*?) work?

It doesn't work because "?" makes the "*" (or "+") LAZY.
I'll use "+" for better view (as it have to match at least one sign).
So if we have
regex: "The (.+?)(\s\(\d{4}\))?",
replace part: "$1, The$2"
and filename: "The Negotiations with enemy (1998)"
regex ends up after matching "The N" and it is happy. It did its work. Pattern matches. And it replaces ONLY the "The N" part of the filename (the rest remains untouched).
"The N"   ->  "N, The"     so you get "N, Theegotiations with enemy (1998)"
Furthermore, if the filename was "The Negotiations with the enemy (1998)", then regex would match twice! First "The N" would be replaced with "N, The" and then ReNamer applies that regex to the rest of the string ("egotiations with the enemy (1998)"). And it finds another match "the e" and replaces it with "e, The". So now we got output that is really hard to understand at first sight: "N, Theegotiations with e, Thenemy (1998)"

The simple trick is to order regex not to stop before reaching the end of string. So regex:
"The (.+?)(\s\(\d{4}\))?$" will do the trick.
It goes like that:
"The (.+?)" part of regex matches "The N" but then it can't reach the end of string ($) even with (\s\(\d{4}\))?
so it takes another sign and now it matches "The Ne"... Still no chance to reach end of string.
So it takes char by char till the moment it matches "The Negotiations with enemy". Now it has two choices:
1st: there is a year - then (\s\(\d{4}\))? will match it
2nd: there is end of filename - then (\s\(\d{4}\))? will match nothing and $ will match the end of file.
In both cases we will get correct output.

I have no idea if I were clear... If you'll have problems, just ask.

Last edited by krtek (2009-02-26 09:07)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#10 2009-02-26 18:31

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Rename "the man.jpg" to "Man, The.jpg"

Hmm... It'll take some time for all of that to sink in! Thanks for the help anyway, and I'll ask you for clarifications if needed (maybe in a separate RegEx topic). smile Best thing of course will be to try all the permutations out for myself to see what each one does, in order to understand it better.

Offline

Board footer

Powered by FluxBB