Powershell脚本不会遍历子文件夹

时间:2018-04-25 16:45:33

标签: powershell

我在线狙击这个脚本,它可以很好地转换父文件夹中的文件。但是,它不会遍历子文件夹。我没有收到任何错误,我已经验证所有文件夹权限都是正确的。另外,我的脚本编码类似于* .docx和* .pptx文件,它们运行成功。然而,这个没有按预期工作。有任何想法吗?

$path = "c:\converted\" 
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] 
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse 
$objExcel = New-Object -ComObject excel.application 
$objExcel.visible = $false 
foreach($wb in $excelFiles) 
{ 
 $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf") 
 $workbook = $objExcel.workbooks.open($wb.fullname, 3) 
 $workbook.Saved = $true 
"converted $wb.fullname" 
 $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath) 
 $objExcel.Workbooks.close() 
 #get rid of conversion copy 
 #Remove-Item $wb.fullname 
} 
$objExcel.Quit() 

1 个答案:

答案 0 :(得分:1)

$excelFiles将包含子文件夹,但您$filepath的构造仅使用原始$path和当前$wb.BaseName,而不考虑当前$wb.FullName可能包含更长的路径。

替换

 $filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf") 

$filepath = $wb.fullname -replace $wb.extension,".pdf"
相关问题