- Joined
- Oct 20, 2019
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:
(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:
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
And that's more or less it. The
And that's it. I've spent longer explaining the code than the code itself. If you want to make this recursive, just add "
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.
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: