我使用 Get-Content 和 foreach 循环从文件逐行读取字符串,我想将这些字符串转换为目录对象(以便我可以访问像 .FullName
这样的属性)。如何轻松地将字符串转换为目录?
对于文件,这很容易:$myFileAsFile = $myFileAsStr | dir $_
,但是,如何获得 $directoryAsString
的目标呢?
我使用 Get-Content 和 foreach 循环从文件逐行读取字符串,我想将这些字符串转换为目录对象(以便我可以访问像 .FullName
这样的属性)。如何轻松地将字符串转换为目录?
对于文件,这很容易:$myFileAsFile = $myFileAsStr | dir $_
,但是,如何获得 $directoryAsString
的目标呢?
好的,答案似乎是Get-Item
:
$dirAsStr = '.\Documents'
$dirAsDir = Get-Item $dirAsStr
echo $dirAsDir.FullName
有效!
您可以使用 .Net 类 System.IO.FileInfo
或 System.IO.DirectoryInfo
。即使目录不存在,这也可以工作:
$c = [System.IO.DirectoryInfo]"C:\notexistentdir"
$c.FullName
它甚至可以使用文件:
$c = [System.IO.DirectoryInfo]"C:\some\file.txt"
$c.Extension
所以要检查它是否真的是一个目录,请使用:
$c.Attributes.HasFlag([System.IO.FileAttributes]::Directory)
下面的评论中有一个使用了 System.IO.FileInfo
的示例。
[System.IO.FileInfo]$filename = "文件路径"
来获取文件对象。然后你可以使用 $filename.directory
来获取它的目录。虽然有点离题,但以防你和我一样来到这里。 - Ty Savercool([System.IO.FileInfo]'.\myfile.txt').FullName
无论当前文件夹是什么,都会生成C:\Windows\System32\myfile.txt。 - Code39因此,我通常使用以下简单方法从字符串类型变量获取路径/完整路径:
(Resolve-Path $some_string_var)
Set-Variable -Name "str_path_" -Value "G:\SO_en-EN\Q33281463\Q33281463.ps1"
$fullpath = (Resolve-Path $some_string_var) ; $fullpath
Get-item将根据输入输出FileInfo或DirectoryInfo对象。或者可以使用管道符号传递给get-item -path { $_ }
。
$myFileAsFile = get-item $myFileAsStr
$directoryAsDir = get-item $directoryAsString
快速的方法
# "Get-Item" automatically grabs $Path item as an object if it exists.
# Carry on your merry way.
$FSO = Get-Item -Path $Path -Force
# Get path string (via parm, pipeline, etc.)
# Can be full path ('c:\users\Me') or properly formatted relative path ('..\..\Logs').
$Path = '<some_string>'
# Never presume the input actually exists, so check it with "Test-Path".
# Note: If the string is a file and ends with "\", this check will fail (generate an error).
# YMMV: add add'l code to strip off trailing "\" unless it's a drive (e.g., "C:\") prior to the check.
if (Test-Path $Path -PathType Leaf) {
$PathType = 'File'
} elseif (Test-Path $Path -PathType Container) {
$PathType = 'Folder'
} else {$PathType = $null}
# "Get-Item" automatically grabs item as an object if it exists.
if ($PathType) {
$FSO = Get-Item -Path $Path -Force
Write-Host 'Object is:' $PathType
Write-Host 'FullName: ' $FSO.FullName
} else {
Write-Host 'Bad path provided.'
exit
}
# Some Test-Path samples:
$Path = 'c:\windows\' # Folder: Test-Path works
$Path = 'c:\windows' # Folder: Test-Path works
$Path = 'c:\windows\system.ini' # File: Test-Path works
$Path = 'c:\windows\system.ini\' # File: Test-Path FAILS
$Path = '..\system.ini' # File: Test-Path works
$Path = '..\system.ini\' # File: Test-Path FAILS
以上代码有些不够简洁,需要优化...
# Get path string (via parm, pipeline, etc.)
$Path = '<some_string>'
# Remove trailing "\" on all but drive paths (e.g., C:\, D:\)
if ($Path.EndsWith("\")) {
if ($Path.Length -gt 3) {
$Path = $Path.Remove($Path.Length - 1)
}
}
# If the provided path exists, do stuff based on object type
# Else, go another direction as necessary
if (Test-Path -Path $Path) {
$FSO = Get-Item -Path $Path -Force
if ($FSO.GetType().FullName -eq "System.IO.DirectoryInfo") {
Write-Host "Do directory stuff."
} elseif ($FSO.GetType().FullName -eq "System.IO.FileInfo") {
Write-Host "Do file stuff."
} else {
Write-Host "Valid path, but NOT a file system object!! (could be a registry item, etc.)"
}
Write-Host $FSO.FullName
} else {
Write-Host "Path does not exist. Bail or do other processing, such as creating the path."
$FSO = $null
}
$(Get-Item $directoryAsString).FullName
$(获取项 $directoryAsString).全名
$myFileAsFile = $myFileAsStr | dir { $_ }
吗?否则我不明白它怎么可能起作用。 - js2010