Printing New Files In Directory

This topic contains 22 replies, has 2 voices, and was last updated by Profile photo of Sam Sam 1 year, 8 months ago.

  • Author
    Posts
  • #31803
    Profile photo of Sam
    Sam
    Participant

    I am brand new to Powershell. I have only used some VB with excel apps and that is about the most I know about programming. I'm also new to this forum!

    What I would like to do is monitor many directories under 1 path and print new files that are created anytime between 1:00AM and 6:00AM to my default printer. Is this possible with powershell? I really need help with this and would really
    appreciate the help. Thank you in advance!

  • #31808
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    It is absolutely possible. Assuming that the files you are monitoring have a print option in their context menu it should be very easy. Look at the Get-ChildItem (alias dir) and Start-Process. With Get-ChildItem, look a the -filter parameter, and with start-process look at the -verb parameter.

  • #31810
    Profile photo of Sam
    Sam
    Participant

    Thank you for the reply! The files are all .HTM.

    Here is some code I was playing around with, trying to understand whats going on. Found this on the web http://superuser.com/questions/226828/how-to-monitor-a-folder-and-trigger-a-command-line-action-when-a-file-is-created

    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
        $watcher = New-Object System.IO.FileSystemWatcher
        $watcher.Path = "C:\Users\User\Desktop\EOSRPrint"
        $watcher.Filter = "*.*"
        $watcher.IncludeSubdirectories = $false
        $watcher.EnableRaisingEvents = $false  
    
    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
        $action = { $path = $Event.SourceEventArgs.FullPath
                    $changeType = $Event.SourceEventArgs.ChangeType
                    $logline = "$(Get-Date), $changeType, $path"
                    Add-content "C:\Users\User\Desktop\log.txt" -value $logline
                  }    
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
        $created = Register-ObjectEvent $watcher "Created" -Action $action
        $changed = Register-ObjectEvent $watcher "Changed" -Action $action
        $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
        $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
        while ($true) {sleep 5}

    I'm really not sure what I'm doing. This is so new to me but looks promising!

  • #31812
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Ok so you want this as an always running processes, or will you be starting it and then it prints the files from that timeframe?

  • #31814
    Profile photo of Sam
    Sam
    Participant

    Always process – as long as I have the powershell window open if that is possible. I guess if I were to need to run it 1 time would also be useful but not main objective.

    As you help me I'm playing around with commands. Took me a long time to figure out (google!) that I have to use ' around command to change directories... commands are like dos commands with some differences. I feel like I have a new toy!

  • #31819
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    will the name of the file ever repeat?

  • #31820
    Profile photo of Sam
    Sam
    Participant

    Yes, the file name is saved with the date in the name and have the word NIGHT.HTM.

    So *-YYYYMMDD-NIGHT.HTM will be the format every time.

    Playing with that monitoring code, looks like the newest file during that time period with the word NIGHT.HTM in it would work. Date being in the name might not have anything to do with finding the right 1. Just the latest in the folder during that time period will work.

  • #31821
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    So you may get multiple files a night so maybe at 1 o'clock you get file-20151109-NIGHT.HTM, and then at 2 o'clock you get file-20151109-NIGHT.HTM, etc. etc., and each time the new file overwites the previous file?

    or does the * in front of your file name indicate that there is something else to always make the file name unique, but you only get one per night so the name never repeats.

    Like:
    inventory-20151109-NIGHT.HTM
    waste-20151109-NIGHT.HTM
    profits-20151109-NIGHT.HTM
    inventory-20151110-NIGHT.HTM
    waste-20151110-NIGHT.HTM
    profits-20151110-NIGHT.HTM
    inventory-20151111-NIGHT.HTM
    waste-20151111-NIGHT.HTM
    profits-20151111-NIGHT.HTM
    etc.etc.etc

  • #31822
    Profile photo of Sam
    Sam
    Participant

    During the time of 1am to 6am there will only be 1 new file created during this time frame.

    In each folder under the top.

  • #31823
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Ok so something like this were the file name does repeat, but it's under a different folder?

    c:\temp\
           \folder1\inventory-20151109-NIGHT.HTM
           \folder2\inventory-20151109-NIGHT.HTM
           \folder3\inventory-20151109-NIGHT.HTM
    
  • #31825
    Profile photo of Sam
    Sam
    Participant

    During the time of 1am to 6am there will only be 1 new file created during this time frame.

    In each folder under the top.

    Will the file name matter if we look for newest file? I have no idea, just trying to make it easy.

  • #31828
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    yes is maters because of how the events get generated with the file system watcher. Unfortunately when a file is created, it does not always generate just one creation event. Different applications may generate multiple creation events for the creation of one file. As such you have to add logic so the script knows when it has already printed a file. Using the file name is the most logical for this purpose if the file name is unique. If it is not, then possibly using a combination of parent folder and file name to uniquely identify the file will work. If you don't add this logic, a single file could get printed multiple times.

    Of course this is only an issue because of the request to always be running and printing based off of the event.

    It would be much easier to write a script that kicks off with task scheduler at say 6:05 AM and looks for all file in the directory structure between 1:00 and 6:00 the current day and print those.

  • #31829
    Profile photo of Sam
    Sam
    Participant

    Ok, I can do that for sure. I'll set up a windows task run it.

  • #31830
    Profile photo of Sam
    Sam
    Participant
    Start-Process -FilePath 'p:\filepath\folder1\CON-20151108-NIGHT.HTM' –Verb Print -PassThru | %{sleep 10;$_} | kill 

    This line works! Problem is the program that is saving the files is saving them with the date in the name. How do I fix the command to print only the file with the current date?

    Current date formatted like *YYYYMMDD-NIGHT.HTM

  • #31833
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    If that works for you, then this should work for you.

    $Path = "F:\Temp\Powershell\part\*.htm"
    $StartDate = Get-Date -Hour 1 -Minute 0 -Second 0 -Millisecond 0
    $EndDate = Get-Date -Hour 6 -Minute 0 -Second 0 -Millisecond 0
    Get-ChildItem -Path $Path -Recurse | 
    Where-Object {$_.CreationTime -gt $StartDate -AND $_.CreationTime -lt $EndDate} |
    ForEach-Object {
        Start-Process -FilePath ($_.FullName) –Verb Print -PassThru | %{sleep 10;$_} | kill
    }
    
  • #31834
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    I was having a challenge with Start-Process with the -verb parameter because IE would just show the print dialog box, but not actually submit the print job. Was looking for an alternative, and not having good luck.

  • #31835
    Profile photo of Sam
    Sam
    Participant

    I was getting an error.

    "Get-Date : A parameter cannot be found that matches parameter name 'millisecond'."

    I removed the -millisecond 0 and it worked... so I'm not sure why it is giving an error.

    What it looks like, I will need to make 1 of these for each folder and set up the windows scheduler to run them. That is what you were thinking correct?

  • #31837
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    "Get-Date : A parameter cannot be found that matches parameter name 'millisecond'."

    That means you are running PowerShell 2.0 or older. You should probably upgrade. PowerShell 5.0 is in product preview. Soon to be fully released.

    What it looks like, I will need to make 1 of these for each folder and set up the windows scheduler to run them. That is what you were thinking correct?

    Nope, the get-childitem cmdlet is using the -recurse parameter, so you should be able to specify your parent folder and it will recursively look through all of your child folders.

  • #31847
    Profile photo of Sam
    Sam
    Participant

    Ok, I will upgrade my version. Keep in mind I just happen to run across powershell about a week ago if that, so I am a noob! I can see the power and I will continue to play with it for sure.

    I do have 1 more question – is there any way to exclude folders under the parent folder?
    There are more folders that need to be printed vs a few that don't. Would be nice to be able to exclude a few folders and not waste paper.

  • #31852
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    yes, check out the -exclude parameter of the get-childitem cmdlet or piping into where-object

  • #31868
    Profile photo of Sam
    Sam
    Participant

    Learned a few things – Powershell 2.0 wasn't working at all really. I could only do 1 folder at a time. I upgraded to 4.0 and now it works with all the folders under the parent.

    I've edited this one line and I believe its working. There are so many files, my guess is this script is checking each file in every folder. So in my mind if there were less files to analyze things might work a little better. Is this the correct way to do this?

    Get-ChildItem -Path $Path -Recurse -exclude *-DAY.HTM, *.PDF , *MAINTENANCE*.HTM 

    Only thing left to do is get rid of this annoying "would you like to print" dialogue

  • #31875
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Yes, your usage of -exclude looks correct, although you should not need to exclude *.PDF since your -Path should specify *.htm, so there are no PDFs to exclude.

    As far as the dialog, ya it's a problem. I spend some time searching and testing and did not come up with a solid solution for rendering and then print the HTM without being prompted by the dialog. The "RUNDLL32.EXE MSHTML.DLL,PrintHTML $file $printer" method would not work for me, nor would the automation using IE com object.

    I actually just ran across this free utility, but I have not tried it. Maybe it will actually do what you need.
    (I have no affiliations with this site or utility so use at your own risk)
    http://www.printhtml.com/

  • #31876
    Profile photo of Sam
    Sam
    Participant

    Yeah, I have looked everywhere myself for a solution. Don't laugh... but I have 1 that might work.... really dirty. I'm going to try and use autohotkey and send a spacebar press over and over during the time the powershell script is running. This should press the print button in the print dialogue box. Fortunately, the Print button is in focus when the box appears. I'll let you know how it works out!

You must be logged in to reply to this topic.