1
votes

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}
1

1 Answers

0
votes

You can Register a new ObjectEvent after renaming a file, so you would prepare for the next file that is coming. If a new file get created while its renaming a file. You will miss a file to rename. Chances of this happeningen is close to zero.

### 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"

                # ADD this line
                Register-ObjectEvent $watcher "Created" -Action $action
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action

OR

You could do it this way, it will run slower and proccess multiple files at the same time. It will look at the creation date to determine which file was created first. Note that if you move a file here the creationdate of the file is not updated and it will use the original creation date of the file.

code:

$counter = 1
while($True){

    # get all the files in C:\ test, filter out the ones that are already renamed and sort them on creation time
    # if a file is moved into this directory you will not have the creationtime will be the time that it was originally created and not the moved time
    Get-ChildItem -Path c:\test | ? {$_.FullName -notlike "*.host*.txt"} | Sort-Object -Property CreationTime | % {

        #determine the suffix
        if($counter % 3 -eq 0){
            $hostSuffix = 3
        }elseif($counter % 3 -eq  2){
            $hostSuffix = 2
        }elseif($counter % 3 -eq  1){
            $hostSuffix = 1
        }


        # renaming file
        $filename = "$($_.BaseName).host$($hostSuffix).txt"
        write-host "#$($counter): renaming: $($_.FullName) to: $filename"
        Rename-Item $_.FullName -NewName $filename

        #count
        $counter++
    }
    sleep(4)
}