试图创建一个文件名数组

时间:2021-07-26 21:06:18

标签: powershell pdf

我正在尝试使用 PSWritePDF 模块来合并 pdf。我有大约 64 个文件夹,每个文件夹都有大约 20 多个需要合并的文件。最后,我将有 64 个 pdf,每个 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 非常陌生!

2 个答案:

答案 0 :(得分:1)

未经测试,但是,如果函数将 >>> np.where(thresh > 5, (thresh - 1) % 5 + 1, thresh) array([1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]) 作为输入,如我的评论中那样,这应该会在每个文件夹上获得一个 [string[]]

在尝试使用 FS 之前,我建议您在本地主机上使用一些包含 pdf 文件的文件夹进行测试。

MERGED PDF.pdf

答案 1 :(得分:0)

您似乎希望合并的 .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
    }