Rename with Numbers

I'm often faced with the following problem:

Let's say that I have 20 files and each one is prefixed with a number: 01 xxxxx, 02 xxxxxx, 03... etc. upto 20 xxxxx.

All I want to do is delete the leading 2 char number. How do I enter that in the rename function to recognize the first 2 characters?

Try this:

Look where I have the cursor in the image above, there is a space character to its left. As I understand your question, you have a space between your two-digit numbers and the rest of the filename. If this is not the case, delete that space

Yes, that worked (thanks).

Are all of the codes that you referenced part of the "pattern matching" in DOpus? I had tried searching the manual but couldn't find anything.

Ken, just so you know... I believe the example you gave will strip ANY leading 2 characters from the filename as long as a space appears after the first 2 chars. Using a ([0-9]) expression should restrict it to JUST numeric chars... like so:

So in total:

'Old Name': b([0-9]) (.*)[/b]
'New Name': \3

And Nate... yes, these codes are part of pattern matching and regular expression syntax in Opus. The online (F1) help goes into this a bit... I haven't checked the manual personally.

Search the Help and manual for "Regular Expression Syntax". and be prepared to do some head scratching. It's powerful, but it can take some time to get the hang of. Which is why the developers have included that awesome Preview window, so you can see what is happening. (The Undo feature can also undo an advanced rename as well.)

The exact same operation could also have be done using this expression:

Old Name: (.)(.) (.*)\.(.*) New Name: \3.\4

Let me break down this example. Here's what The Old Name expression means:[ul][li] (.) = Any single character.
In this case, we used two of these, one for each numeric digit.[/li]
[li] We typed a single space character, because the files contain a space after the numeric portion.[/li]
[li] (.) {first instance} = Search for any and all characters, up to the next term in the expression.

In the screen grab in my first post, we didn't use any subsequent term in the expression, so (.
) was resolved to mean the remainder of the file name, to include the the dot and the file extension. However, in the Code box above, I am looking for the file name, then a dot, and then the file extension as three separate terms. The dot is the next term after the (.), so in the codebox expression (.) resolves to the file name only, without the dot and extension.
[/li]
[li] . = This is looking for an actual dot in the file name. The dot character is itself a regular expression character (it is viewed by search as any single character). Preceding it with a slash means to escape the special meaning of a dot, and actually look for it as a character.[/li]
[li] (.) {second instance} = Again we search for any and all characters, after the previous term and before the next term in the expression.

The previous term was the ., and there is not a next term, so this second instance of (.
) will parse file extensions of any character length.[/li][/ul][quote="The authors of the DOpus Help File"]The parenthesis, besides affecting the evaluation order of the regular expression, also serves as tagged expression which is something like a temporary memory. This memory can then be used when we want to replace the found expression with a new expression.[/quote]
That means that each term enclosed inside parenthesis in The Old Name can be used in The New Name. In my screen grab in my first post, we had three such parenthesis-enclosed terms. The first two where the first and second digit of the number you wanted to discard, the third was the file name, dot, and extension that you wanted to retain. So we used \3 as the New Name to retain the third tagged expression.

In the Code Box expression above, I have parsed the expression differently, I have isolated the Filename, the dot, and the file extension. So now we have four such parenthesis-enclosed terms. We want to use the third and fourth term, this time separated by a dot (because the dot from the Old File Name is no longer included in any tagged expression).

I thought it might help to see two different examples of what you are trying to do, so you get some understanding of how Regular Expression works. It is fairly tricky when you are new to it.

Another tricky concept is looking for files of certain extensions. Say in the Code Box example above you only wanted to rename those files that had a .bmp or a .jpg file extension. You would change the second instance of (.*) to (bmp|jpg). If you wanted to also perform the rename on .png files you would use (bmp|jpg|png) , and so on.

My advice is to create a folder with some files you can delete without consequence, and to play around and see how it works.

[quote="steje"]Ken, just so you know... I believe the example you gave will strip ANY leading 2 characters from the filename as long as a space appears after the first 2 chars. Using a ([0-9]) expression should restrict it to JUST numeric chars...

So in total:

'Old Name': b([0-9]) (.*)[/b]
'New Name': \3

And Nate... yes, these codes are part of pattern matching and regular expression syntax in Opus. The online (F1) help goes into this a bit... I haven't checked the manual personally.[/quote]

That's a good point Steje, I have personally never used that syntax, but I'm very glad you brought it up.

The Help file and Manual both have this information. However, I believe that examples displayed are fairly basic, and do not display even a portion of the possibilities. Having a coding background helps for understanding this feature.

While I'm testing this, I found that the following expression would work for files that were prefixed with a series of number, regarless of how many.

b (.*)
\2[/b]

Before:
01 file 1.txt
2 file 2.txt
0333 file 333.txt

After:
file 1.txt
file 2.txt
file 333.txt

correct me if I'm wrong but I guess the logic is that until it finds a space, it will delete any numbers in the string.

Hmmm... confirmed and not what I expected. How about that - I chimed in with an 'incorrect' 'correction'. Sorry Ken...

Calling JohnZeman - help!

This happened because the expression

([0-9]) (.*)

does not specify exactly where to begin looking for a match in a file name. What the above expression does look for is a number somewhere in a file name which precedes a space. And only what follows that space will be used as the new file name. That's not being not very specific eh?

To explicitly look for one or more numbers at the beginning of a file name with RegEx, start the expression with a ^

Note the ^ must be before any parenthesis.

Try this expression to match file names which begin with one or more numbers.

Old name
^[0-9]+(.*)

New name
\1

If there is a space between the leading numbers and the rest of the file name, and you do not want that space, use this for the old name expression

^[0-9]+ (.*)

EDIT: Disregard this post, these expressions don't work either!

Apply every technique I showed you in my previous post to arrive at this syntax:

Old Name: (0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9) (.*) New Name: \3

Or this One:

Old Name: (0|1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9) (.*)\.(.*) New Name: \3.\4

Both will rename only those files prefixed by two-digit numbers and a space. This has been shown to be incorrect, as it too will rename

007 File Name.txt

to

File Name.txt

Which isn't what is wanted.

Wow, I never knew this about the codes, and I use them all the time! Of course, I've never tried to isloate just the two-digit numbers. My First post still works, so long as you have not selected any that start with a two-letter word.[/b]

Ken, with respect... it looks like JohnZ has the right of it. Your last example is the same as mine - but with the pipe symbols used to create a multi "or" construct that does the same as the range I used in the beginning.

So... if Nate wants to to process ONLY files that 'begin' with 2 digits and then a space... he will be ok with:

Old name:
^([0-9])([0-9]) (.*)

New name:
\3

The online (F1) help illustrates JohnZ's explanation of needing to use the "^" symbol to match the expression that follows it to the 'beginning' of the strings... Forest for the trees. Thanks John... nice save.

[quote="JohnZeman"]To explicitly look for one or more numbers at the beginning of a file name with RegEx, start the expression with a ^

Note the ^ must be before any parenthesis.

Try this expression to match file names which begin with one or more numbers.

Old name
^[0-9]+(.*)

New name
\1

If there is a space between the leading numbers and the rest of the file name, and you do not want that space, use this for the old name expression

^[0-9]+ (.*)[/quote]

These expressions also will rename any of the following files:
[ul]
[li] 0 File 111 Name.txt[/li]
[li] 1 File 111 Name.txt[/li]
[li] 02 File 111 Name.txt[/li]
[li] 03 File 111 Name.txt[/li]
[li] 40 File 111 Name.txt[/li]
[li] 50 File 111 Name.txt[/li]
[li] 40000 File 111 Name.txt

to
[/li]
[li] File 111 Name.txt[/li][/ul]

(without or without the space of course)

It'sthe carret that JohnZ informed of that is relevant:

Old name:
^([0-9])([0-9]) (.*)

New name:
\3

Done.

Okay, I got it now. I was using his whole expression.

Okay, I get that the caret dictates a search at the start of the file name.

Here's what I don't get.

There is no way to lock down the character position of numeric sequence when the sequence is not at the beginning or end of the string?

Thus, in the string below 123 could not be isolated as a term, 007 could with the ^, and 999 could with the $. But there seems to be no way to isolate 123, everything I try gets me 456. Is this the same for you?

007 File 123 Name 456 Fun 999.txt

in other words rename the file above to:

007 File Name 456 Fun 999.txt

Assuming that there are 100 such files Where "File Name" and "Fun" change from file-to-file and the numbers as well (though not the length of the number sequence).

The key thing you said in your question Ken was that very last statement. For that's the idea of regular expressions, to look for a specific pattern in the text you want to find (then change). Here is a simplistic answer to your question of how to change this file name:

007 File 123 Name 456 Fun 999.txt

To this

007 File Name 456 Fun 999.txt

Old name
^(........) [0-9][0-9]0-9

New name:
\1\2

In the old name, each . represents one character. The above expression looks for 8 any kind of characters followed by a space and three numbers, followed by another space then 20 any kind of characters.

The same expression could be written in a much more precise way like this:

Old name:
^([0-9][0-9][0-9] [A-Z][a-z][a-z][a-z] )[0-9][0-9][0-9] ([A-Z][a-z][a-z][a-z] [0-9][0-9][0-9] [A-Z][a-z][a-z] [0-9][0-9][0-9]..*)

New name:
\1\2

Where we specifically state what type of character each must be in the file name in order to get a match. To match the above expression a file name must:

Begin with three numbers
Followed by a space
Followed by an uppercase alphabet letter
Followed by three lowercase alphabet letters
Followed by a space
Followed by 3 numbers
Followed by a space
Followed by an uppercase alphabet letter
Followed by three lowercase alphabet letters
Followed by a space
Followed by 3 numbers
Followed by a space
Followed by an uppercase alphabet letter
Followed by two lowercase alphabet letters
Followed by a space
Followed by 3 numbers
Followed by the file extension

Note the Opus "Case sensitive" option must also turned on for the last expression to match.

So a RegEx can be very simple or very complex. Which should you use? Well I make that decision based upon the other files in the same directory which I do not want to change. If only the files I want to rename begin with say, 2 numbers, then I can use a simple RegEx to do that. However if there are selected files in that same directory which have a similar but not quite same name as the ones I want to change, then my RegEx must become more precise to specifically target the files I do want.

John,
I appreciate your efforts and enthusiasm. However, I wish you hadn't gone through all that trouble just to tell me everything that I already clearly demonstrated an understanding of, in my earlier posts above! :open_mouth:

I think you may have mis-interpreted what I was making an observation of. Perhaps because I wasn't clear enough? :confused:

So for instance there are files with names of dynamic length that contain number sequences of static length. Take a look at the examples below.

Inputs:[ul][li] 0 A File 012 With a Name 051 Fun 719.txt[/li]
[li] 006 Some File 672 With another Name 657 Not so much Fun 779.txt[/li]
[li] 02 Some Other File 232 Yet another name 253 Fun for all 739.txt[/li]
[li] 03 Yet Another File 342 Some Name 354 All for Fun 749.txt[/li]
[li] 080 File Me 892 Name Me 859 Funny 799.txt[/li]
[li] 1 File away 122 My Name 152 Funk 729.txt[/li]
[li] 40 File File File 452 Name Name 455 Fun no more 759.txt[/li]
[li] 50 File cabinet 562 Name of drawer 556 Fun for the whole family 769.txt[/li]
[li] 700 File your nails 782 Name your dog 758 Fun with friends 789.txt[/li][/ul]Desired Outputs:[ul][li] 0 A File With a Name 051 Fun 719.txt[/li]
[li] 006 Some File With another Name 657 Not so much Fun 779.txt[/li]
[li] 02 Some Other File Yet another name 253 Fun for all 739.txt[/li]
[li] 03 Yet Another File Some Name 354 All for Fun 749.txt[/li]
[li] 080 File Me Name Me 859 Funny 799.txt[/li]
[li] 1 File away My Name 152 Funk 729.txt[/li]
[li] 40 File File File Name Name 455 Fun no more 759.txt[/li]
[li] 50 File cabinet Name of drawer 556 Fun for the whole family 769.txt[/li]
[li] 700 File your nails Name your dog 758 Fun with friends 789.txt[/li][/ul]Restated Observation:
In the Inputs list above you will see that each file contains two internal three-digit number sequences. (For this exercise, number sequences at the beginning and end of the file name, are merely obstacles to search around; they are part of the "keeper text" that will comprise the new file name.) The internal number sequences all have the same length, but the text between them does not. Thus, stringing together a bunch of dots isn't going to help, so one must use the (.*) term. However the length of the number sequences is static in this case. The problem is that there are two such internal number sequences, and Directory Opus always seems to find the last occurrence of " ([0-9])([0-9])([0-9]) ". Thus, isolating an internal number sequence of known length and a known relative position appears impossible.

In this scenario, the user wants to be able to isolate (and discard) the first internal three-digit number sequence. All of my attempts to accomplish this, so far, have discarded the second internal three-digit number sequence instead. There seems to be no way for Directory Opus regular expression to say: "After any number of characters at the beginning of the string, find the first three numbers that are surrounded by a single space on either side and remove them."

Can you confirm that this is impossible?

Thanks for your efforts. (I hope this is more clear now.)

[quote]"After any number of characters at the beginning of the string, find the first three numbers that are surrounded by a single space on either side and remove them."

Can you confirm that this is impossible? [/quote]

Well I'll certainly grant you that it's a challenge Ken, however I don't think it's impossible. The challenge comes because Opus apparently looks for matches starting from right to left in each file name. In other words it would be relatively easy to write a RegEx to extract the LAST 3 digit number surrounded by spaces before the file extension, this would do it:

Old name:
^(.+ )[0-9][0-9][0-9] (.*)

New name:
\1\2

However that's not what you wanted to do, in effect you wanted to do the opposite and find the first 3 digit number surrounded by spaces. The challenge is further complicated because your example file names are followed by one or more other 3 digit numbers and text followed by the file name extension.

Yup, I admit you had me scratching my head on that one. However the RegEx below seems to do the trick on your examples. I know it's not perfect for any possible file name so you wouldn't have to try very hard to find an example file name where it would fail.

Old name:
^([0-9|a-z]+)([^0-9]+)( [0-9][0-9][0-9])(.*)

New name:
\1\2\4

I guess my basic point is it can be done however one would have to be pretty precise in what the actual existing file name patterns are before writing the RegEx.

I also have to confirm your other comment last time, that I hadn't spent a lot of time studying your previous observations. Yesterday morning when I answered you last time I was a long ways from home in a motel room after a nasty night of thunderstorms which had knocked out the motel's internet access. The next morning by the time it was restored I only had a short while to catch up on emails and messages before I had to go meet a client for the rest of the day. So I scanned this thread quickly looking for the current crux of the matter and obviously missed it. Sorry bout that.

Wouldn't it be easier to just type in the following???

OLD: 123
NEW: **

Not really Nate... the idea in this thread was using expressions to allow for more than just a static pattern. At first just 2 digits at the beginning of the filename (variable numbers not just '123') and then snowballed into a discussion about controlling just how to match digits (again variable values - not a static '123') in different places within the filename...