从PowerShell循环添加内容到具有不同列的CSV文件

时间:2017-11-29 17:36:33

标签: powershell csv

我目前有一个脚本会遍历文件夹中的一组文件 - 打开每个文件,让我输入一个新的文件名,然后重命名。然后,我尝试在一列中创建一个旧文件名的CSV文件,并在第二列中创建该文件的新名称。

#[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Add-Type -AssemblyName Microsoft.VisualBasic 

$folderpath = 'C:\Scans\docs\' '#
$items = Get-ChildItem -Recurse $folderpath *.pdf
$newFileName = "" 
$counterID = 0
$amountOfScans = $items.Length

foreach( $i in $items)  {
    Start-Process ((Resolve-Path ("$folderpath$i")).Path)

    Start-Sleep -Seconds 1

    $counterID++ 

    $newFileName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the new file name $counterID / $amountOfScans :", $i) 

    Stop-Process -Name "Acro*"

    Add-Content -Path 'C:\Scans\fileNames.csv' -Value "$i","$newFileName "

    Rename-Item $i.FullName ("$newName.pdf")
}

这确实有效,但只在第一列输出到csv,如:

old file name 1
new file name 1
old file name 2
new file name 2

如何输出:

old file name 1 | new file name 1
old file name 2 | new file name 2

1 个答案:

答案 0 :(得分:1)

您正在传递数组。 CSV只不过是一个文本文件,因此您希望将整行作为单个字符串传递。

Add-Content -Path 'C:\Scans\fileNames.csv' -Value "`"$i`",`"$newFileName`""