Hey everyone! I'm new to scripting and struggling a bit. I'm trying to automate batch printing and move files to different folders based on whether the print job is successful. I can manage to print the files, but once I add commands to move them or implement error checks, my print jobs start failing and the files end up in the error folder. I want to know what I'm missing or doing wrong here! Just so you know, I'm using VS Code for scripting. The errors I encounter are mostly about the file either not existing because it's moved before it prints or that it can't be moved since it's in use. These issues seem to pop up particularly with PDFs, which are the main files I'm dealing with. I really need to streamline this process since I have about 600 documents to print, and printing in bulk only allows me to do about 15 at a time through file explorer. Here's my script for reference:
$folderPath = "T:statementsto be printed"
$filestoprint = Get-childitem -path $folderPath -File
$archivePath = "$folderPathCompleted"
$logPath = "$archivePathprint_log.txt"
$reprint = "$folderPathErrors"
Start-Transcript -Path $logPath
foreach ($file in $filestoprint) {
try{
$process = Start-Process -FilePath $file.FullName -Verb Print -WindowStyle Hidden -PassThru
Wait-Process -Id $process.Id -Timeout 5
start-sleep -seconds 5
}
catch{
Move-item -path $file.FullName -Destination $reprint -ErrorAction Stop
}
Move-item -Path $file.fullname -Destination $archivePath
}
Stop-Transcript
1 Answer
It looks like you're having a few issues with how the logic in your script flows. When you call `Wait-Process`, it waits for the process to return a code—it's not necessarily waiting for the print job to complete as you might expect. Also, your error handling seems to try and move files even after successfully printing them. I recommend separating the success and failure logic a bit more. Here’s a revised version you could try:
```
$folderPath = "T:statementsto be printed"
$archivePath = "$folderPathCompleted"
$logPath = "$archivePathprint_log.txt"
$reprint = "$folderPathErrors"
Start-Transcript -Path $logPath
$filesToPrint = Get-ChildItem -Path $folderPath -File
foreach ($file in $filesToPrint) {
$printSucceeded = $false
try {
$process = Start-Process -FilePath $file.FullName -Verb Print -WindowStyle Hidden -PassThru
if ($process) {
Wait-Process -Id $process.Id -Timeout 10
Start-Sleep -Seconds 5
$printSucceeded = $true
} else {
Write-Warning "Failed to start print process for: $($file.Name)"
}
}
catch {
Write-Warning "Exception printing file $($file.Name): $_"
}
if ($printSucceeded) {
Move-Item -Path $file.FullName -Destination $archivePath -Force
} else {
Move-Item -Path $file.FullName -Destination $reprint -Force
}
}
Stop-Transcript
```
Give this a shot!
I tried your revision, and it worked the first time! But then, it started to choke on PDFs after a few runs—some printed but wouldn't move, while others got shoved into the error folder because they couldn't print. I'm starting to think Adobe is giving me a hard time. I also tried SumatraPDF, but it prints everything so slowly that it defeats the purpose of the automation.