How to setup a print to file that doesn't overwrite the files.

I recently had a request to setup a print queue for testing. Not wanting to waste paper, I wanted to have it print to file.  So I looked online, and I found you can setup a generic/text only printer that will print to a file by setting the print point to a directory path and file name.  The one problem with that is each new print job overwrites the last one. 

I found some apps that you can buy to do this, but nothing in the scripting realm, and definitely nothing for free.

So I created a powershell script that monitors the folder, and when a new file arrives, it renames it to the date/time down to the second.  Being that granular with the time insures that the file names are never duplicated.  The process runs in an endless loop, and won't stop until the server shuts down.

Set this up as a scheduled task to run on system start-up.

========================================================

$folder = "Print Load Test"
$end = "False"
DO
{
 Get-EventSubscriber | Unregister-Event
 Get-Event | Remove-Event
$query = @"
 Select * from __InstanceCreationEvent within 1  
 where targetInstance isa 'Cim_DirectoryContainsFile'  
 and targetInstance.GroupComponent = 'Win32_Directory.Name="d:\\\\$Folder"'
"@
Register-WmiEvent -Query $query -SourceIdentifier "MonitorFiles"
$fileEvent = Wait-Event -SourceIdentifier "MonitorFiles"
#$fileEvent.SourceEventArgs.NewEvent.TargetInstance.PartComponent
$date=(get-date -Format d) -replace("/")
$time=(get-date -Format T) -replace(":")
$datetime = ("$date $time") -replace(" ", "_")
Rename-Item "d:\$folder\new.txt" "Print_$datetime.dat"
} Until ($end -eq "True")
 
=============================================================

I hope this helps someone.

Clay Perrine


September 10th, 2015 4:40pm

Thanks for the information
Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 11:54pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics