"Swapping" part of the filename

Post any Bulk Rename Utility support requirements here. Open to all registered users.

"Swapping" part of the filename

Postby belfix » Thu Jun 10, 2010 6:08 pm

Hi
Can this be done please and if, how?
Got a few thousand files that lets say the file name consists of:-
4digityear<space>firstname<space>lastname<space>birthyear
where <space> is actualy a space character and the firstname and lastname fields are variable length.
What I need to do is rename them to a format such that the firstname replaces the
lastname and the lastname replaces the first name. The file extension would remain the same.
Hoping quite simples?
regards
belfix
 
Posts: 3
Joined: Thu Jun 10, 2010 5:58 pm

Re: "Swapping" part of the filename

Postby Stefan » Thu Jun 10, 2010 8:09 pm

Only possible if "firstname" didn't contain any <space>

Try an regex search&replace:

Code: Select all
Name:  4digityear<space>firstname<space>lastname<space>birthyear
Match: (\d\d\d\d)<space>   (.+)  <space>  (.+)  <space>  (\d+)
Group:    \1                \2             \3             \4



RegEx(1)
Match: (\d\d\d\d) (.+) (.+) (\d+)
Repla: \1 \3 \2 \4

or

Match: (\d\d\d\d) (.+?) (.+) (\d+)
Repla: \1 \3 \2 \4

belfix wrote:Hi
Can this be done please and if, how?
Got a few thousand files that lets say the file name consists of:-
4digityear<space>firstname<space>lastname<space>birthyear
where <space> is actualy a space character and the firstname and lastname fields are variable length.
What I need to do is rename them to a format such that the firstname replaces the
lastname and the lastname replaces the first name. The file extension would remain the same.
Hoping quite simples?
regards








General notes/ Disclaimer

Hope this helps ? :D

Please note:

* Test my solution first with some test files before you destroy your data.

* It's always an good idea to provide all possibilities of file name pattern in question.
* That would give the supporter an change to do it right ;)
* If your real file names doesn't fit into your example pattern my solution may fail.

* Don't use this ' ' or " " -quotes from my explanation. They are only for clarification.
* '?' means non-greedy: stop at first match, instead of last possible.
* This (...) parenthesis are used to "group" what is found by this RegEx inside the ( )'s
  to reuse this capture later as replacement by using \1 or $1.
* Instead of ~ -signs, if used in my explanations, type an space/blank.


RegEx is an pattern matching solution, so all your files have to fit into the same pattern.
If they not, you have to separate them and run some more actions against them.

To find your own solution you have to virtual (in mind) split your file names/strings into parts
following the rules of the regular expression syntax, see the help file coming with your application.
(Please note that there are several flavors of RE engines and also different implementations into apps
and even different ways of doing or thinking, so your experiences may differ from my explanation)
Once you have split your string into parts you can decide which to use into replacement by grouping the pattern
into (...) parenthesis to which you can refer by using "\1" or "$1" signs later, or which to drop and which to modify.


See this both oldest threads in the "Regular Expressions" forum for an RegEx syntax overview:
=> Getting Started: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=5
=> Go ahead: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=27
There you will find more examples and tips. Read other threads in the "Regular Expressions" sub-forum too.

More Help
* online tester:
- http://rereplace.com/
- http://www.regextester.com/
- http://www.regexlib.com/RETester.aspx

* online help:
- www.regular-expressions.info
- www.regexlib.com/
- www.regexlib.com/CheatSheet.aspx
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: "Swaping" part of the filename

Postby belfix » Fri Jun 11, 2010 10:05 am

Thanks for the response. The regexp(1) worked a treat - what a terrific utility once you've been guided in the right direction. Just a slight "extra"...what if after the birthyear there was a possible -not in all cases - <space> followed by some text before the file extension. What extra parameter would I need?
:roll: If not possible, no matter as what is supplied is "just what the doctor ordered!"
belfix
 
Posts: 3
Joined: Thu Jun 10, 2010 5:58 pm

Re: "Swaping" part of the filename

Postby Stefan » Fri Jun 11, 2010 8:56 pm

belfix wrote:Thanks for the response. The regexp(1) worked a treat - what a terrific utility once you've been guided in the right direction.

No matter, great it works!
belfix wrote:Just a slight "extra"...what if after the birthyear there was a possible -not in all cases - <space> followed by some text before the file extension. What extra parameter would I need?
:roll: If not possible, no matter as what is supplied is "just what the doctor ordered!"

You know already that we have matched

e.g. "lastname" by RegEx ".+" which means for the dot. "match any sign" and for the plus+ "one-ore-more-of-the RegEx right before",
that is combined "one-or-more of any sign"

and "birthyear" by using RegEx "\d+" which means "one-ore-more of any digit"

Now lets combine the knowledge to search as last part for "\d+.*" ---yes, this time an star *---
which means as before "\d+" "one-ore-more of any digit" followed by non-or-more of any sign.

As i posted above take an look into the oldest thread in the regex sub-forum to get an overview what all this dots, plus and stars means :D





So the RegEx for your new request would be

Match: (\d\d\d\d) (.+?) (.+) (\d+.*)
Repla: \1 \3 \2 \4

which means: match
"4 digits" / "space" / "non-greedy one-or-more of any sign" / "space" / "one-or-more of any sign" / "space" / "one-ore-more digits followed by nothing, one-or-more of any sign"

so this should work for both
4digityear<space>firstname<space>lastname<space>birthyear
and
4digityear<space>firstname<space>lastname<space>birthyearChars

.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: "Swaping" part of the filename

Postby belfix » Mon Jun 14, 2010 7:21 am

Hi Stefan
Thanks once again for your guidance and and again, what a truely super utility.
best regards

:shock:
belfix
 
Posts: 3
Joined: Thu Jun 10, 2010 5:58 pm


Return to BRU Support