2

我知道有一种方便的方法可以在 Powershell 中验证 IPAdresses:

[ipaddress]"12.12.12.12"

[ipaddress]"12.12.12.812" #Throws an Error.

您知道是否有类似的方法来验证文件名和路径名?例如

[path]"c:\temp\t?est.txt" #Throws an Error

4

1 回答 1

6

您可以使用Test-Pathcmdlet:

Test-Path "c:\temp\t?est.txt" -IsValid

从帮助:

PS> help test-path -param isvalid

-IsValid [<SwitchParameter>]
    Determines whether the syntax of the path is correct, regardless of whether the elements of the path exist. This
    parameter returns TRUE if the path syntax is valid and FALSE if it is not.

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false
于 2013-04-21T15:04:48.287 回答