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
Solved

Powershell (FileSystemWatcher FileCreated) with Task Schedule

Posted on 2014-02-10
7
945 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
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.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

860 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