PowerShell 怎么判断一个 字符串是一个路径,还是文件,还是都不是?

1、任意字符串,ldfjilsjflkjlkj,只要不为空,直接用 test-path 返回 True,说明是一个全路径,并且存在。
2、怎么判断是文件,还是目录,一个目录下不存在同名的目录和文件。
G: G:\ ? Get-Item $string ?
信息量太大,百度限制我的字数无奈啊,图片上面。

    问题1:判断文件夹和文件:

PS> (Get-Item .\dir).GetType().fullname
System.IO.DirectoryInfo
PS> (Get-Item .\file).GetType().fullname
System.IO.FileInfo

PS> (Get-Item .\file) -is [IO.fileinfo]
True
PS> (Get-Item .\file) -is [IO.DirectoryInfo]
False

    问题2:D: 和D:\ 有什么区别,正如你所理解的,这个是规定。即使在cmd.exe 前者是定位到该盘的历史目录,后者是定位到根目录。

    问题3:get-item支持那些类型?

这个问题可以换个问法?那些类型支持虚拟驱动器,支持虚拟驱动器的基本上都可以支持get-item。即使你自己自定义的某种类型。

PS> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
Alias                                  Alias
C                  43.80         36.20 FileSystem    C:\                                                     Users\pstips.net
Cert                                   Certificate   \
D                   5.41         74.44 FileSystem    D:\
E                   8.32        122.63 FileSystem    E:\
Env                                    Environment
F                  59.50         20.50 FileSystem    F:\
Function                               Function
H                                      FileSystem    H:\
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
N                  25.46         54.54 FileSystem    N:\
Variable                               Variable
WSMan                                  WSMan

更过虚拟驱动器的解释,请参考:

Powershell驱动器变量:http://www.pstips.net/powershell-drive-variables.html

PowerShell 添加个性化驱动器:http://www.pstips.net/powershell-add-special-vital-driver.html

温馨提示:答案为网友推荐,仅供参考
相似回答