【问题标题】:PowerShell: How to format a script that renames pdf's based on the number of rows in a csv file?PowerShell:如何格式化根据 csv 文件中的行数重命名 pdf 的脚本?
【发布时间】:2021-12-10 07:19:01
【问题描述】:

我有一个 PowerShell 脚本,它将根据压缩(压缩)文件夹的名称重命名 pdf。压缩(压缩)文件夹内是一个 excel (.csv) 文件。

例如:

压缩(压缩)文件夹 - 23579_ADV.zip(包含 excel (.csv) 文件 - ADV_Mailer.csv)

pdf - ADVVPrintOutput_0001 & ADVVPrintOutput_0002

最终结果 - ADV_23579_Pr01_1-5000.pdf & ADV_23579_Pr02_5001-10000.pdf

由于包含的 csv 有 8750 行,因此所需的结果:

ADV_23579_Pr01_1-5000.pdf & ADV_23579_Pr02_5001-8750.pdf

以下 PowerShell 脚本的问题是 pdf 名称需要具有包含在 excel (.csv) 文件中的可变数量的行。 pdf名称需要按5000分解。下面是脚本:

If (Test-Path D:\WORK\JetLetter\ABC\ABC_VL_Hot\*.zip) {
    $source=Get-ChildItem "D:\WORK\JetLetter\ABC\ABC_VL_Hot\*.zip" | select  -Last 1
    $JobID = "ABC_" + $source.BaseName.Split('_')[0]
    Echo $source.name
    Echo $JobID
    
    If (Test-Path ABCPrintOutput0001.pdf) {
        Rename-Item ABCPrintOutput0001.pdf "$($JobID)_Pr01_1-5000.pdf"
    }
    If (Test-Path ABCPrintOutput0002.pdf) {
        Rename-Item ABCPrintOutput0002.pdf "$($JobID)_Pr02_5001-10000.pdf"
    }

我找到了一个可以计算行数的命令行。我只需要一种将结果传达给 Rename-Item 的方法。

Import-Csv *.csv | Measure-Object | Select-Object -expand count

根据 Dilly B 的回答,我重新格式化了 PowerShell 脚本。

If (Test-Path D:\WORK\JetLetter\JWH\JWH_Tr_Hot\*.zip){

$source=Get-ChildItem "D:\WORK\JetLetter\JWH\JWH_Tr_Hot\*.zip" | select  -Last 1
Expand-Archive -Path *.zip -DestinationPath D:\WORK\JetLetter\JWH\JWH_Tr_Hot\
Rename-Item -Path "JWH_Mailer.csn" -NewName "JWH_Mailer.csv"
$csvFile = 'D:\WORK\JetLetter\JWH\JWH_Tr_Hot\JWH_Mailer.csv'
$lineCount = Import-Csv $csvFile | Measure-Object | Select-Object -expand count #Counting lines of a CSV File
$JobID = "JWH_" + $source.BaseName.Split('_')[0]
Echo $source.name
Echo $JobID


If (Test-Path JWHPrintOutput_0001.pdf){
    if( $lineCount -lt 5000 ){
        Rename-Item JWHPrintOutput_0001.pdf "$($JobID)_Pr01_1" + $lineCount + ".pdf"}
    elseif( $lineCount -gt 5000){
        Rename-Item JWHPrintOutput_0001.pdf "$($JobID)_Pr01_1-5000.pdf"}
}

If (Test-Path JWHPrintOutput_0002.pdf){
    if( $lineCount -lt 10000 ){
        Rename-Item JWHPrintOutput_0002.pdf "$($JobID)_Pr02_5001" + $lineCount + ".pdf"}
    elseif( $lineCount -gt 10000){
        Rename-Item JWHPrintOutput_0002.pdf "$($JobID)_Pr02_5001-10000.pdf"}
}
}

当我运行脚本时,它给了我以下错误。

Expand-Archive:路径 '' 要么不存在,要么不是有效的文件系统路径。 在 D:\WORK\JetLetter\JWH\JWH_Tr\PrtOutpt_NameChange.ps1:4 char:1

  • Expand-Archive -Path *.zip -DestinationPath D:\WORK\JetLetter\JWH\JWH ...
  •   + CategoryInfo          : InvalidArgument: (:String) [Expand-Archive], IOException
      + FullyQualifiedErrorId : PathNotFound,Expand-Archive
    
    

重命名项目:无法重命名,因为“JWH_Mailer.csn”中的项目不存在。 在 D:\WORK\JetLetter\JWH\JWH_Tr\PrtOutpt_NameChange.ps1:5 char:1

  • 重命名-项目-路径“JWH_Mailer.csn”-NewName“JWH_Mailer.csv”
  •   + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
      + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
    
    

134721_JWH.zip JWH_134721

【问题讨论】:

  • 您好,请您改写您的问题。对不起!它与所有 CSV 文件和 Zip 文件有点混淆。
  • 我已经编辑了问题,以便以更好的格式解释我遇到的问题。
  • 嗨,我有两个问题。 1. 你如何读取 zip 文件夹中的 CSV 文件而不解压它? 2. 此 Zip 文件是否包含多个带有相应 CSV 文件的 PDF 文件,以及您如何匹配这些名称。或者它只包含一个 PDF 文件和一个 CSV 文件。
  • 嗨,Dilly B。感谢您的提问。最初,该脚本仅读取 zip 文件夹的名称。提前考虑,需要添加一个命令来提取 zip 文件夹。类似于:Expand-Archive -LiteralPath。 zip 文件仅包含一个 csv 文件。要重命名的 pdf 文件位于名为“ABC_VL_9835”的 $SourceDir 中。源文件夹将根据 csv 文件中有多少行并按 5000 行细分,包含可变数量的 pdf。
  • 嗨,@cnjfo672 从错误中我们可以看出路径错误或文件不存在。如果您正在查找文件,请尝试使用带有 -leaf 的 Test-Path 或使用带有通配符的 Get-ChildItem 并选择您要解压缩的文件。您也可以使用 Set-Path 选择当前工作目录

标签: powershell pdf


【解决方案1】:

我仍然不确定您将如何匹配您的 csv 文件和 pdf 文件。我给了你一个一对一匹配的参考脚本。

$csvFile = 'C:\PS\Sample-Spreadsheet-5000-rows.csv' #CSV file having 5000 lines
$pdfFile = 'C:\PS\Sample.pdf' # A PDF file
$pdfFileName = (Get-Item $pdfFile).BaseName #Get pdf file basename
$lineCount = Import-Csv $csvFile | Measure-Object | Select-Object -expand count #Counting lines of a CSV File

if(Test-Path $pdfFile){
    # Condition to check if linecount is lesser than equal to 5000
    if( $lineCount -lt 5000 ){
        Rename-Item $pdfFile "ABC_$($pdfFileName)_Pro01_1-5000.pdf"
    }
    # Condition to check if linecount is greater than equal to 5000 or lesser than equal to 10000
    elseif( ($lineCount -gt 5000) -and ($lineCount -lt 10000) ) {
        Rename-Item $pdfFile"ABC_$($pdfFileName)_Pro02_5001-10000.pdf"
    }
} else {  #If File Not found
    Write-Output "File Not Found"
}

如果您有多个 PDF 和 CSV,请使用 foreach 并继续。 谢谢!

【讨论】:

  • 您好 Dilly B。感谢您提供的答案。它确实帮助我更好地理解语言。我重写了脚本并将其放在我原来的问题中。但是,我似乎无法弄清楚我得到的错误。
【解决方案2】:

多亏了 Dilly B,我才能够弄清楚这一点。这是在可变数据印刷环境中。它根据艺术和邮寄记录生成多重 pdf。 下面的 PowerShell 查看邮件列表并根据邮件列表中的记录数重命名 pdf。

If (Test-Path D:\WORK\JetLetter\ABC\ABC_A001_Hot\*.zip){

$source=Get-ChildItem "D:\WORK\JetLetter\ABC\ABC_A001_Hot\*.zip" | select  -Last 1
Expand-Archive -Path D:\WORK\JetLetter\ABC\ABC_A001_Hot\*.Zip -DestinationPath D:\WORK\JetLetter\ABC\ABC_A001_Hot\ -Force
Rename-Item -Path "D:\WORK\JetLetter\ABC\ABC_A001_Hot\ABC_Mailer.csn" -NewName "D:\WORK\JetLetter\ABC\ABC_A001_Hot\ABC_Mailer.csv"
$csvFile = 'D:\WORK\JetLetter\ABC\ABC_A001_Hot\ABC_Mailer.csv'
$lineCount = Import-Csv $csvFile | Measure-Object | Select-Object -expand count #Counting lines of a CSV File
$JobID = "ABC_" + $source.BaseName.Split('_')[0]
Echo $source.name
Echo $JobID

If (Test-Path D:\WORK\JetLetter\ABC\ABC_A001_9835\ABCPrintOutput0001.pdf){
    if( $lineCount -lt 5000 ){
        Rename-Item ABCPrintOutput0001.pdf "$($JobID)_Pr01_1_$lineCount.pdf"}
    elseif( $lineCount -gt 5000){
        Rename-Item ABCPrintOutput0001.pdf "$($JobID)_Pr01_1-5000.pdf"}
}

If (Test-Path D:\WORK\JetLetter\ABC\ABC_A001_9835\ABCPrintOutput0002.pdf){
    if( $lineCount -lt 10000 ){
        Rename-Item ABCPrintOutput0002.pdf "$($JobID)_Pr02_5001_$lineCount.pdf"}
    elseif( $lineCount -gt 10000){
        Rename-Item ABCPrintOutput0002.pdf "$($JobID)_Pr02_5001-10000.pdf"}
}
}
Remove-Item D:\WORK\JetLetter\JWH\JWH_Tr_Hot\JWH_Mailer.csv

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 2017-08-20
    • 2012-11-17
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    相关资源
    最近更新 更多