BeW*_*ned 12 powershell system.io.fileinfo
我现在开始使用PowerShell并且经过大量时间使用Unix shell并想知道如何检查文件或目录的存在.
在Powershell中,为什么Exist
在下面的表达式中返回false?
PS H:\> ([System.IO.FileInfo]"C:\").Exists
False
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来检查文件是否是一个目录:
PS H:\> ([System.IO.FileInfo]"C:\").Mode.StartsWith("d")
True
Run Code Online (Sandbox Code Playgroud)
Mic*_*ael 23
使用'test-path'而不是System.IO.FileInfo.Exists
PS C:\Users\m> test-path 'C:\'
True
Run Code Online (Sandbox Code Playgroud)
您可以使用PSIsContainer来确定文件是否是目录:
PS C:\Users\m> (get-item 'c:\').PSIsContainer
True
PS C:\Users\m> (get-item 'c:\windows\system32\notepad.exe').PSIsContainer
False
Run Code Online (Sandbox Code Playgroud)
Kev*_*Kev 11
除了Michael的回答,您还可以使用以下方法进行测试:
PS H:> ([System.IO.DirectoryInfo]"C:\").Exists
True
Run Code Online (Sandbox Code Playgroud)
小智 9
Help Test-Path
Test-Path Determines whether all elements of a path exist
Test-Path -PathType Leaf C:\test.txt
Test-Path -PathType Container C:\
Test-Path C:\
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
38336 次 |
最近记录: |