?
Solved

Print .PDF files using command start-procces in ascending order of their names (values)

Posted on 2014-08-11
14
Medium Priority
?
226 Views
Last Modified: 2014-08-31
Hi folks,

There is script

$ex = New-Object -ComObject Excel.Application
$ex.Visible = 1
$wb = $ex.Workbooks.Open("C:\users\143\Documents\Hoveret-3-Eli.xlsx")

$ws = $wb.Worksheets.Item(1)

$result = @()

[int]$rmax = $ws.Range("G65536").End(-4162).Row
$ws.Range("G3:G$rmax") | Where {$_.Value2} | Foreach {
      $col = $_.Column+1
      $row = $_.Row
      $val = $ws.Cells.Item($row,$col)
      
            $result += [psobject]@{      
            Invoices = [System.Convert]::ToString($_.Value2)
            Values = $val.Value2
      }
}

$result | Where {!$_.Values} | Foreach {Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print }  
$ex.Quit()

how can I do so that the files are printed in ascending order of their names. (Eg 11.pdf, 13.pdf, 28.pdf)?
0
Comment
Question by:nesher13
  • 8
  • 6
14 Comments
 
LVL 41

Expert Comment

by:footech
ID: 40254082
You can try the below, though it's possible you may encounter names that aren't sorted in a natural language fashion.
$result | Where {!$_.Values} | Sort -property Invoices | Foreach {Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print } 

Open in new window

You could have an order of
1.pdf
11.pdf
2.pdf

The solution to this is zero-padding (could also use spaces, or maybe some other characters).
$result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach {Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print } 

Open in new window

0
 

Author Comment

by:nesher13
ID: 40265774
footech,

many thanks for you solution, but it does not completely solve my problem

I found out: if in the column of Excel the are numbers invoices, for which there is no corresponding pdf files in the folder, then in ,  I am receive error messages and sorting is not working.
I was very grateful to you if you can give a final decision
0
 
LVL 41

Expert Comment

by:footech
ID: 40266972
Well, this is an issue that would have existed in your original code and is independent of your original question.  I'm assuming that the error messages are from the Start-Process command, but I'm not seeing how this would impact the sorting as that's already been done.  You can avoid the error messages by testing the path first in order to filter what's passed along to the Start-Process command, or you could use a try-catch statement to catch the errors that come from submitting the invalid path.  Here's an example of the former.
$result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach `
{
    If (Test-Path "s:\INVOICES\0070\$($_.Invoices).pdf")
    {
        Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print
    }
}

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 

Author Comment

by:nesher13
ID: 40267041
footech

Here is the modified script:

Open in new window

$ex = New-Object -ComObject Excel.Application
$ex.Visible = 1
$wb = $ex.Workbooks.Open("C:\users\dov143\Documents\Eli-column.xlsx")
$ws = $wb.Worksheets.Item(1)


$result=@()

[int]$rmax = $ws.Range("G65536").End(-4162).row

$ws.Range("G1:G$rmax") | Where {$_.Value2} | Foreach {
      $col = $_.Column+1
      $row = $_.Row
      $val = $ws.Cells.Item($row,$col)

      
            $result += [psobject]@{      
            
            Invoices = [System.Convert]::ToString($_.Value2)
            Values = $val.Value2
      }          
}
                
      $result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach `
{
    If (Test-Path "s:\INVOICES\0070\$($_.Invoices).pdf")
   {
       Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print
  }
}

$ex.Quit()

Open in new window


but it does not give the correct result
0
 
LVL 41

Expert Comment

by:footech
ID: 40267067
What result does it give you?
0
 

Author Comment

by:nesher13
ID: 40267106
footech

While I write $result.invoices

I get on the console correct order numbers invoices.

While I run the pipeline, I think the command start-process broke the correct order output files.
0
 
LVL 41

Expert Comment

by:footech
ID: 40267165
I don't understand what you're trying to describe.
You should compare the output from
$result.invoices
$result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach {$_.Invoices}

If possible, post the output of both and point out exactly where you think the problem is.
0
 

Author Comment

by:nesher13
ID: 40267185
footech

$result.invoices

137125
140788
142625
142629
143114
143152
143153
143161
143328
143435

Pipeline  with sort output:

142629.pdf
142625.pdf
143328.pdf
143435.pdf
143161.pdf
143153.pdf
143152.pdf
143114.pdf
0
 
LVL 41

Expert Comment

by:footech
ID: 40267209
What is that second output from?  It's not from the exact command I put in my last post.
0
 

Author Comment

by:nesher13
ID: 40267241
You are right, the last command will output numbers invoices (from Excel file) in the correct order. My task: to print the files pdf (invoices), corresponding numers of the invoices from the folder, and display a message, if the folder does not contain the corresponding files pdf
0
 
LVL 41

Expert Comment

by:footech
ID: 40268584
So we know the results are being sorted.  If they are not printing out in the same order then perhaps it has something to do with how fast a file is processed for printing (some might take longer than others).  You might try adding a command like
Start-Sleep -seconds 3
after the Start-Process command (but still in the ForEach block).  You may have to play around with how much a delay to use.  Another thing you could try is using the -Wait parameter of Start-Process.
0
 

Author Comment

by:nesher13
ID: 40272178
footech,

If I understand you correctly then the code:

should look like this:


Open in new window

$result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach {$_.Invoices}

 {  If (Test-Path ("s:\INVOICES\0070\$($_.Invoices).pdf")
      
   { Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print | Start-Sleep -seconds 3 }}

Open in new window


or as follows:


Open in new window


Open in new window

$result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach {$_.Invoices}

 {  If (Test-Path ("s:\INVOICES\0070\$($_.Invoices).pdf")
      
   { Start-Process -wait -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print  }

Open in new window

0
 
LVL 41

Accepted Solution

by:
footech earned 1500 total points
ID: 40273849
The first one would be like
$result | Where {!$_.Values} | Sort -property {($_.Invoices).PadLeft(20,"0")} | Foreach `
{
    If (Test-Path "s:\INVOICES\0070\$($_.Invoices).pdf")
    {
        Start-Process -FilePath "s:\INVOICES\0070\$($_.Invoices).pdf" –Verb Print
        Start-Sleep -seconds 3
    }
}

Open in new window


The second one is correct.
0
 
LVL 41

Expert Comment

by:footech
ID: 40296080
Please update with any issues, or close the question.
Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question