Mass rename using "Regular Expressions and Find And Replace"

Hi forum and Dopus Support Team,

I have a few 100 files which all have a certain name portion at the end of the filename as follows:

[filename]~20200116-09-[minute-second].extension

I want to delete that ~20200116-09-[minute-second] part from the file name.

The part ~20200116-09- is always the same, and the part [minute-second] is different.

I suppose this works with "Regular Expressions and Find And Replace", using the "Rename" dialog:

What would I have to enter in the "Old Name" field to ensure that all name components are found and deleted according to the pattern ~20200116-09-[minute-second], regardless of the value of the [minute-second] part?

An asterisk or multiple question marks don't seem to work (please bear with my Regular Expression ignorance) :o

Many thanks for help!

You probably want just Regular Expressions (not the Find & Replace mode).

Old name: ^(.*)~20200116-09-.*
New name: \1

1 Like

Thank you very much Jon!

I tried that -- could it be that the Old name should read slightly different:
^(.*)~20200116-09-*

Only this seems to catch all wanted files?

I don't think so, but if it works great :slight_smile:

Why would you (not) think so?

Sorry, am asking out of complete RegExp illiteracy -- but don't want to break something :grimacing:

(actually, your syntax ^(.*)~20200116-09-.* doesn't catch any of the files :frowning: )

Even this seems to completely work:
^(.*)~20200116-09

This also works:
^(.*)~20200116-09*.*

(all with "Ignore extension" checked)

:thinking: :question:

Since you didn't provide an actual example filename and just described the format I had to make up the test names to use, but it seems to work to me:

I'm sorry, my fault. I didn't describe the format exactly enough (actually, I described it incorrectly). Sorry.

The filenames actually look like this, for example:

blahblah~20200116-093959.zip

The part ~20200116-09 is always the same, and I would like to rename them to

blahblah.zip

Take out the second hyphen.

1 Like

Yeah, that works:
^(.*)~20200116-09.*

Strangely (to me), these two seem to do exactly the same:
^(.*)~20200116-09.
^(.*)~20200116-09

Sorry, I'm aware that I'm just taking half an hour of time from a world-class software developer, with my dummie RegEx question.

Please tell me where I can donate. Solving this issue is just about to save my a** here :grimacing:

2 Likes

Bear with me -- I have another unexpected case where I have to rename some 1000 files having a similar filename pattern, where the datestamp has to be removed from the filenames.

The files look like this again:

Example:
BlahBlah~20200116-200937.zip

Pattern:
[Filename]~[yyyymmdd-HHMMSS].[extension]

The following simple regex seems to catch all files, and remove the complete part after and including the ~Tilde, while keeping the file extension:

Old name
^(.*)~
New name
\1


Do you guys also think this regex could work like this?

Thanks heaps for comments!

If it doesn't work, try ^(.*)~.* for the old name.

1 Like

Thanks very much!

Since I probably will need this method (deleting a suffix starting with a certain character string) in the file name) even more often in the future, would you (or some other reader) mind briefly explaining the syntax of ^(.*)~.* to me?

^ means anchor to the start of the string.
.* means match any number of characters. If it's in () it defines a capture group that can be used in the replacement string (e.g. \1 refers to the first capture group)
~ is a literal character that it's searching for.

So what it means is, starting from the start of the string, capture all characters up until the first ~ character.

1 Like

Thank you Jon. I appreciate your explanation very much!

I think I will be able to work out from here how to capture any filename suffix starting with a certain string.

Suppose I want to delete the date suffix from this file pattern:

BlahBlah-(2019-11-[dd-HHMMSS]).zip

Then I'd use this ^(.*)-(2019-11-.* for the search string, right?

Last question :blush:: why is the .* needed (or, used here) at the end of the search Regex? Does this have something to do with the file extension?

Then I'd use this ^(.*)-(2019-11-.* for the search string, right?

The parenthesis needs to be escaped. The leading ^ is not needed in RegEx Mode. So you would use (.*)-\(2019-11-.*.

Does this have something to do with the file extension?

No, the option Ignore extension is checked. In RegEx Mode the string needs to represent the entire filename. That's the main difference to RegEx+F&R.

Have a look at the regex explanations in the Opus help. Although it claims to be just an introduction it is very comprehensive and covers a lot of ground. You'll get good mileage from it.

1 Like

Whoa, thanks!

In this case, for the original task (remove filename date suffix starting with ~Tilde, like blahblah~20200116-093959.zip), already this syntax fully does it:

Old name
(.*)~
New name
\1

Correct? :open_mouth:

No. It doesn't get shorter than this:

2020-01-18 - 12.00.56 - Rename_-_Directory_Opus

Like in your question before and in my first posting. There I didn't remove the ^ because it's just a cosmetic thing and I didn't want to cause confusion.

Why don't you just try it out? It's free :wink:

2 Likes

Yeah I tried it, and this worked, on like blahblah~20200116-093959.zip:

Old name
(.*)~
New name
\1

:thinking::thinking::thinking: