powershell 输出到 .txt 文件不会打印 for 循环

powershell output to .txt file wont print the for loop

我有一些音频、视频和 jpg 文件。 我正在使用 ffmpeg 来获取所有持续时间

一切都很完美,我从 for 循环中获取了所有持续时间并将其设置为一个变量,当我回显它时,它完美地工作,但是当我将它输出到 .txt 文件时,它只打印最后一个环形 任何建议 下面是我的代码

$file = "D:\edl\input\aa.xlsx"
  $sheetName = "Sheet1"
  #Create an instance of Excel.Application and Open Excel file
  $objExcel = New-Object -ComObject Excel.Application
  $workbook = $objExcel.Workbooks.Open($file)
  $sheet = $workbook.Worksheets.Item($sheetName)
  $objExcel.Visible=$false
  #Count max row
  $rowMax = ($sheet.UsedRange.Rows).count
  #Declare the starting positions
  $rowName,$colName = 1,1
  $rowAge,$colAge = 1,2
  $rowCity,$colCity = 1,3
  $rowMessage,$colMessage = 1,9
  #loop to get values and store it
  for ($i=0; $i -le $rowMax-1; $i++)
  {
  $id = $sheet.Cells.Item($rowName+$i,$colName).text
  $name = $sheet.Cells.Item($rowAge+$i,$colAge).text
  $subtitle = $sheet.Cells.Item($rowCity+$i,$colCity).text
$filename2 = "$id.jpg"
$filename = "$id.mp4"
$filename1 = "$id.m4a"
$duration2 = if ((ffmpeg -i $filename2 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
$duration1 = if ((ffmpeg -i $filename1 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
$duration = if ((ffmpeg -i $filename 2>&1 | Out-String) -match 'Duration:\s+([\d:.]+)') { $matches[1] };
if($duration2){
$duration2 = "02:00:11:15"
$bb = $duration2
$out = "$i" + "  " + "AX" + "   " + "V" + "      " + "C" + "   " + "00:00:00:00" + " " + $bb + "   "+ "* FROM CLIP NAME: " + $id+".jpg"
}
if($duration1){

$out1 = "$i"+ "  " + "AX"  + "   " + "AA" + "     " + "C" + "   " + "00:00:00:00" + " " + $duration1 + "   " + "* FROM CLIP NAME: " + $id+".m4a"
}
if(!$duration){
}else{
$out2 = "$i" + "  " + "AX" + "   " + "AA/V" + "   " + "C" + "   " + "00:00:00:00" + " " + $duration + "   "+ "* FROM CLIP NAME: " + $id+".mp4"
}

$gg = $out+"`n"+"`n"+$out1+"`n"+"`n"+$out2+"`n"
echo $gg
$gg | Out-File -FilePath d:\edl\input\output.txt

  }

  #close excel file
  $objExcel.quit()

当我回显变量 $gg
这是输出

0  AX   V      C   00:00:00:00 02:00:11:15   * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.jpg

0  AX   AA     C   00:00:00:00 00:00:03.48   * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.m4a

0  AX   AA/V   C   00:00:00:00 00:00:07.64   * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4

1  AX   V      C   00:00:00:00 02:00:11:15   * FROM CLIP NAME: 9a52a954-ffe9-4420-a701-68988923f81f.jpg

1  AX   AA     C   00:00:00:00 00:00:03.46   * FROM CLIP NAME: 9a52a954-ffe9-4420-a701-68988923f81f.m4a

0  AX   AA/V   C   00:00:00:00 00:00:07.64   * FROM CLIP NAME: 1aef53e6-92ac-4d28-89f8-4cce28fa0f58.mp4

2  AX   V      C   00:00:00:00 02:00:11:15   * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.jpg

2  AX   AA     C   00:00:00:00 00:00:03.52   * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.m4a

2  AX   AA/V   C   00:00:00:00 00:00:09.75   * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.mp4

3  AX   V      C   00:00:00:00 02:00:11:15   * FROM CLIP NAME: 73aa1f8e-1067-4efd-b2bd-6748a088f33a.jpg

3  AX   AA     C   00:00:00:00 00:00:03.21   * FROM CLIP NAME: 73aa1f8e-1067-4efd-b2bd-6748a088f33a.m4a

2  AX   AA/V   C   00:00:00:00 00:00:09.75   * FROM CLIP NAME: 20b92847-72eb-455d-98ce-6661ed52951e.mp4

4  AX   V      C   00:00:00:00 02:00:11:15   * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.jpg

4  AX   AA     C   00:00:00:00 00:00:03.05   * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.m4a

4  AX   AA/V   C   00:00:00:00 00:00:06.93   * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.mp4

但我的 txt 文件输出

4  AX   V      C   00:00:00:00 02:00:11:15   * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.jpg

4  AX   AA     C   00:00:00:00 00:00:03.05   * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.m4a

4  AX   AA/V   C   00:00:00:00 00:00:06.93   * FROM CLIP NAME: 1877c551-ceef-48e9-a1a5-00629a9964eb.mp4

默认情况下,如果指定路径中存在文件,Out-File 会覆盖该文件 且不会发出警告。使用 -Append parameter将输出添加到现有文件的末尾):

$gg | Out-File -FilePath d:\edl\input\output.txt <b>-Append</b>

顺便说一句,作为

的替代品
<strike>echo $gg
$gg | Out-File -FilePath d:\edl\input\output.txt -Append
</strike>

使用Tee-Object cmdlet它将输出存储在一个文件或变量中,同时也将它发送到管道中。如果Tee-Object是管道中的最后一个命令,则该命令输出显示在提示):

$gg | Tee-Object -FilePath d:\edl\input\output.txt -Append