powershell 尝试创建文件名数组

eqoofvh9  于 2023-03-18  发布在  Shell
关注(0)|答案(3)|浏览(205)

我正在尝试使用PSWritePDF模块来合并PDF文件。我有大约64个文件夹,每个文件夹都有大约20+个文件需要合并。最终,我会有64个PDF文件,每个文件夹包含64个文件夹中的合并文件。我已经写了一些代码,但我正在努力创建一个文件名数组,我可以传递给合并PDF函数。我知道这个代码的第一部分是多余的,只是还没有修复它。

#https://github.com/EvotecIT/PSWritePDF/blob/master/Example/Example03.Merging/Example03.ps1

#This gives me the 64 folder names
$folder_NM = Get-ChildItem -Path \\main_directory\CURRENT |
           Where-Object {$_.PSIsContainer} |
           Foreach-Object {$_.Name}

#This iterates through the 64 folders
foreach ($X IN $folder_NM)
{
#this grabs each of the 64 directories
$main_path = join-path -path \\main_directory\CURRENT -ChildPath $X

#This grabs the names of the pdfs in each folder
$file_names = Get-ChildItem $main_path |
    ForEach-Object {$_.Name}
    
   #This is grabbing each file in the folder and giving me the formatted string I need to pass to Merge-PDF. i.e. C:\\User\Current\pdf.1
   foreach($Y in $file_names){
   $idv_files = join-path -path $main_path -ChildPath $Y
   #This is where I am stuck. I am trying to create an array with each filename comma separated. This currently just overwrites itself each time it goes through the loop.
   $arr = $idv_files-join','
   
    #This is needed for mergePDF
    $OutputFile = "$maindirectory\TESTING\$X.pdf"
    #This only puts the most recent file in the output file. Thus the need for an array of file names.
   Merge-PDF -InputFile $arr -OutputFile $OutputFile

   #Debugging
   #Write-Host $arr
   }

}

具体来说,这就是我的问题所在。我正在$idv_files中获取正确的文件,如果我在Merge-PDF中使用这些文件,那么我只会得到一个PDF,其中包含最后处理的一个文件。我想我只需要将它们以逗号分隔,并全部放入同一个数组中,以便Merge-PDF将它们合并在一起。

foreach($Y in $file_names){
   $idv_files = join-path -path $main_path -ChildPath $Y
   #This is where I am stuck. I am trying to create an array with each filename comma separated. This currently just overwrites itself each time it goes through the loop.
   $arr = $idv_files-join','

任何帮助。非常新的powershell!

1szpjjfi

1szpjjfi1#

未经测试,但是,如果函数像我的评论中那样将[string[]]作为输入,这应该会在每个文件夹上得到一个MERGED PDF.pdf
我建议您在尝试使用FS之前,先在本地主机上使用几个包含pdf文件的文件夹进行测试。

# Get the Directories
$folder_NM = Get-ChildItem -Path \\main_directory\CURRENT -Directory

#This iterates through the 64 folders
foreach ($dir IN $folder_NM)
{
    # This gets you the array of PDF Files
    $file_names = Get-ChildItem $dir.FullName -Filter *.pdf -File |
                  Sort-Object Name
    
    # Define the output file for Merged PDF
    $OutputFile = Join-Path $dir.FullName -ChildPath 'MERGED PDF.pdf'
    
    # If Merge-PDF takes [string[]] as input, this should work
    Merge-PDF -InputFile $file_names.FullName -OutputFile $OutputFile
}
plupiseo

plupiseo2#

您似乎希望合并的.pdf文件为子目录名称+“. pdf”。可能我误解了。这也是未经测试的,但可能符合您的要求。使用当前的Windows PowerShell 5.1或任何PowerShell核心,无需测试.PSIsContainer。Get-ChildItem支持-File和-Directory开关。

[CmdletBinding()]
param ()
$RootDir = '\\main_directory\CURRENT'
# Get the subdirectory list.
Get-ChildItem -Directory -Path $RootDir |
    # Process each subdirectory.
    ForEach-Item {
        # Create an array of the .pdf files to be merged.
        $file_names = (Get-ChildItem -File -Path $_.FullName -Filter '*.pdf').FullName
        #This is needed for mergePDF
        $OutputFile = Join-Path -Path $RootDir -ChildPath $($_.Name + '.pdf')
        Write-Verbose "OutputFile is $OutputFile"
        Merge-PDF -InputFile $file_names -OutputFile $OutputFile
    }
fykwrbwg

fykwrbwg3#

我不是每次都编写一个新的自定义模块,而是简单地从批处理文件中运行一个generic.ps1脚本。(正如所写的那样,它需要文件集排序为00-99)然而,不需要100,如果超过100,可以通过快速第二次传递在输出上运行不止一次,如merged 100 -00 merged 100-##等。因此,最多可以快速运行9999个文件。
所以基本脚本是PDFmerge##.ps1(我确信它的语法不好,但是可以工作!)

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
$Fin = $args[0]
$Ext = $args[1]
$Out = $args[2]
echo "Merging $Fin##$Ext to $Out"
for ($i=0; $i -lt 10; $i++) {
    for ($j=0; $j -lt 10; $j++) {
        New-Variable -Name "F$i$j" -Value "$Fin$i$j$Ext"
    }
}
echo Processing
Merge-PDF -OutputFile $Out -InputFile $F00, $F01, $F02, $F03, $F04, $F05, $F06, $F07, $F08, $F09, $F10, $F11, $F12, $F13, $F14, $F15, $F16, $F17, $F18, $F19, $F20, $F21, $F22, $F23, $F24, $F25, $F26, $F27, $F28, $F29, $F30, $F31, $F32, $F33, $F34, $F35, $F36, $F37, $F38, $F39, $F40, $F41, $F42, $F43, $F44, $F45, $F46, $F47, $F48, $F49, $F50, $F51, $F52, $F53, $F54, $F55, $F56, $F57, $F58, $F59, $F60, $F61, $F62, $F63, $F64, $F65, $F66, $F67, $F68, $F69, $F70, $F71, $F72, $F73, $F74, $F75, $F76, $F77, $F78, $F79, $F80, $F81, $F82, $F83, $F84, $F85, $F86, $F87, $F88, $F89, $F90, $F91, $F92, $F93, $F94, $F95, $F96, $F97, $F98, $F99
echo "Done Merging $F00 - $F99"

并进行类似调用,以合并所有“dev:/input path/partBefore”##“partAfter.pdf”

powershell -file PDFmerge##.ps1 "input/partBefore" "partAfter.pdf" "output/Parts01.pdf"

例如,对于/folder/##.pdf,将简单地是“/folder/”“.pdf”“/folder/out-01.pdf”,其本身可以按照其它方法是文件夹的循环列表。

相关问题