Hi,
please have patience with the cat
[Attachment 86417 - Click to enlarge]
thanks.
I try to do a renamer for files with special character in powershell
but this acts on ALL file, meanwhile I need it acts only on *.mp3 and *.mp4 filesCode:dir | % {$_ | rename-item -NewName $_.name.replace('%','').replace('#','').replace('_',' ').replace('!',' ').replace('[',' ').replace(']',' ').replace('{',' ').replace('}',' ').replace('&',' ').replace('?',' ').replace(',',' ').replace(';',' ').replace(':',' ').replace('$',' ').replace('^',' ').replace('\',' ').replace('"',' ').replace('@',' ').replace('*',' ').replace('(',' ').replace(')',' ').replace('-',' ').replace('=',' ').replace('+',' ').replace('/',' ').replace('.',' ').replace('>',' ') -ErrorAction SilentlyContinue}
How I can do?
+ Reply to Thread
Results 1 to 5 of 5
-
-
(Coming from there, this thread was listed below “Similar Threads”.)
A cat with PowerShell is like a chimpanzee with a machine-gun.
But in case you still haven't found, it should be something like:
(GCI being a shorthand for “Get-ChildItem”.)Code:ForEach ($file in GCI *.mp3 *.mp4) { ...
Not sure about how to properly use this with the rest of the script – I'll try to make some tests as it could come in handy.
It may be possible to simplify the command if several characters are to be replaced by a space, but I wouldn't know how.
How can there be valid file names with characters like “\” or “/” or “>” or “*” or “:” or “?” or “"” on a Windows system?
Since you seem to be doing quite a few file manipulations beyond video stuff, you might find this interesting. -
Use ReNamer GUI, highly recommend: https://www.den4b.com/products/renamer
In your case, start it,
--- Click + Add (rule) and select "Delete", add # character "+ Add Rule"
--- Click + Add (rule) and select "Delete", add % character and select "+ Add Rule"
--- Click + Add (rule) and select "RegEx" and for Expression use: [^a-zA-Z0-9] and for Replace: just type empty space bar character, select "+ Add Rule"
Drop your files on GUI (select it before as "type" in Windows explorer so they bunch up with extensions)
--- Click Preview
Only when you are satisfied with new name results, click "Rename"
or I can drop you a python file you can call from batch script, that you can call from batch script -
Haven't tried it yet, it seems convenient and flexible. Bulk Rename Utility allows to use regular expressions, but not to define multiple single character replacements simultaneously (well the version I have is from 2021, it could have been improved since...). It does however allow to pre-filter files in a given folder, by extension or any other pattern; in this case it could filter files with a name fitting the “*.mp*” pattern, but that would also display *.mpg files for instance; with regular expressions enabled, “.+\.mp[34]$” would work reliably to display *.mp3 and *.mp4 files only; but I wouldn't know how to display simultaneously two types of files with a completely different extension, for instance *.avi and *.mp4.Use ReNamer GUI, highly recommend
Still, for a task that has to be performed regularly, a script solution might be preferred.
I have a tiny bit of knowledge of PowerShell, no experience with Python. I'd be eager to learn, as it seems both fairly easy and powerful, but now wouldn't be the right time to learn anything as involved as a programming language. -
There could be a python command line utility made that does this (not gui):
That works for PowerShell. For windows batch there must be some escaping done for regex sometimes, in our case it would be --regextospace [^^a-zA-Z0-9]Code:python "rename filenames.py" --directory "E:\Videos\extraordinary_cat_videos" --confirm_renaming --extensions mp4*mp3 --regextodelete [%#] --regextospace [^a-zA-Z0-9]
or just running exe utility, not needing python installed, below it is for windows batch, not sure now how to call it with arguments now in PowerShell:
--- if directory argument is not in command line, executable directory is used (same directory as py or exe file is in)Code:"rename filenames.exe" --directory "E:\Videos\extraordinary_cat_videos" --confirm_renaming --extensions mp4*mp3 --regextodelete [%#] --regextospace [^^a-zA-Z0-9]
--- if flag --confirm_renaming is removed, renaming is not waiting for confirmation, DO ONLY IF EXPERIENCED what this all does
if starting to be familiar with this, better to leave it there , look at printed files (originals and would be renamed) and then confirm y/n if wanting renaming
--- extensions need to be specified, separated by a star character: "*"
--- characters in regextodelete argument are characters to be deleted, it is a regex pattern, so in our example characters "%" and "#"
--- regextospace, characters in regex that will be replaced by space, in our example "^" makes it opposite, so if not alphabetic characters or digits, they will be replaced by space
so it is flexible, regex variables could be changed a bit for different needsLast edited by _Al_; 17th Mar 2026 at 22:27.
Similar Threads
-
Issue with unicode characters when encoding a subtitle file
By Hemer in forum SubtitleReplies: 1Last Post: 2nd Jul 2024, 05:33 -
Rename single file in a directory based on the content of a txt file
By D.LUFFY in forum ProgrammingReplies: 3Last Post: 7th Jun 2023, 12:16 -
How to get media info using powershell?
By iKron in forum Newbie / General discussionsReplies: 1Last Post: 6th May 2022, 08:53 -
how to delete file in powershell only if exist
By marcorocchini in forum Newbie / General discussionsReplies: 3Last Post: 21st Mar 2022, 23:25 -
how to deal with special characters such as french accents
By inklara in forum Video Streaming DownloadingReplies: 7Last Post: 10th Jan 2022, 11:13


?
Quote