1

I have a Powershell script doing the following:

 # Lists txt files, remove 'newline' in files, move them to another folder

 $files = @(Get-ChildItem c:\temp\*.txt)
 $outputfolder = "c:\temp\fixed"

        foreach ($file in $files) 
                {
                (Get-Content $file -Raw) -replace "`n",'' | Set-Content $file
                Move-Item $file $outputfolder
                }

Now I would like to add some while cycle (or other routine) with the aim to keep the script awake and listening for new files to be processed. When new incoming files are detected in c:\temp folder the script should process them automatically. Maybe it could be achieved using some "sleep" command, to check the directory every 5 seconds.

Some good suggestion?

2
  • I like the "-2" votes but no explanation as to why? I think this is a good question, apart from the missing "I have tried X, Y and Z but they haven't worked." Maybe he just doesn't know where to start, and neither would I! Keeping this one watched so I can learn if an answer gets offered. Commented Nov 18, 2017 at 22:28
  • Thanks @RossLyons, I agree with you. It's not the first time that anybody votes negatively with no explanation at all. By the way, I don't care about votes. I found my own solution and I will post it for the benefit of everybody, which is the spirit of any forum. Thanks for your words. Commented Nov 20, 2017 at 0:23

3 Answers 3

1

The basic way to do this is:

while ($true) {

    # Your script here

    Start-Sleep -Seconds 5;
}

The next method is only slightly more complex. You could save your script to a .ps1 file and then use Windows Task Scheduler to run the script every 5 seconds.

Both of those methods are so basic that it's reasonable to expect that you should already know them, and not explaining why you're not using them is probably why you've been downvoted and gotten close votes.

A third option is to use System.IO.FileWatcher along with Register-ObjectEvent. This option is fairly advanced. There are examples here on StackOverflow and elsewhere. This has the advantage of being even less resource intensive, but has the disadvantage that because you're using methods intended for authoring services it's kind of like using a nailgun to drive a single nail.

Sign up to request clarification or add additional context in comments.

2 Comments

The negative votes just because we 'should already know something' is a very poor excuse. People like myself and @Marco Falzone come here for these kind of suggestions because it may just not be that obvious to us, thus the request for advice. Having it pointed out in a negative light like "you should already know this..." is a deterrent to learning in my point of view. Aside from that! Thanks for the answer, gives myself at least a good starting point to start playing around and a new topic to learn.
@RossLyons I agree that downvotes without comments are not helpful and that's why I answered as I did.
0

Since youre moving the file - the easiest way would be to wrap into while 1 loop

Comments

0

I really cannot understand negative votes to my post. This is my solution after some investigation and trials. It may be useful for somebody else.

#  The following script listens for new files in a folder and processes them
#
# BEGIN SCRIPT

$folder = 'c:\temp'             # My path
$filter = '*.*'                 # File types to be monitored

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{    # Listening function
 IncludeSubdirectories = $false              # Put "True" to scan subfolders
 EnableRaisingEvents = $true
 NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
 $path = $Event.SourceEventArgs.FullPath            
 $name = $Event.SourceEventArgs.Name                
 $changeType = $Event.SourceEventArgs.ChangeType    
 $timeStamp = $Event.TimeGenerated                  
 $destination = 'c:\temp\fixed\'                    
 $outfile = $destination + $name
 Write-Host "The file '$name' was $changeType and processed at $timeStamp"   -ForegroundColor Yellow # Log message on the screen
 (Get-Content $path -Raw) -replace "`n",'' | Set-Content -path $outfile  
 Remove-Item $path   # Delete original files
}

#  END SCRIPT

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.