试图创建一个文件名数组

Trying to create an array of filenames

我正在尝试使用 PSWritePDF 模块合并 pdf。我有大约 64 个文件夹,每个文件夹都有大约 20 多个需要合并的文件。最后,我将有 64 个 pdf,每个包含 64 个文件夹中每个文件夹的合并文件。我已经编写了一些代码,但我正在努力创建一个可以传递给 Merge-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 非常新!

未经测试,但是,如果该函数像我评论中那样采用 [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
}

您似乎希望合并后的 .pdf 文件为子目录名称 + '.pdf'。也许我误解了。这也是未经测试的,但可能会做你想做的。使用当前的 Windows PowerShell 5.1 或任何 PowerShell Core,无需测试 .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
    }