Solved

Explain how this works

Posted on 2014-01-07
4
294 Views
Last Modified: 2014-01-08
Someone gave me this recursive code, but it would fail with this error message:
"Program 'JPEGtoPDF.exe' failed to run: Not enough quota is available to process this command"
I Googled and it seemed to say I was out of resources, but I am not even close, so I decided to watch the program work. I figured out "gci" stood for Get-ChildItem and I started off inserting a SLEEP(1) after the jpegtopdf.exe line. This was painfully slow and only one filename was processed between sleep(1).
I then used the FOR loops to slow it down, but to my surprise it allowed jpegtopdf.exe to process the entire directory it was in and then pausing a moment before doing the next directory\subfolder.
I figure this has something to do with SLEEP(1) stopping the whole program, while the FOR loops allow the executable to run to a certain point and then spit out the results?
What do you think?
Also, is there a web page that can explain how this program works so that I can learn how to do this on my own? I see the %{ after the pipe a lot and then the $(${_.objectname, but I don't know how it works or where to start looking. I Googled, but did not find anything for beginners.
thanks!


$search = "C:\photos"
Get-ChildItem $search -filter *.jpg -Recurse | %{
    write-host "Begin work on :" $_.FullName
    jpegtopdf.exe "$($_.DirectoryName)\$($_.BaseName).PDF" $_.FullName
    #slow it down with FOR loops works.
    #SLEEP(1) pauses for each filename
    for($i=0;$i -lt 1000;$i++)
    {
    for($o=0;$o -lt 100;$o++){}
    }
 }
0
Comment
Question by:johnj_01201
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39763095
Conversion programs tend to eat up the memory. Have you tried increasing the paging file?
0
 
LVL 1

Author Comment

by:johnj_01201
ID: 39763114
Yes, the issue turned out to be the Remote Powershell Window. The program runs fine, 1 file at a time and without the FOR loops, when run on the local server.
I found 1 comment in Google that told there's a limit on Windows Messages and if you fill it up it trips a wire causing this error so that it does not overflow.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39763429
| % is shortcut for foreach. $_ is self

Your script translates to:
$search = "C:\photos"
$results = Get-ChildItem $search -filter *.jpg -Recurse
foreach ($result in $results) {
     write-host "Begin work on :" + $result.FullName
    jpegtopdf.exe $result.DirectoryName + "\" + $result.BaseName + ".PDF" $result.FullName
}

Open in new window


HTH,
Dan
0
 
LVL 1

Author Closing Comment

by:johnj_01201
ID: 39765035
THANK YOU!!!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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