How to strip illegal characters before trying to save filenames?

前端 未结 6 847
夕颜
夕颜 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: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 '|')),'_'

提交回复
热议问题