Please find below script, what it does is find *.txt files and rename with either filename.host1.txt or filename.host2.txt.
What my requirement is it should create in sequence mode and not in random (Currently it is creating Host1 or host2 as random basis and not in sequence mode), If first file it renames as file1.host1.txt, then second file should be file2.host2.txt,and third file will be file3.host3.txt, fourth will be again starts with file4.host1.txt,file5.host2.txt,file4.host3.txt,. Basically from i want to add "host1" to "host3" whenever new file detects in folder and create files in sequential mode from 1 to 3 only. Host1,Host2,Host3 i used three virtual machine to execute sperete script. Here is my script..
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\test"
$watcher.Filter = "*.txt"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$filename = [io.path]::GetFileNameWithoutExtension($path)
###$changeType = $Event.SourceEventArgs.ChangeType
###$logline = "$(Get-Date), $changeType, $path, $filename"
###Add-content "D:\log.txt" -value $logline
###$proc=start-process "D:\source\$filename.bat" -Wait -NoNewWindow
$script:counter++
$hostname=""
if(($script:counter % 2) -eq 0){
$hostname="host1"
} Else {
$hostname="host2"
}
Rename-Item -Path "C:\test\$filename.txt" -NewName "$filename.$hostname.txt"
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
### Register-ObjectEvent $watcher "Changed" -Action $action
### Register-ObjectEvent $watcher "Deleted" -Action $action
### Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}