
When working with association classes, there is always a property that relates one to the other. The Cim_Director圜ontainsFile WMI class associates files and directories. To find a file in a directory by using WMI means that we need to use an association WMI class. The problem with monitoring for a newly created file is that a file must reside somewhere-for example, inside a directory. This class is a generic event class, and it will monitor for new instances of “stuff.” It can be anything from a new entry in an event log to a new file.

It begins by selecting everything from the _InstanceCreationEvent WMI class. The WQL query itself is not too horribly bad.
#Monitor folder for new files code#
I knew that a lot of that old code was easily adaptable to Windows PowerShell and would be useful for years to come. Note This is ONE of the major reasons I insisted on migrating all of the old Hey Scripting Guy! Blogs to the new Hey, Scripting Guy! Blog format four years ago when I became the Scripting Guy. But also you can see how having a nice reference query, even from an eight-year old VBScript script, is also beneficial. You can see where the use of a here-string vastly simplifies things by allowing me to forget about line continuation and having to escape quotation marks and other things. & “Targetinstance ISA ‘CIM_Director圜ontainsFile’ and ” _ (“SELECT * FROM _InstanceCreationEvent WITHIN 10 WHERE ” _ Here is the VBScript query from that blog. Guess what? The query was just the thing I needed to refresh my memory for creating my new query. I referred to an old Hey Scripting Guy! Blog, How Can I Automatically Run a Script Any Time a File is Added to a Folder, which was written nearly eight years ago in VBScript. You cannot place it at the end of your last line of text, nor can you indent to make things “line up.” It must be in the first position of its own line. Do not press the spacebar and then ENTER you need the return right after the The closing tag ( must be in position 1 of its own line. Immediately after the opening tag hit ENTER.The here-string closes with a closing quotation mark ampersand: are two rules that you must follow: The here-string begins with an ampersand and an opening quotation mark: Everything inside the here-string is interpreted literally so you do not need to worry with escaping special characters or quotation marks or any of that stuff. The basic syntax is to use a variable to hold the resulting here-string. Here-strings are really finicky (they make Morris the Cat seem like an omnivore). Luckily, Windows PowerShell can bring some sanity to this part of the process. When you start trying to do this, however, you run into weird quoting rules that only make a confusing situation more confusing. Luckily, if you have WQL event query from VBScript or some other language, it is not too difficult to migrate the query to Windows PowerShell. The hardest part of creating a WMI WQL event query is, well…just about everything. Creating a temporary event consumer with Windows PowerShell 2.0 is really easy, so it only makes sense to take this first step. In fact, whenever I am creating a permanent WMI event consumer, I always test it out as a temporary event consumer first. Then I will use this WQL event query tomorrow to create a permanent WMI event consumer.

Today I am going to develop a WMI event query to detect newly created files in a particular folder. Note For more information about WMI event driven scripts, see An Insider’s Guide to Using WMI Events and PowerShell. Then if the files match the naming pattern discovered yesterday, rename them by using the procedure from the script I posted yesterday in Use PowerShell to Detect and Fix Files with Leading Spaces. Although running a script on demand to find and rename files in a folder might work, it would be better to use an event to monitor the folder for newly created files. Yesterday’s email from KS about his problems with files that contain leading spaces in them got me thinking. Microsoft Scripting Guy, Ed Wilson, is here.
#Monitor folder for new files how to#
Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to monitor for the creation of new files.
