我正在嘗試匯入每行有一個檔案夾路徑的 .txt 檔案。我正在嘗試查看記事本檔案中是否存在多個檔案夾路徑。我現在不成功,因為我的腳本似乎認為整個 .txt 檔案是檔案路徑,或者因為我沒有讓腳本正確讀取 .txt 檔案。我想知道這是否可能?
$Folder = 'C:\Users\User\Downloads\FolderExist.txt'
Get-Content -Path $Folder
if (Test-Path -Path $Folder) {
"Path exists!"
} else {
"Path doesn't exist."
}
Export-Csv -Path "C:\Users\User\Documents\FolderExist.csv" -NoTypeInformation
這是我收到的錯誤
Get-Content : Cannot find path 'C:\Users\User\Downloads\FolderExist.txt' because it does not exist.
At line:2 char:1
Get-Content -Path $Folder
~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (C:\Users\_Micha...FolderExist.txt:String) [Get-Content], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
uj5u.com熱心網友回復:
您收到的錯誤表明您嘗試決議的檔案不存在。確保您在此處確實有一個檔案:
'C:\Users\User\Downloads\FolderExist.txt'
還有一些一般性的改進:首先,將文本檔案內容保存到一個變數中:
$Folder = 'C:\Users\User\Downloads\FolderExist.txt'
$MyPaths = Get-Content -Path $Folder
然后,要逐條測驗每條路徑,使用foreach回圈,并再次捕獲輸出。在這里,我已將檔案夾路徑添加到輸出中,以便您可以查看哪些檔案夾存在和不存在:
$output = Foreach ($folderPath in $MyPaths) {
if (Test-Path -Path $folderPath) {
Write-Output "Path '$folderPath' exists!"
} else {
Write-Output "Path '$folderPath' doesn't exist."
}
}
最后,將結果寫入檔案
$output | Out-File -Path "C:\Users\User\Documents\FolderExist.csv"
檔案看起來像:
Path 'c:\windows\' exists!
Path 'c:\users\' exists!
Path 'c:\bogus\' doesn't exist.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451702.html
上一篇:任務運行5次后啟動任務
