我有一个脚本来合并pdf' s,如果它们在同一个文件夹中。我试图将最终合并的pdf的名称更改为它所在目录的名称,但我不熟悉powershell。有人可以帮忙吗?
$PDFtk = "C:\pdktk\pdftk.exe"
$inputfolder = "C:\pdftest"
$outputfile = "combined_file.pdf"
dir $inputfolder -r -include *.pdf -exclude $outputfile | sort-object | group DirectoryName |
% {& $PDFtk $_.group CAT OUTPUT "$($_.Name)\$outputfile" verbose}
结束结果:
pdftest\
Folder 1\
1.pdf
2.pdf
3.pdf
Folder 1.pdf <--merged pdf
Folder 2\
4.pdf
5.pdf
Folder 2.pdf <--merged pdf
答案 0 :(得分:0)
您必须将$outputfile
作业移至%
循环内。我也分开获取文件夹,从每个文件夹中获取文件。现在有两个Get-ChildItem&#39;命令(或dir
):
$PDFtk = "C:\pdktk\pdftk.exe"
$inputfolder = "C:\pdftest"
Get-ChildItem $inputfolder -Recurse | ? {$_.PSIsContainer } | % {
$currentfolder = $_
$outputfile = "$($currentfolder.Name -split '\\' | Select -Last 1).pdf"
Get-ChildItem $currentfolder.FullName -Exclude $outputfile | ? {$_.Extension -ieq ".pdf"} | % {
& $PDFtk "$($currentfolder.FullName)\$($_.Name)" CAT OUTPUT "$($currentfolder.FullName)\$outputfile" verbose
}
}
答案 1 :(得分:0)
以下代码应该可以正常工作:
$pdftk = 'C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe'
$root = 'E:\posh\test'
gci $root -Include *.pdf -Recurse |
group DirectoryName |
% {
$dir = gi $_.Name
$al = $_.Group | % { $_.FullName }
$al += 'cat', 'output'
$al += Join-Path $dir.FullName "$($dir.BaseName).pdf"
$al += 'verbose'
saps $pdftk $al -Wait
}