Solved

Powershell (FileSystemWatcher FileCreated) with Task Schedule

Posted on 2014-02-10
7
1,010 Views
Last Modified: 2014-02-14
Hi all
I have the code below..
I need to rename all the files that are created in Folder A and them move them to folder B

Line 1 is the current file name
161606516.ABC111-P85-PCQ3362.527806.map
Line b is the expected file name
161606516.ABC173-P85-PCQ4548.527806.map

The sections I need to rename are ABC111 and PCQ3362: With a static value or variables.
Any help  on how I can accomplished this using Powershelland Task scheduler..

# Here, all three events are registerd.  You need only subscribe to events that you need:

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Out-File -FilePath C:\temp\scripts\filechange\outlog.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
Get-ChildItem -Filter "*PCA3362*" -Recurse | Rename-Item -NewName {$_.name -replace 'PCA3362','PCA4548'}

}

Open in new window



Thanks in Advance
0
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39849759
So every file that you need to rename has both "ABC111" and "PCQ3362"?  And everything else is variable?
Stated another way - "x" is variable per file, but should be maintained in the rename
xxxxxxxxx.ABC111-xxx-PCQ3362.xxxxxx.map

Unless more sophisticated matching is required the below should work for you.  Test and let me know if there are any issues.  You may want to use the -whatif parameters for rename-item and move-item.
$source = "c:\folderA"
$dest = "c:\folderB"
Get-ChildItem $source -Filter "*PCA3362*" -Recurse | ForEach `
{
    $_ | Rename-Item -NewName ($_.name -replace 'PCA3362','PCA4548' -replace 'ABC111','ABC173') -PassThru |
     Move-Item -Destination $dest
}

Open in new window

0
 

Author Comment

by:ZURINET
ID: 39850053
Hi footech

I have reach this stage in my testing..
My last problem is how can I schedule this code to work with task scheduler..
I need it to run every 20 seconds, or how can I configure this script to watch the folder..?

Thanks in Advance.
0
 
LVL 40

Expert Comment

by:footech
ID: 39850979
Create a new scheduled task.
Your action should be set to run
powershell.exe
with the argument
-file c:\path\script.ps1
If needed, the task should have the options checked for "Run whether user is logged on or not" and "Run with highest privileges".  Can't remember exactly under what circumstances the following is necessary, so try without it first - If the account that you're using to run the task is not an admin on the machine, either make it so or add it to the "Log on as batch job" setting in the local security policy (run gpedit.msc to get to it).

For the trigger you'll probably want something like
Begin the task:  At startup
Repeat the task every: 5 minutes
for a duration of: indefinitely
5 minutes is the shortest repeat interval you can choose.  If you really need it to run every 20 seconds, then I suppose we could modify the script to loop and just keep it continuously running.
0
Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

 

Author Comment

by:ZURINET
ID: 39852751
Hi .. I tried it and it works..
but I need the script to exit after executing..
but it not doing that .. I have tried

using exit at the last of the line.. but it not doing.. it ..
How can I achive this objective?

Thanks in Advance
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39852833
Why do you think the script is not exiting? If you use the powershell -file ... syntax, PS is terminated after the script has finished (no exit needed anywhere).
0
 

Author Comment

by:ZURINET
ID: 39852972
The issue is, when I run the batch.. The shell console comes up..
and stays open..  .. I need it to exit..
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39853028
If you run footech's code, no console window should persist. If you run your initial code, it has to stay, as the registration only works in the current session - and that again needs a console to run in.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question