Change of substring in file names
Moderators: Hacker, petermad, Stefan2, white
Change of substring in file names
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.
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.
Re: Change of substring in file names
Search for regex "(.*) #Tableau Course #(\d\d\d)"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:
Replace with "$2 $1"
Re: Change of substring in file names
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:
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.
-- 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
- 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
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
Code: Select all
Search for:#.+#\d+$
Replace with:<clear>
Re: Change of substring in file names
Thank you very much, friend @white, I just had to adapt here and did it in three steps, but it turned out great.white wrote: 2024-04-21, 22:50 UTCSearch for regex "(.*) #Tableau Course #(\d\d\d)"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:
Replace with "$2 $1"
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


Regards,
DruidCtba®.
Last edited by DruidCtba on 2024-04-22, 17:45 UTC, edited 1 time in total.
Re: Change of substring in file names
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.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: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:Code: Select all
Search for:^(.*)#.*#(\d+)$ Replace with:$2 $1
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.-- 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 likeCode: Select all
Search for:^(\d\d\d)\d+ (.*)$ Replace with:$1 $2
Code: Select all
Search for:#.+#\d+$ Replace with:<clear>
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


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@.
Re: Change of substring in file names
Hey, sorry, but just one thing, how do I know what $1 and $2 are, where these substrings start and end?
Regards.
DruidCtba.
Regards.
DruidCtba.
Re: Change of substring in file names
Have you read the help?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?
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.
Re: Change of substring in file names
Ok buddy @white, I got it now, thank you very much, I'll read the help more carefully, thanks again for your help.white wrote: 2024-04-23, 22:32 UTCHave you read the help?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?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.
Best regards,
DruidCtba.
Re: Change of substring in file names
Well, you can combine the 3 steps. For example like this: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![]()
.
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
Re: Change of substring in file names
Dear friend @white, it worked perfectlywhite wrote: 2024-04-23, 23:15 UTCWell, you can combine the 3 steps. For example like this: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![]()
.
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
Thanks a lot for your time and help
Best regards,
DruidCtba®.
Re: Change of substring in file names
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.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
Joube
Re: Change of substring in file names
HI @DruidCtba,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![]()
.
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:
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)?$
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.
- crossinglines
- New Member
- Posts: 1
- Joined: 2024-04-26, 06:46 UTC
Re: Change of substring in file names
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:
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.
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}")
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
Reason: Put code in code block
Re: Change of substring in file names
Moderator message from: white » 2024-04-26, 11:28 UTC
I have put the code in a code block in order to preserve indentation.crossinglines wrote: 2024-04-26, 07:01 UTC Here's a Python script that renames files in bulk according to your specifications:
Re: Change of substring in file names
Fine thanks dear friendcrossinglines 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:Make sure to replace "/path/to/your/directory" with the path to the directory containing your files.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}")
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.




Att.
DruidCtba®.