PowerShell 技能连载 - 检查文件名的非法字符

文件的命名是十分敏感的,不能包含某些特定的保留字符。要验证文件名并确保这些字符是合法的,以下对昨天的脚本(检查文件系统路径)做了一点改进。这个脚本检查文件名的合法性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# check path:
$filenameToCheck = 'testfile:?.txt'

# get invalid characters and escape them for use with RegEx
$illegal =[Regex]::Escape(-join [System.Io.Path]::GetInvalidFileNameChars())
$pattern = "[$illegal]"

# find illegal characters
$invalid = [regex]::Matches($filenameToCheck, $pattern, 'IgnoreCase').Value | Sort-Object -Unique

$hasInvalid = $invalid -ne $null
if ($hasInvalid)
{
"Do not use these characters in file names: $invalid"
}
else
{
'OK!'
}

结果如下:

Do not use these characters in file names: : ?

本文国际来源:Check File Names for Illegal Characters

评论