PowerShell 怎么判断一个 字符串是一个路径,还是文件,还是都不是? Powershell 写一脚本判断在一特定路径下是否存在指定...

powershell \u5224\u65ad\u662f\u76ee\u5f55\u8fd8\u662f\u6587\u4ef6

# \u76ee\u5f55\u5b58\u5728
Test-Path $path -PathType Container
[System.IO.Directory]::Exists($path)

# \u6587\u4ef6\u5b58\u5728
Test-Path $path -PathType Leaf
[system.IO.File]::Exists($path)

$filelist=gc "file.txt" #\u83b7\u53d6\u8981\u68c0\u67e5\u7684\u6587\u4ef6\u5217\u8868
$csvs= new-object collections.arraylist #\u521b\u5efa\u4e00\u4e2aarraylist\u5bf9\u8c61
foreach($file in $filelist){
$csv=new-psobject|select yes,no
if([io.Directory]::Exists($file)){ #\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728
$csv.yes=$file
}else{
$csv.no=$file
}
$null=$csvs.add($csv)
}
$csvs|Export-Csv file.csv -notype -Encoding oem #\u5bfc\u51fa\u6210csv\u6587\u4ef6

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

这个问题可以换个问法?那些类型支持虚拟驱动器,支持虚拟驱动器的基本上都可以支持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



扩展阅读:powershell iex ... powershell 中文翻译 ... powershell命令大全 ... windows powershell ... powershell 教程 ... powershell修复电脑 ... powershell怎么打开 ... 卸载powershell有影响吗 ... powershell是病毒吗 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网