I'm trying to batch rename plain text files using the first line of each file. I want to keep only alphanumeric characters in with your help I'm almost there. The only issue is that I need accented characters like é
or á
to be preserved in a form of their respective not accented characters: e and a (text is in Spanish) or be preserved in the name as they are, not removed. This is what I'm using right now:
Get-ChildItem *.txt | Rename-Item -NewName {
$firstLine = ($_ | Get-Content -TotalCount 1) -replace '[^a-z0-9 ]'
'{0}.txt' -f $firstLine
}
Thank you. If possible, please let me know if there is a way to keep the symbol "?" too.
All you need to do is to add á
and é
to exclusion list of your replacement, ant they will be preserved:
Get-ChildItem *.txt | Rename-Item -NewName {
($_ | Get-Content -TotalCount 1 -Encoding UTF8) -replace '[^a-z0-9éá ]', '' -replace '.*', '$0.txt'
}
As for ?
- it is not valid symbol for filename in windows, so I don't see a point there. But you always can do multiple replacement, and replace it with something allowed. Like so:
"asd we'wea?gke é or á? to b" -replace '[^a-z0-9éá ]', '' -replace '\?', '!!!!'