powershell - 在读取 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"
}
}
任何意见将不胜感激。
解决方案
首先,您必须将输入文件存储到变量中,即更改:
$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)"
推荐阅读
- json - 如何在 vuejs 中访问嵌套 json 的内部元素
- javascript - 忽略/删除数组计数中的空值、未定义值和空值 - Javascript
- sql - SQL Server:为什么变量在 IF 之外起作用
- python - 当终端显示“检查日志以获取完整的命令输出”时,我该怎么办?
- python - Mtaplotlib:TypeError:对象似乎不是 8 位字符串路径或 Python 文件类对象
- python - 正则表达式在带连字符的字符串周围添加引号
- excel - 手动选择时保存组合框值
- flutter - 如何禁用警告“省略局部变量的类型注释”?在 Dart 中(带有 Pedantic 包)
- json - json2typescript serializeObject 方法返回空对象
- c - 将二进制文件读取到C中的二维数组