1

I'm trying to rename a large number of plain text files based on the first line. The issue I'm encoutering is that some first lines have invalid characters (illegal characters in path) so I get errors. This is what I'm using:

$files = Get-ChildItem *.txt

$file_map = @()
foreach ($file in $files) {
    $file_map += @{
        OldName = $file.Fullname
        NewName = "{0}.txt" -f $(Get-Content $file.Fullname| select -First 1)
    }
}

$file_map | % { Rename-Item -Path $_.OldName -NewName $_.NewName }

Is there a way to ignore special characthers when renaming?

Thanks.

eera5607
  • 305
  • 2
  • 8

1 Answers1

1

The following might work for you. Essentially, you can use the Path.GetInvalidFileNameChars Method to get a char array of those invalid characters for a file name then create a regex pattern to remove those invalid chars:

$invalidChars = ([IO.Path]::GetInvalidFileNameChars() |
    ForEach-Object { [regex]::Escape($_) }) -join '|'

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace $invalidChars
    '{0}.txt' -f $firstLine
}

Perhaps an easier approach would be to remove any Non-word character with \W:

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace '\W'
    '{0}.txt' -f $firstLine
}

Or to remove any character that is not in the character ranges a-z, A-Z, 0-9 or a space :

Get-ChildItem *.txt | Rename-Item -NewName {
    $firstLine = ($_ | Get-Content -TotalCount 1) -replace '[^a-z0-9 ]'
    '{0}.txt' -f $firstLine
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • Thank you! What would you say is the easiest way to leave in the name just alphanumeric characters and remove symbols and characters with accents for example? Using your method of course. – eera5607 Mar 30 '23 at 19:15
  • @eera5607 you could use `-replace '\W'` – Santiago Squarzon Mar 30 '23 at 19:17
  • Thanks. It's interesting because both methods work in the sense that the name change is done without any errors. But I don't understand why the new names have lots of characters that were not present before, example:  where there was a ¿ or à where there was an é. – eera5607 Mar 30 '23 at 19:24
  • Is there a way to leave the spaces between words? – eera5607 Mar 30 '23 at 19:27
  • @eera5607 see my last and final edit. – Santiago Squarzon Mar 30 '23 at 19:30
  • Thank you Santiago. I'm sorry I know the question didn't specied this, but is there a way to substitute the accented characters like é for an e for example. Not just remove them? – eera5607 Mar 30 '23 at 19:35
  • @eera5607 yes but should be part of a different question – Santiago Squarzon Mar 30 '23 at 19:36
  • @eera5607 something like this would work https://stackoverflow.com/questions/73764009/how-can-i-turn-turkish-chars-to-ascii/73764185#73764185 most likely – Santiago Squarzon Mar 30 '23 at 19:39