How to strip illegal characters before trying to save filenames?

前端 未结 6 831
夕颜
夕颜 2020-12-08 20:04

I was able to find how to use the GetInvalidFileNameChars() method in a PowerShell script. However, it seems to also filter out whitespace (which is what I DON

相关标签:
6条回答
  • 2020-12-08 20:27

    [System.IO.Path]::GetInvalidFileNameChars() returns an array of invalid chars. If it is returning the space character for you (which it does not do for me), you could always iterate over the array and remove it.

    > $chars = @()
    > foreach ($c in [System.IO.Path]::GetInvalidFileNameChars())
      {
         if ($c -ne ' ')
         {
            $chars += $c
         }
      }
    

    Then you can use $chars as you would have used the output from GetInvalidFileNameChars().

    0 讨论(0)
  • 2020-12-08 20:30

    I wanted spaces to replace all the illegal characters so space is replaced with space

    $Filename = $ADUser.SamAccountName
    [IO.Path]::GetinvalidFileNameChars() | ForEach-Object {$Filename = $Filename.Replace($_," ")}
    $Filename = "folder\" + $Filename.trim() + ".txt"
    
    0 讨论(0)
  • 2020-12-08 20:40

    I suspect it has to do with non-display characters being coerced to [string] for the regex operation (and ending up expressed as spaces).

    See if this doesn't work better:

    ([char[]]$name | where { [IO.Path]::GetinvalidFileNameChars() -notcontains $_ }) -join ''
    

    That will do a straight char comparison, and seems to be more reliable (embedded spaces are not removed).

    $name = 'abc*\ def.txt'
    ([char[]]$name | where { [IO.Path]::GetinvalidFileNameChars() -notcontains $_ }) -join ''
    
    abc def.txt
    

    Edit - I believe @Ansgar is correct about the space being caused by casting the character array to string. The space is being introduced by $OFS.

    0 讨论(0)
  • 2020-12-08 20:41

    My current favourite way to accomplish this is:

    $Path.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
    

    This replaces all invalid characters with _ and is very human readable, compared to alternatives such as:

    $Path -replace "[$([RegEx]::Escape([string][IO.Path]::GetInvalidFileNameChars()))]+","_"
    
    0 讨论(0)
  • 2020-12-08 20:43

    Casting the character array to System.String actually seems to join the array elements with spaces, meaning that

    [string][System.IO.Path]::GetInvalidFileNameChars()
    

    does the same as

    [System.IO.Path]::GetInvalidFileNameChars() -join ' '
    

    when you actually want

    [System.IO.Path]::GetInvalidFileNameChars() -join ''
    

    As @mjolinor mentioned (+1), this is caused by the output field separator ($OFS).

    Evidence:

    PS C:\> [RegEx]::Escape([string][IO.Path]::GetInvalidFileNameChars())
    "\ \ \|\  \ ☺\ ☻\ ♥\ ♦\ ♣\ ♠\ \\ \t\ \n\ ♂\ \f\ \r\ ♫\ ☼\ ►\ ◄\ ↕\ ‼\ ¶\ §\ ▬\ ↨\ ↑\ ↓\ →\ ←\ ∟\ ↔\ ▲\ ▼\ :\ \*\ \?\ \\\ /
    PS C:\> [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ' '))
    "\ \ \|\  \ ☺\ ☻\ ♥\ ♦\ ♣\ ♠\ \\ \t\ \n\ ♂\ \f\ \r\ ♫\ ☼\ ►\ ◄\ ↕\ ‼\ ¶\ §\ ▬\ ↨\ ↑\ ↓\ →\ ←\ ∟\ ↔\ ▲\ ▼\ :\ \*\ \?\ \\\ /
    PS C:\> [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
    "\| ☺☻♥♦\t\n♂\f\r♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼:\*\?\\/
    PS C:\> $OFS=''
    PS C:\> [RegEx]::Escape([string][IO.Path]::GetInvalidFileNameChars())
    "\| ☺☻♥♦\t\n♂\f\r♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼:\*\?\\/

    Change your function to something like this:

    Function Remove-InvalidFileNameChars {
      param(
        [Parameter(Mandatory=$true,
          Position=0,
          ValueFromPipeline=$true,
          ValueFromPipelineByPropertyName=$true)]
        [String]$Name
      )
    
      $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
      $re = "[{0}]" -f [RegEx]::Escape($invalidChars)
      return ($Name -replace $re)
    }
    

    and it should do what you want.

    0 讨论(0)
  • 2020-12-08 20:45

    Please try this one-liner with the same underlying function.

    to match

    '?Some "" File Name <:.txt' -match ("[{0}]"-f (([System.IO.Path]::GetInvalidFileNameChars()|%{[regex]::Escape($_)}) -join '|'))

    to replace

    '?Some "" File Name <:.txt' -replace ("[{0}]"-f (([System.IO.Path]::GetInvalidFileNameChars()|%{[regex]::Escape($_)}) -join '|')),'_'

    0 讨论(0)
提交回复
热议问题