Solved

Powershell (FileSystemWatcher FileCreated) with Task Schedule

Posted on 2014-02-10
7
891 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
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
Comment Utility
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 39

Expert Comment

by:footech
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:ZURINET
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
The issue is, when I run the batch.. The shell console comes up..
and stays open..  .. I need it to exit..
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now