[英]Adding multiple attachments to email with paths from txt file via powershell
[英]Using .txt to see if multiple file paths exist
我正在尝试导入每行有一个文件夹路径的 a.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
您收到的错误表明您尝试解析的文件不存在。 确保你确实有一个文件在这里:
'C:\Users\User\Downloads\FolderExist.txt'
以及一些常规改进:首先,将文本文件内容保存到一个变量中:
$Folder = 'C:\Users\User\Downloads\FolderExist.txt'
$MyPaths = Get-Content -Path $Folder
然后,为了逐条测试每条路径,使用foreach
循环,再次捕获 output。 在这里,我将文件夹路径添加到 output,这样您就可以看到哪些文件夹存在,哪些不存在:
$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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.