gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:58:25 28 4
gpt4 key购买 nike

我有一些音频、视频和 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 -Append

顺便说一句,作为替代

   
    echo $gg
    
$gg | Out-File -FilePath d:\edl\input\output.txt -Append

使用 Tee-Object cmdlet (它将输出存储在文件或变量中,并将其发送到管道中。如果 Tee-Object 是管道中的最后一个命令,则命令输出将显示在提示符处):
$gg | Tee-Object -FilePath d:\edl\input\output.txt -Append

关于excel - powershell 输出到 .txt 文件不会打印 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61238074/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com