Needing help with a IF in a WHILE Loop

I am very new to PowerShell -- not the same as bash for sure. This might be very simple to resolve for someone who is familiar with PowerShell. The purpose of this script is to move incoming files from a shared folder "MYincoming" to a non-shared folder "MYfiles." It's in a loop, but I am running into a problem with the loop. When I start the script it will pick up the right condition, meaning "Source contains files" if there are files in the incoming folder. If none, it will pick up the next condition "Source does not contain files." However, once it picks up one of the two conditions it won't change or check the next condition.

I place files in the incoming folder and it picks them up and move them to the "MYfiles," then sleep and continue to output "Source contains Files" every 30 seconds. I expected it change to "Source does not contain files" once the files have moved, but it a bit loopy at that point. I am not sure how to resolve this. I would appreciate any guidance, even if it shows how little I know about PowerShell. You would be right, it's the first time I try PowerShell.

See script here:

$ErrorActionPreference="continue"
$Source = "f:\MYincoming"
$Destination = "f:\MYfiles"
$Date = Get-Date -format M
$LogFile = "f:\MYfiles\Logs\$Date-xfer_log.txt"

# Get existing files into arrays:
$SourceFiles = Get-ChildItem -Path $Source -Recurse

# Loop all files in the source folder:

while(1) {
if($SourceFiles.Count) {

    "Source contains files"
    Get-Date | Out-File $LogFile -Append
   
         $SourceFiles | ForEach-Object {
              $FilePath = $_.fullname
          if (Test-Path -Path $_.fullname)
          {
              Move-Item $FilePath -Destination $destination -force
              $FilePath | Out-File $LogFile -Append
          }
    }
}
else {
    "Source does not contain files"  
}
start-sleep -s 30
}

---------------------------
LVL 2
Robert LemMrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You need to move the Get-ChildItem into the loop:
while($true) {
    # Get existing files into arrays:
    $SourceFiles = Get-ChildItem -Path $Source -Recurse

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Robert LemMrAuthor Commented:
Thank you very much Qlemo. This was it. It resolved my issue.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.