首页 > 解决方案 > 在读取 CSV 和输出 TXT 文件的 PowerShell 脚本中需要我的错误/遗漏的 ID

问题描述

在尝试改进脚本以生成提示文件时,我想出了一个改进,希望能接近理想的 PowerShell 脚本。可悲的是,它输出了一个标题,但随后的(预期的行没有写入文件,它们显示在控制台上。

我想我正在修改外部变量并且未能将它们传递给这里的字符串,但我很困惑。这是我的整个脚本:

Clear-Host
Set-Location "D:\PShell"

$InFile = 'cues.csv'
$OutFile = 'Cues.cue'
$Maker = 'Yours Truly'
$Product = 'Awful Good'


$DocHead1 = 'REM MP3 CueFileRmrks'
$DocHead2 = 'FILE "' + $Product + '.mp3" MP3'

$TrakTitlPrfx = ' TITLE '
$TrakIndxPrfx = ' INDEX 01 '
$TrakRemsPrfx = ' REM GW '


$Parts = @()
    Import-CSV -Delimiter "`t" $InFile

    Set-Content "$OutFile" -Value 'REM GoldWave'
    Add-Content "$OutFile" -Value $TitleHdr

    foreach($item in $Parts)
        {
        "$Nmbr = $($item.Nbr) and $Titl = $($item.Title) and $Time = $($item.Time) and $Dscr = $($item.Description)"

        $Time = $sheet.Cells.Item($i,2).text      # Cue's time location
        $Title = $sheet.Cells.Item($i,3).text     # Cue's name
        $Rmrks = $sheet.Cells.Item($i,4).text    # Text associated with cue

        $TitleLine = $TrakTitlPrfx + "{0:d3}" -f + $i + " " + $Title
        $IndexLine = $TrakIndxPrfx + $Time
        $DescrLine = $TrakRemsPrfx + $Rmrks

        Add-Content "$OutFile" "  TRACK $Nmbr AUDIO"
        Add-Content "$OutFile" "    $TitleLine"
        Add-Content "$OutFile" "    $IndexLine"
        if ($null, $Rmrks -ne $null)
            {
            Add-Content "$OutFile" "    $DescrLine"
            }

        }

任何意见将不胜感激。

标签: powershellcsvtxt

解决方案


首先,您必须将输入文件存储到变量中,即更改:

$Parts = @()
    Import-CSV -Delimiter "`t" $InFile

对此:

$Parts = Import-CSV -Delimiter "`t" $InFile

从哪里获取输出Import-CSV并将其存储在一个变量中,然后迭代(例如 for 循环)。

其次,这个:

"$Nmbr = $($item.Nbr) and $Titl = $($item.Title) and $Time = $($item.Time) and $Dscr = $($item.Description)"

将简单地输出到控制台。你实际上希望它去某个地方,所以你需要Add-Content这样:

Add-Content "$OutFile" -Value "$Nmbr = $($item.Nbr) and $Titl = $($item.Title) and $Time = $($item.Time) and $Dscr = $($item.Description)"

推荐阅读