ZURINET
asked on
Powershell (FileSystemWatcher FileCreated) with Task Schedule
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-PCQ33 62.527806. map
Line b is the expected file name
161606516.ABC173-P85-PCQ45 48.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..
Thanks in Advance
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-PCQ33
Line b is the expected file name
161606516.ABC173-P85-PCQ45
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'}
}
Thanks in Advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
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
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
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).
ASKER
The issue is, when I run the batch.. The shell console comes up..
and stays open.. .. I need it to exit..
and stays open.. .. I need it to exit..
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.
ASKER
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.