AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1668087
Accepted
ASD
ASD
Asked: 2021-08-08 08:15:14 +0800 CST2021-08-08 08:15:14 +0800 CST 2021-08-08 08:15:14 +0800 CST

PDFTK 连发,初始文件名控制

  • 772

使用 PDFTKburst将多页 PDF 文件转换为单个文件,我需要一种方法来允许我控制第一个文件名和它为输出文件命名约定开始的数字。

例如,我希望它提取到自己的 PDF 文件的第一页的初始文件名具有以下名称:“ e00526.pdf”。这样,它会逐步“爆破”多页 PDF 文件:“ e00526.pdf”、“ e00527.pdf”、“ e00528.pdf”等输出文件名明智。

现在这就是我所拥有的:

#[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
#$firsFile = [Microsoft.VisualBasic.Interaction]::InputBox("Enter first file name:", "File name")

$firstFile = "e00526"

$QRDir = "C:\1_PDF"

chdir $QRDir

$output = "e00526%d.pdf"

pdftk input.pdf burst output $output

但是,这给出了一个连发文件输出命名标准,1在提取到其自己的 PDF 文件的第一页的基本文件名的末尾附加了一个。然后它为每个后续提取的页面增加附加1的。1

所以这就是这段代码的作用:“ e005261.pdf”、“ e005262.pdf”、“ e005263.pdf”等。

我需要什么:“ e00526.pdf”、“ e00527.pdf”、“ e00528.pdf”等。

1因此,与其在6文件名中附加 ,不如将其增加到6a7等,但从提取的第二个页面开始,因此我控制起始页面1文件名。


更新

由于这个排序问题,我认为这不会像那样工作:

排序问题

“e005261.pdf”应该变成“e00526.pdf”,“e0052611.pdf”应该变成“e00537.pdf”

这就是为什么我认为如果我们找到一种方法给它第一个文件名并让它在之后增加1位数字会使整个事情变得容易得多。

powershell pdftk
  • 3 3 个回答
  • 363 Views

3 个回答

  • Voted
  1. Best Answer
    Vomit IT - Chunky Mess Style
    2021-08-08T12:47:54+08:002021-08-08T12:47:54+08:00

    下面是一个文件重命名后 PDFTK 操作解决方案,它将使用逻辑来获取基本文件名部分并将它们设置为int数据类型,为您想要的增量数值计算这些,然后将它们相应地连接在一起以用于新的基本文件每个文件的。

    电源外壳

    注意:从下面的 PowerShell 中删除-Whatif以运行逻辑,而不是告诉您如果运行会发生什么。此外,如果您需要递归遍历其中的子文件夹,C:\1_PDF\可以-Recurse在命令中添加参数Get-ChildItem(即Get-ChildItem -Path "C:\1_PDF\*.pdf" -Recurse)。

    Get-ChildItem -Path "C:\1_PDF\*.pdf" | % { Process { 
        $firstFile = [string]$_.Basename;
        Try { [int]$fnameF = $firstFile[1..5] -join ""} Catch {};
        Try { [int]$fnameL = $firstFile[6..99] -join ""} Catch {};
        $f = Try { [convert]::ToInt32($firstFile[6..99] -join "", 10) } Catch {};
        If ( $f -is [int] ) { 
            Rename-Item -Path $_.FullName -NewName "$($_.BaseName[0])$(([string]($fnameF+$fnameL-1)).PadLeft(5,"0"))$($_.Extension)" -WhatIf;
            } Else {
            Write-Host "$($_.BaseName)$($_.Extension) does not end with numerical digits in its basename." -ForegroundColor Yellow 
            };
        }}; 
    

    支持资源

    • Get-ChildItem通常返回一个FileInfo对象。这些对象包含一些有用的属性——其中包括...

      • 完整路径、文件名和扩展名 ( FullName),
      • 仅路径(DirectoryName),
      • 文件名 ( Name),以及
      • 不带扩展名的文件名 ( BaseName)

      资源

    • 重命名项目

    • 定义 PowerShell 数据类型

    • PowerShell 运算符 -查找 PowerShell 数组中值的索引号

    • 尝试 {...} 捕捉 {...} 最后 {...}

    • about_Arithmetic_Operators

    • String.PadLeft 方法

    • 0
  2. Silbee
    2021-08-08T14:32:11+08:002021-08-08T14:32:11+08:00

    让我们像上次一样尝试。

    正如您所描述的那样,重命名文件将成为一个问题,所以让我们考虑一种不同的方式。

    我们可以逐页提取并为每一页定义我们自己的文件名。我们需要的第一件事是找到页数,这应该使用以下命令:

    $totalPages = pdftk $firstFile dump_data | Select-String "NumberOfPages"
    

    现在让我们设置一些东西来开始。

    #First we determine our starting number by dropping the first letter from the filename and turning it into an integer.
    $startingNumber = [int]$firstFile.substr(1)
    $currentNumber = $startingNumber
    
    #Now we iterate through every page we want to extract
    For ($i = 1; i -le $totalPages; i++) {
        #create an output name based on the current number
        $outName = "e" + $currentNumber.ToString('00000') + ".pdf"
    
        #output the one page
        pdftk input.pdf cat $currentNumber output $outName
    
        #increment currentNumber with one
        $currentNumber++
    }
    
    • 0
  3. ASD
    2021-08-08T14:51:32+08:002021-08-08T14:51:32+08:00

    不工作

    这就是我所做的,稍微改变了你的代码:

    $WorkDir = "D:\Bank\QR_ES\1_ES_Test"
    
    chdir $WorkDir
    
    $totalPages = pdftk QR.pdf dump_data | Select-String "NumberOfPages"
    
    #First we determine our starting number by dropping the first letter from the filename and turning it into an integer.
    $startingNumber = [int]$firstFile.substr(1)
    $currentNumber = $startingNumber
    
    #Now we iterate through every page we want to extract
    For ($i = 1; i -le $totalPages; i++) {
        #create an output name based on the current number
        $outName = "e" + $currentNumber.ToString('00000') + ".pdf"
    
        #output the one page
        pdftk input.pdf cat $currentNumber output $outName
    
        #increment currentNumber with one
        $currentNumber++
    }
    

    这是错误代码,虽然它是德语:

    PS D:\Bank\QR_ES\1_ES_Test> C:\Users\Sasha\Desktop\Test.ps1
    Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
    In C:\Users\Sasha\Desktop\Test.ps1:8 Zeichen:1
    + $startingNumber = [int]$firstFile.substr(1)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    i : Die Benennung "i" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms 
    erkannt. Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den 
    Vorgang.
    In C:\Users\Sasha\Desktop\Test.ps1:12 Zeichen:14
    + For ($i = 1; i -le $totalPages; i++) {
    +              ~
        + CategoryInfo          : ObjectNotFound: (i:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    
    • 0

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve