0
$SearchFolder = "C:\Test\New folder\*.txt" 


$CSVFilePath = "C:\Test\txtfiles" # this doesn't work... 


$File = Select-String -Path $SearchFolder -Pattern new-print-server, default, dymo, zebra -NotMatch | select line, filename



$File | foreach-object {
$_.line = $_.line.replace("^",",")

}


$File | export-csv -path $CSVFilePath -NoTypeInformation


$csv = Get-Content -path $CSVFilePath



$csv | Out-File $CSVFilePath -Encoding utf8


Invoke-Item $CSVFilePath

这是我从网上获得的脚本,我想将一个文本文件文件夹转换为 csv 文件。此代码仅允许我将所有 txt 文件放入一个 csv 文件中。我想将多个 txt 文件转换为多个具有相同名称的 csv 文件。

4

1 回答 1

2

对于 Select-String 您需要引用 Pattern 例如:

Select-String -Path $SearchFolder -Pattern 'new-print-server, default, dymo, zebra' -NotMatch | select line, filename

看看这是否能让你更接近:

$files = Get-ChildItem "C:\Test\New folder\*.txt"
foreach ($file in $files) {
    $notMatchedLines = $file | Select-String -Pattern 'new-print-server, default, dymo, zebra' -NotMatch | select line,filename
    $notMatchedLines | foreach {$_.line = $_.line.replace("^",",")}
    $notMatchedLines | Export-Csv ([io.path]::ChangeExtension($file.Fullname, '.csv')) -NoTypeInformation -Encoding UTF8
}
于 2013-09-27T16:09:59.137 回答