powershellfile-renamebatch-rename

Error renaming text files based on the first line in the file in Powershell


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.


Solution

  • 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
    }