Change of substring in file names

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Change of substring in file names

Post by *DruidCtba »

Hey guys, I'm not sure if this is possible, but I need to rename 140 files in bulk, like this:

File name example - The Power of Tableau's Geographic and Image Roles #Tableau Course #125.mkv

I need to change them to a sequence like:

FROM

The Power of Tableau's Geographic and Image Roles #Tableau Course #125.mkv

TO

125 The Power of Tableau's Geographic and Image Roles.mkv

I can use regex (regular expression) to clean up (clear option) the end of the file that is like - #Tableau Course #, using the regex command " #Tableau Course #\d\d\d", but I don't know how to put this final number at the beginning of the file name, in the example above it would be 125:

The Power of Tableau's Geographic and Image Roles.mkv

Any ideas, is this possible?

If it is possible, since there are 140 files, the numbering goes from 1 to 140, could it be something like 001, 003, ..., 010, ..., 099, ..., 140?

Thank you very much for the help.

If it's not possible, is there any program that can do this?

Regards,

DruidCtba.
User avatar
white
Power Member
Power Member
Posts: 4626
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Change of substring in file names

Post by *white »

DruidCtba wrote: 2024-04-21, 22:36 UTC I can use regex (regular expression) to clean up (clear option) the end of the file that is like - #Tableau Course #, using the regex command " #Tableau Course #\d\d\d", but I don't know how to put this final number at the beginning of the file name, in the example above it would be 125:
Search for regex "(.*) #Tableau Course #(\d\d\d)"
Replace with "$2 $1"
User avatar
nsp
Power Member
Power Member
Posts: 1813
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: Change of substring in file names

Post by *nsp »

For all the Search & replace expression:
- tick [RegEx] only
- Do not tick with extension [E]

If the # is the Marker you want to use, and exactly 3 digits @white expression will wok fine.
If you want to have less constraints and use a replace string for the complete name, you can use:

Code: Select all

Search for:^(.*)#.*#(\d+)$
Replace with:$2 $1
To do the re-numbering, you need to be back to normal panel and order the files as you need then you can re select all then:
in the name field set [C:3][N] instead of [N] to get the renumbering done with 3 digits and leading 0 before the name.

Code: Select all

Search for:^(\d\d\d)\d+ (.*)$
Replace with:$1 $2
-- if you are able to select or make an ordered file list, you can do it all at once, just using [C:3] [N] counter + name and only keeping the title like

Code: Select all

Search for:#.+#\d+$
Replace with:<clear>
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Re: Change of substring in file names

Post by *DruidCtba »

white wrote: 2024-04-21, 22:50 UTC
DruidCtba wrote: 2024-04-21, 22:36 UTC I can use regex (regular expression) to clean up (clear option) the end of the file that is like - #Tableau Course #, using the regex command " #Tableau Course #\d\d\d", but I don't know how to put this final number at the beginning of the file name, in the example above it would be 125:
Search for regex "(.*) #Tableau Course #(\d\d\d)"
Replace with "$2 $1"
Thank you very much, friend @white, I just had to adapt here and did it in three steps, but it turned out great.

1. find - (.*) #Tableau Course #(\d\d\d) ---> for three digits
replace - $2 $1
2. find - (.*) #Tableau Course #(\d\d) ---> for two digits
replace - 0$2 $1
3. find - (.*) #Tableau Course #(\d) ---> for one digit
replace - 00$2 $1

I had forgotten the $ parameter, which allows you to shift substrings, and that's exactly what I needed here.

Maybe I didn't need the three steps, but at some point I'll learn to do it better with the full power of regex in just one step :) :wink:.

Regards,

DruidCtba®.
Last edited by DruidCtba on 2024-04-22, 17:45 UTC, edited 1 time in total.
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Re: Change of substring in file names

Post by *DruidCtba »

nsp wrote: 2024-04-22, 05:34 UTC For all the Search & replace expression:
- tick [RegEx] only
- Do not tick with extension [E]

If the # is the Marker you want to use, and exactly 3 digits @white expression will wok fine.
If you want to have less constraints and use a replace string for the complete name, you can use:

Code: Select all

Search for:^(.*)#.*#(\d+)$
Replace with:$2 $1
To do the re-numbering, you need to be back to normal panel and order the files as you need then you can re select all then:
in the name field set [C:3][N] instead of [N] to get the renumbering done with 3 digits and leading 0 before the name.

Code: Select all

Search for:^(\d\d\d)\d+ (.*)$
Replace with:$1 $2
-- if you are able to select or make an ordered file list, you can do it all at once, just using [C:3] [N] counter + name and only keeping the title like

Code: Select all

Search for:#.+#\d+$
Replace with:<clear>
Hey @nsp, thank you so much for your help too, but I ended up solving it through the post before yours from @white. Still, I really appreciate your willingness to help; it means a lot these days when people are more focused on themselves.

I tried your first command, but it didn't change anything. Since I'm not very familiar with the power of regex yet, I can't tell you why it didn't work while @white's did. As you can see above, I needed three steps to solve it here, and I think it could have been done in just one step. The numbering can be 3, 2, and 1 digits, and the zeros in front of them also need to account for 1 and 2 digits. If you know how to do it in one step, following @white's model, I'd love to try it out here to learn.

Anyway, thanks a lot for your help and interest in helping me with my issue :) :wink: .

Ah, correcting and sorry for not being complete in my question, each file, actually there are always 3 files, the mkv, the srt in English, and the srt in Portuguese, and I was omitting in my problem description, see in the image below using your model:

Image: https://i.imgur.com/U4lMEUM.jpg

Best regards,

DruidCtba@.
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Re: Change of substring in file names

Post by *DruidCtba »

Hey, sorry, but just one thing, how do I know what $1 and $2 are, where these substrings start and end?

Regards.

DruidCtba.
User avatar
white
Power Member
Power Member
Posts: 4626
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Change of substring in file names

Post by *white »

DruidCtba wrote: 2024-04-23, 22:26 UTC Hey, sorry, but just one thing, how do I know what $1 and $2 are, where these substrings start and end?
Have you read the help?
Help - Regular expressions wrote:Subexpressions for search+replace

Text parts in round brackets are taken as subexpressions. Up to 89 subexpressions are supported now.
Example: To swap the title and interpret in the file name of an mp3 file, when they are separated by a dash (Title - Interpret.mp3), use the following options:
Search for: (.*) - (.*)\.mp3
Replace by: $2 - $1.mp3
Here $1 means the text in the first round bracket, and $2 the text in the second round bracket.
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Re: Change of substring in file names

Post by *DruidCtba »

white wrote: 2024-04-23, 22:32 UTC
DruidCtba wrote: 2024-04-23, 22:26 UTC Hey, sorry, but just one thing, how do I know what $1 and $2 are, where these substrings start and end?
Have you read the help?
Help - Regular expressions wrote:Subexpressions for search+replace

Text parts in round brackets are taken as subexpressions. Up to 89 subexpressions are supported now.
Example: To swap the title and interpret in the file name of an mp3 file, when they are separated by a dash (Title - Interpret.mp3), use the following options:
Search for: (.*) - (.*)\.mp3
Replace by: $2 - $1.mp3
Here $1 means the text in the first round bracket, and $2 the text in the second round bracket.
Ok buddy @white, I got it now, thank you very much, I'll read the help more carefully, thanks again for your help.

Best regards,

DruidCtba.
User avatar
white
Power Member
Power Member
Posts: 4626
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Change of substring in file names

Post by *white »

DruidCtba wrote: 2024-04-22, 16:29 UTC Maybe I didn't need the three steps, but at some point I'll learn to do it better with the full power of regex in just one step :) :wink:.
Well, you can combine the 3 steps. For example like this:

Code: Select all

Rename mask: filename
00[N]

Search for:     ^()00(.*?)\s*#.*#(\d\d\d)|^(0)0(.*?)\s*#.*#(\d\d)|^(00)(.*?)\s*#.*#(\d)
Replace with:   $1$3$4$6$7$9 $2$5$8

[X] RegEx
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Re: Change of substring in file names

Post by *DruidCtba »

white wrote: 2024-04-23, 23:15 UTC
DruidCtba wrote: 2024-04-22, 16:29 UTC Maybe I didn't need the three steps, but at some point I'll learn to do it better with the full power of regex in just one step :) :wink:.
Well, you can combine the 3 steps. For example like this:

Code: Select all

Rename mask: filename
00[N]

Search for:     ^()00(.*?)\s*#.*#(\d\d\d)|^(0)0(.*?)\s*#.*#(\d\d)|^(00)(.*?)\s*#.*#(\d)
Replace with:   $1$3$4$6$7$9 $2$5$8

[X] RegEx
Dear friend @white, it worked perfectly 😊👍, thank you very much. I will study this regex, as it is important for me since I am studying Data Science and it will be very useful for the infamous "Data Cleaning" 😱😅😂.

Thanks a lot for your time and help 😜.

Best regards,

DruidCtba®.
JOUBE
Power Member
Power Member
Posts: 1482
Joined: 2004-07-08, 08:58 UTC

Re: Change of substring in file names

Post by *JOUBE »

DruidCtba wrote: 2024-04-24, 18:53 UTC I will study this regex, as it is important for me since I am studying Data Science
Please note that the group reference with $<number> works with Regex.Replace (like in TCs sync tool), for a Backreference (like e.g. with 'sed') use \<number> instead.

Joube
User avatar
nsp
Power Member
Power Member
Posts: 1813
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: Change of substring in file names

Post by *nsp »

DruidCtba wrote: 2024-04-22, 16:38 UTC
I tried your first command, but it didn't change anything. Since I'm not very familiar with the power of regex yet, I can't tell you why it didn't work while @white's did. As you can see above, I needed three steps to solve it here, and I think it could have been done in just one step. The numbering can be 3, 2, and 1 digits, and the zeros in front of them also need to account for 1 and 2 digits. If you know how to do it in one step, following @white's model, I'd love to try it out here to learn.

Anyway, thanks a lot for your help and interest in helping me with my issue :) :wink: .

Ah, correcting and sorry for not being complete in my question, each file, actually there are always 3 files, the mkv, the srt in English, and the srt in Portuguese, and I was omitting in my problem description, see in the image below using your model:
HI @DruidCtba,
In your initial post you give a sample with an mkv file so the name of the file ends with a number and then you have an mkv extension.
TC consider as extension only the chars after the latest ".'" in fullname. The expression between ^ and $ match complete file name. This is why the expression is working with mkv and fails with all subtitles as complete fine name also include the srt lanuage name i.e. ".pt" ".en" ...
To make it works as you want, you can modify to add possible(?) .xx (dot and two letters \.\w\w ) as

Code: Select all

^(.*)#.*#(\d+)(\.\w\w)?$
@White expression match inside file name but do not force complete matching this is why it is working in your case as it find the expression, stop and do the replacement.

Adding counter, info,... regex and finally being able to reload and apply saved "expression" from button/hot key/.. makes TC a really a wonderful renaming tool.
Regex takes a bit of time to learn and experiment but is great for every file search/replace/.... operations.
User avatar
crossinglines
New Member
New Member
Posts: 1
Joined: 2024-04-26, 06:46 UTC

Re: Change of substring in file names

Post by *crossinglines »

You can accomplish this task using a scripting language like Python. Here's a Python script that renames files in bulk according to your specifications:

Code: Select all

 
import os
import re

# Define the directory containing the files
directory = "/path/to/your/directory"

# Define the regex pattern to extract the number from the filename
pattern = re.compile(r"#Tableau Course #(\d{1,3})\.mkv$")

# Iterate over each file in the directory
for filename in os.listdir(directory):
    # Check if the file is an MKV file
    if filename.endswith(".mkv"):
        # Extract the number from the filename
        match = re.search(pattern, filename)
        if match:
            number = match.group(1)
            # Rename the file
            new_filename = f"{number.zfill(3)} {filename.replace('#Tableau Course #' + number, '').strip()}"
            os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
            print(f"Renamed {filename} to {new_filename}")
Make sure to replace "/path/to/your/directory" with the path to the directory containing your files.
This script extracts the number from the filename using a regular expression, formats it to be zero-padded to 3 digits, and then inserts it at the beginning of the filename.
Last edited by white on 2024-04-26, 11:28 UTC, edited 1 time in total.
Reason: Put code in code block
hi
User avatar
white
Power Member
Power Member
Posts: 4626
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: Change of substring in file names

Post by *white »

Moderator message from: white » 2024-04-26, 11:28 UTC

crossinglines wrote: 2024-04-26, 07:01 UTC Here's a Python script that renames files in bulk according to your specifications:
I have put the code in a code block in order to preserve indentation.
DruidCtba
Junior Member
Junior Member
Posts: 7
Joined: 2024-04-21, 22:21 UTC

Re: Change of substring in file names

Post by *DruidCtba »

crossinglines wrote: 2024-04-26, 07:01 UTC You can accomplish this task using a scripting language like Python. Here's a Python script that renames files in bulk according to your specifications:

Code: Select all

 
import os
import re

# Define the directory containing the files
directory = "/path/to/your/directory"

# Define the regex pattern to extract the number from the filename
pattern = re.compile(r"#Tableau Course #(\d{1,3})\.mkv$")

# Iterate over each file in the directory
for filename in os.listdir(directory):
    # Check if the file is an MKV file
    if filename.endswith(".mkv"):
        # Extract the number from the filename
        match = re.search(pattern, filename)
        if match:
            number = match.group(1)
            # Rename the file
            new_filename = f"{number.zfill(3)} {filename.replace('#Tableau Course #' + number, '').strip()}"
            os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
            print(f"Renamed {filename} to {new_filename}")
Make sure to replace "/path/to/your/directory" with the path to the directory containing your files.
This script extracts the number from the filename using a regular expression, formats it to be zero-padded to 3 digits, and then inserts it at the beginning of the filename.
Fine thanks dear friend :shock: :) :D :wink: .

Att.

DruidCtba®.
Post Reply