Niche filename issue I'm hoping someone can help me with

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
So I'd honestly like to see a screenshot of this because there's room for confusion here. But taking you at your word, here is something which will rename files with illegal characters. Obviously be fucking careful with things that mass rename files. Run this where you want it to rename files only. With all due respect to @We Are The Witches, you don't need to break out the Python for this. The following will first search for any files containing illegal file name characters and wont rename anything:

Code:
$illegalCharsRegExp =[\^/\*"<>:\|\?\\\{}]'
Get-ChildItem | Where-Object { $_.BaseName -match $illegalCharsRegExp }

(edit: I amended the illegal chars to include { } as you specifically said they were giving you problems although they are not in fact illegal characters in NTFS so maybe it really is a Windows 7 UI problem)

And if you would like to then automatically rename them, you can do the following:

Code:
$replacementChar = "_"
Get-ChildItem | Rename-Item -NewName { $_.Name -replace $illegalCharsRegExp, $replacementChar }

You could do all of this in one line but I broke it down to make it slightly more readable for those new to Powershell. The $replacementChar should be understandable enough - every illegal character in the file name becomes a '_'. So "my?file.txt' would become 'my_file.txt'. Change the '_' to something else if you want.

$illegalCharsRegExp is maybe slightly harder to read. But it's just the illegal file characters which are '/*"<>:|?\'. Only written in the form of a regular expression so some of them have the escape symbol '\' before them and the whole thing has '[' and ']' either side. I'm not going to explain regular expressions here but if you wanted to make this a more general file re-namer, you'd change that line and I can help with that if needed. And if you can be more specific like "I want to replace ?, |, { and } in file names" I'll give you a better version of this that is specific.

And that's more or less it. The Get-ChildItem lines in the first or second version are pretty similar. The first version just gets all the files and then pipes them to Where-Object which is a filter based on if they actually have the illegal characters in them. The one in the second code block does similar except it pipes them to a Rename-Item which will do an actual replacement of the illegal characters. There's no need to filter here as the rename is its own filter.

And that's it. I've spent longer explaining the code than the code itself. If you want to make this recursive, just add "-Recurse" after Get-ChildItem. But be careful with anything which batch renames stuff on your system. You might also want to filter out directories by adding -Attributes !D after the Get-ChildItem.

Run this only from the directory that you want to run it. You could turn this into a script if you want but it's so short I figure just paste the lines in a terminal from wherever you want to be. It'll abort if there's a clash in file names which I could code around but am not bothering too.

Let us know if this helps.
 
Last edited:
@skykiii No longer give a fuck?
For some reason I stopped getting updates.

But I was kinda hoping there would already be a freeware program somewhere that did this. The minute people started saying "learn programming" I was like "maybe I should just cart around a Windows 10 or Linux laptop to sidestep the entire issue."
 
For some reason I stopped getting updates.

But I was kinda hoping there would already be a freeware program somewhere that did this. The minute people started saying "learn programming" I was like "maybe I should just cart around a Windows 10 or Linux laptop to sidestep the entire issue."
There is freeware for this. It's in the posts above. Two people now have written you scripts that would do it. Hell, mine is literally three lines you copy paste.

But up to you. Thought you'd just abandoned thread.
 
For some reason I stopped getting updates.

But I was kinda hoping there would already be a freeware program somewhere that did this. The minute people started saying "learn programming" I was like "maybe I should just cart around a Windows 10 or Linux laptop to sidestep the entire issue."
renamer.webp
Add a rule, go down to strip, check "symbols" or just type/paste the offending characters in "user defined". If you encounter any new character that causes trouble, double click the rule and add it to "user defined". Super easy and it gives you a preview of what the filename(s) will be.
 
  • Winner
Reactions: skykiii
Back