Solved

Powershell (FileSystemWatcher FileCreated) with Task Schedule

Posted on 2014-02-10
7
966 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 69

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 69

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

756 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