Polling for file change inside a folder and then taking the new file and moving

Welcome Forums General PowerShell Q&A Polling for file change inside a folder and then taking the new file and moving

Viewing 11 reply threads
  • Author
    Posts
    • #261941
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      I need a good sample code that will allow me to monitor a folder for ANY changes to a given file and then once that change takes place, move that file to an alternate location.

      The challenging part here for me is the polling code.

      Anyone please assist me.

      Thank you

    • #261950
      Participant
      Topics: 4
      Replies: 95
      Points: 428
      Helping Hand
      Rank: Contributor

      I built a solution for almost this exact scenario at my work. I use it to push important code out to teammates computers so it’s easy to stay on the same version. This is the article I used as an example. Hope this helps.

      https://mcpmag.com/articles/2015/09/24/changes-to-a-folder-using-powershell.aspx?m=1

    • #261956
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      Logan, that Filter limitation is going to kill me.  I need a solution that will track a file with regard to its file extension specifically.

       

      Any ideas?

    • #261962
      Participant
      Topics: 4
      Replies: 95
      Points: 428
      Helping Hand
      Rank: Contributor

      Im not certain what you mean by filter. Can you show me with an example of where you are stuck? Personally, I use a RegEx Switch statement to handle the $Event.SourceEventArgs.ChangeType, that allows me to customize what happens for the different change types I am interested in, renamed, change, create, and deleted. From my switch I do additional verification and manipulations because the objects have different properties depending on the event. You can do filtering on file extensions based on the $Event.SourceEventArgs.Name I would imagine. Does that help?

    • #261965
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      Logan:

      Any sample code you can share for simply watching a folder for file extension changes, and when they occur, moving them to another location on the network?

       

      Would be much appreciated my friend.

       

      Thank you in advance.

    • #262013
      Participant
      Topics: 4
      Replies: 95
      Points: 428
      Helping Hand
      Rank: Contributor

      I do not have an example specifically for when a file extension changes. That should occur under a “Changed” event type. The link I shared has example code and a nice function at the end. The magic is in the scriptblock you define as the argument for the -Action parameter (if using the filesystemwatcher function from that MCP article). I am happy to help debug some of your code if you share an example. If you want me to write the code my consulting rates start at $1500 and that buys you up to 20 hours.

    • #262109
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      By Filter I mean in the article where it states

      The Filter property is basically just specifying what kind of items you are looking for. It only allows for a single string to be used and you cannot use something like “*.txt|*.xls” as it is not supported.

       

      Forget about file extensions then Logan.  Do you just have some sample code that will monitor a file folder for changes such as a new file appearing and then moving it to another place in the network such as a shared network drive?

      Will the code in that article accomplish such a task?

      Please respond ASAP if possible.

       

      Thank you Sir for all your help thus far!!!

    • #262118
      Participant
      Topics: 4
      Replies: 95
      Points: 428
      Helping Hand
      Rank: Contributor

      At this point I am quite convinced you merely skimmed the article rather than taking time to read and understand it. That is unfortunate because you would have seen the code that can be used as an example had you taken the time. Boe Prox is highly respected in this community and if you are serious about learning PowerShell you need to put in the time and effort. I did you a favor and copied the code from Boe’s and pasted it below. This is a working example of the FileSystemWatcher in action. I changed the folder path on line 74, and updated the event types in line 100 to “Changed”,”Renamed”. The meat and potatoes of the code remains unchanged from the article. For further assistance please demonstrate you are serious about learning this stuff. It takes an incredible amount of time and energy to develop these solutions to be handing them out for free. Especially to those who seem unwilling to help themselves.

       

    • #262133
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      Logan, I am truly convinced… You are NOT A HUMAN BEING!!!  Your an ANGEL IN DISGUISE!!!!

      Something is definitely wrong with your profile picture because I cannot see your halo and your wings in the picture.  Whats up with that?

      All kidding aside thank you so much for this code.

      One question though.   Lets pretend that the folder being watched is the C:\Temp folder and any changes to it, (addition of new files as an example), show up and are re routed to the Z:\Active network share.

       

      Could you modify this code with those particular specs please?

       

      Thank you again so much Mr. Angel in Disguise!!!!  hehe

    • #262142
      Participant
      Topics: 4
      Replies: 95
      Points: 428
      Helping Hand
      Rank: Contributor

      Thank you for the compliments. It has taken me 7 years to get to this point. Thousands of hours studying with literally over a million lines of code written. If I do the modifications for you then I would be writing the code, and I don’t create solutions for free. The best I can do is direct you to line 96 in the example. That’s an excellent place for you to start experimenting to achieve your objectives. Best of luck to you.

    • #262148
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

       

      Thanks again for all your help.  I truly appreciate everything you shared with me.

       

      Have a blessed Friday and Weekend Sir

      • This reply was modified 2 weeks, 3 days ago by nal2us224.
      • This reply was modified 2 weeks, 3 days ago by nal2us224.
    • #262154
      Participant
      Topics: 13
      Replies: 40
      Points: 177
      Rank: Participant

      Got it.  Thanks again

      • This reply was modified 2 weeks, 3 days ago by nal2us224.
Viewing 11 reply threads
  • You must be logged in to reply to this topic.