Solved

Powershell (FileSystemWatcher FileCreated) with Task Schedule

Posted on 2014-02-10
7
931 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
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 39

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

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

Suggested Solutions

Title # Comments Views Activity
Question! 4 36
NTFS Permissions 6 47
Run Powershell Function as Scheduled Task with Parameters 1 23
Winforms -- Capture CTRL+SHIFT+D at the form not working? 3 22
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 show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
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…

777 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