将字符串搜索的结果附加到现有的 csv 文件
Appending results of string search to existing csv file
我想在多个文件夹中搜索包含特定字符串的文件。每个文件夹的名称都列在具有单个 header (List1) 的 csv 文件 (thelist.csv) 中。我想将搜索结果附加到 thelist.csv 以添加文件名 (Result1) 和目录 (Result2) 列。我正在尝试进入一个 csv 文件(或 excel sheet),以便最终确定哪些文件夹 do/don 不包含“TestString.txt”文件。
示例代码:
$csv = Import-Csv C:\The\Path\thelist.csv -Header List1, Results1, Results2
foreach ($line in $csv) {
$Results1= $line.Results1
$Results2 = $line.Results2
Get-ChildItem -Path "C:\The\Path" -Filter *TestString* -Recurse | Select List1, Results1, Results2 | Export-csv C:\The\Path\thelist2.csv -NoTypeInformation
}
我成功搜索了丢失的字符串 returns 结果包括文件名 (result1) 和目录 (result2)。但是,我在将结果附加到 csv 文件时遇到问题。现在我的代码 returns 原始列表中 (List1) 的空值。原始 csv 文件中唯一存在的数据是 header。
您的代码试图从文件名中提取属性 "List1, Results1, Results2",但没有任何代码告诉它如何做到这一点,因此它们是空的。
您正在尝试在循环内进行导出,删除文件并为每次搜索覆盖它,因此它只会有一个结果。
您可能需要处理一次搜索 returns 多个匹配文件的情况,这需要向 CSV 添加新行。
我没试过这段代码,但这种方法应该更接近你想要的:
# Import from single column file, calling the column 'List1', and process each line:
Import-Csv -LiteralPath C:\The\Path\thelist.csv -Header List1 | ForEach-Object {
# store line with a name, for use later on
$Line = $_
# the folders coming in from the CSV are in the column 'List1', search them
Get-ChildItem -LiteralPath $Line.List1 -Filter *TestString* -Recurse | ForEach-Object {
# for each search result TestString.txt, make a hashtable
# representing a new line in the output CSV, with the new data,
# and convert to PS Custom Object so it will work with Export-CSV later on.
[PSCustomObject]@{
'List1' = $Line.List1
'Result1' = $_.Name
'Result2' = $_.DirectoryName
}
}
# at the end of both loops, export all the results in one go
} | Export-csv C:\The\Path\thelist2.csv -NoTypeInformation
我想在多个文件夹中搜索包含特定字符串的文件。每个文件夹的名称都列在具有单个 header (List1) 的 csv 文件 (thelist.csv) 中。我想将搜索结果附加到 thelist.csv 以添加文件名 (Result1) 和目录 (Result2) 列。我正在尝试进入一个 csv 文件(或 excel sheet),以便最终确定哪些文件夹 do/don 不包含“TestString.txt”文件。
示例代码:
$csv = Import-Csv C:\The\Path\thelist.csv -Header List1, Results1, Results2
foreach ($line in $csv) {
$Results1= $line.Results1
$Results2 = $line.Results2
Get-ChildItem -Path "C:\The\Path" -Filter *TestString* -Recurse | Select List1, Results1, Results2 | Export-csv C:\The\Path\thelist2.csv -NoTypeInformation
}
我成功搜索了丢失的字符串 returns 结果包括文件名 (result1) 和目录 (result2)。但是,我在将结果附加到 csv 文件时遇到问题。现在我的代码 returns 原始列表中 (List1) 的空值。原始 csv 文件中唯一存在的数据是 header。
您的代码试图从文件名中提取属性 "List1, Results1, Results2",但没有任何代码告诉它如何做到这一点,因此它们是空的。
您正在尝试在循环内进行导出,删除文件并为每次搜索覆盖它,因此它只会有一个结果。
您可能需要处理一次搜索 returns 多个匹配文件的情况,这需要向 CSV 添加新行。
我没试过这段代码,但这种方法应该更接近你想要的:
# Import from single column file, calling the column 'List1', and process each line:
Import-Csv -LiteralPath C:\The\Path\thelist.csv -Header List1 | ForEach-Object {
# store line with a name, for use later on
$Line = $_
# the folders coming in from the CSV are in the column 'List1', search them
Get-ChildItem -LiteralPath $Line.List1 -Filter *TestString* -Recurse | ForEach-Object {
# for each search result TestString.txt, make a hashtable
# representing a new line in the output CSV, with the new data,
# and convert to PS Custom Object so it will work with Export-CSV later on.
[PSCustomObject]@{
'List1' = $Line.List1
'Result1' = $_.Name
'Result2' = $_.DirectoryName
}
}
# at the end of both loops, export all the results in one go
} | Export-csv C:\The\Path\thelist2.csv -NoTypeInformation