在读取 CSV 和输出 TXT 文件的 PowerShell 脚本中需要我的 errors/omissions ID
Need ID of my errors/omissions in PowerShell script reading CSV and outputing TXT file
在尝试改进生成提示文件的脚本时,我想出了一个改进,我希望它能接近理想的 PowerShell 脚本。可悲的是,它输出 header 但随后的(预期行未写入文件,它们显示在控制台上。
我想我正在修改外部变量,但未能将它们传递给 here-string,但我很困惑。这是我的整个脚本:
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"
}
}
如有任何意见,我们将不胜感激。
首先,您必须将输入文件存储到一个变量中,即更改为:
$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)"
在尝试改进生成提示文件的脚本时,我想出了一个改进,我希望它能接近理想的 PowerShell 脚本。可悲的是,它输出 header 但随后的(预期行未写入文件,它们显示在控制台上。
我想我正在修改外部变量,但未能将它们传递给 here-string,但我很困惑。这是我的整个脚本:
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"
}
}
如有任何意见,我们将不胜感激。
首先,您必须将输入文件存储到一个变量中,即更改为:
$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)"