Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am new to powershell

Currently I am trying to write a script on powershell.

1. Monitor the Changes in a particular folder (C:\Users\CELINE\Desktop\Testing4)

2. If there is a new file detected, it will automatically move the file to another folder (C:\Users\CELINE\Desktop\PowerShellChanges2)

3. Automatically rename the moved file to "Request.csv"

4. Append incremental numbers to the file names when new files is being moved to the folder (eg. Request(1).csv, Request(2).csv, Request(3).csv etc)

5. Open a notepad after the file is moved

I am now stuck with point 4 and 5.
I have no idea why when the file is being moved, multiple notepads are opened continuously but I only want to open one
I also do not know how to append the file names

Anyone able to help?

What I have tried:

$watcher = New-Object System.IO.FileSystemWatcher

#Path of folder to watch

$watcher.Path = "C:\Users\CELINE\Desktop\Testing4"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

#Action executed when changes detected

$action = {


#SourcePath
$path = $Event.SourceEventArgs.FullPath

$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"

#Destination path
$dest = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request.csv"

#Add Log file to monitor the changes

Add-content "C:\Users\CELINE\Desktop\logtest5.txt" -value $logline

#Move Files

$actionMove = Move-Item -Path $path -Destination $dest


#Execute Notepad


$FileExists = Test-Path $dest
If ($FileExists -eq $True) {C:\Windows\notepad.exe}
}

#Event Trigger
Register-ObjectEvent $watcher "Changed" -Action $action
while ($true) {Sleep 0}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900