[英]powershell output to .txt file wont print the for loop
我有一些音頻、視頻和 jpg 文件。 我正在使用 ffmpeg 來獲取所有持續時間
一切都很完美,我從 for 循環中獲得了所有持續時間並將其設置為一個變量,當我回顯它時,它運行良好,但是當我將 output 它轉換為 a.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
這是 output
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 文件 output
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
參數(將 output 添加到現有文件的末尾):
$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 (它將 output 存儲在文件或變量中,並將其發送到管道中。如果Tee-Object
是管道中的最后一個命令,則在提示符處顯示命令 output ):
$gg | Tee-Object -FilePath d:\edl\input\output.txt -Append
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.