循环麻烦

时间:2019-01-18 20:31:51

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

我正在尝试将所有PDF文件拆分到一个文件夹中,然后将其移动到输出文件夹中。问题是,我正在使用的程序(PDFtk)一次只能拆分一个文件。

如果只有1个文件,则效果很好,但是用户经常一次扫描多个文件,这会导致PowerShell脚本耸耸肩,只执行输出移动而不会拆分文件。我使用的是ForEach吗?

$pdfPath = 'C:\Temp\Incoming'
$pdfoutPath = 'C:\Temp\Completed'
$pdfFile = Join-Path $pdfPath '*.pdf'
$SetsOfPages = 1
$Match = 'NumberOfPages: (\d+)'
$NumberOfPages = [regex]::Match((pdftk $pdfFile dump_data), $Match).Groups[1].Value
"{0,2} pages in {1}" -f $NumberOfPages, $pdfFile

Get-ChildItem $pdfFile | ForEach-Object {
    for ($Page=1; $Page -le $NumberOfPages; $Page+=$SetsOfPages) {
        $File = Get-Item $pdfFile
        $Range = "{1}" -f $page, [Math]::Min($Page+$SetsOfPages-1, $NumberOfPages)
        $OutFile = Join-Path $pdfoutPath ($File.BaseName + "_$Range.pdf")
        "processing: {0}" -f $OutFile
        pdftk $pdfFile cat $Range output $OutFile
    }
    Get-ChildItem $pdfoutPath '*.pdf' -Recurse | foreach {
        $new_folder_Year = Get-Date $_.LastWriteTime -Format yyyy
        $new_folder_Month = Get-Date $_.LastWriteTime -uformat %m
        $new_folder_Day = Get-Date $_.LastWriteTime -uformat %d
        $des_path = "${pdfoutPath}\${new_folder_Year}\${new_folder_Month}\${new_folder_Day}"

        if (Test-Path $des_path){ 
            Move-Item $_.FullName $des_path 
        } else {
            New-Item -ItemType Directory -Path $des_path
            Move-Item $_.FullName $des_path 
        }
    }
    Get-ChildItem $pdfPath '*.pdf' -Recurse | foreach {
        $new_folder_Year = Get-Date $_.LastWriteTime -Format yyyy
        $new_folder_Month = Get-Date $_.LastWriteTime -uformat %m
        $new_folder_Day = Get-Date $_.LastWriteTime -uformat %d
        $des_path = "${pdfoutPath}\${new_folder_Year}\${new_folder_Month}\${new_folder_Day}"

        if (Test-Path $des_path){ 
            Move-Item $_.FullName $des_path 
        } else {
            New-Item -ItemType Directory -Path $des_path
            Move-Item $_.FullName $des_path 
        }
    }

1 个答案:

答案 0 :(得分:0)

如果我解释您的代码,请尝试正确执行此脚本:

## Q:\Test\2019\01\18\SO_542610444.ps1
$pdfPath    = 'C:\Temp\Incoming'
$pdfoutBase = 'C:\Temp\Completed'
$pdfFile    = Join-Path $pdfPath '*.pdf'

Get-ChildItem $pdfFile | ForEach-Object {
    "processing: {0}" -f $_.FullName
    $pdfOutPath = Join-Path $pdfoutBase $_.LastWriteTime.ToString('yyyy\\MM\\dd')
    MD $pdfoutPath | Out-Null
    $OutFile = Join-Path $pdfoutPath ("{0}_%03d.pdf" -f $_.BaseName)
    &pdftk "$($_.FullName)" Burst output $OutFile
}

它从LastWriteTime(yyyy \ MM \ dd)构建文件夹结构,并将页码附加3个位置附加到BaseName。

使用此输入文件:

> gci .\Temp\Incoming\

    Directory: C:\Temp\Incoming

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-10-27     14:17       17540001 cc.18.01.pdf

运行脚本后的示例树:

tree /F
C:.
└───Temp
    ├───Completed
    │   └───2018
    │       └───10
    │           └───27
    │                   cc.18.01_001.pdf
    │                   cc.18.01_002.pdf
%<...snip...>%
    │                   cc.18.01_155.pdf
    │                   cc.18.01_156.pdf
    │
    └───Incoming
            cc.18.01.pdf