Home Check if file exist then continue with the code in Powershell
Reply: 1

Check if file exist then continue with the code in Powershell

Gwill Zx Spectrum
1#
Gwill Zx Spectrum Published in 2018-01-12 12:45:05Z

I need to check if some pdf exists in c:\pdf and if exists continue with the execution, if not, check again every 15th second.

My actual code just checks for files in the folder and print if there is something, if not just repeat again and again. My problem is that sometimes my code deletes the items before printing, thats why I want to loop in the file check and only continue with my code, if a file exists.

My code:

Do {
    $fileDirectory = "C:\pdf";
    foreach($file in Get-ChildItem $fileDirectory)
    {
        $filePath = $fileDirectory + "\" + $file;
        Start-Process –FilePath $filePath –Verb Print -WindowStyle Minimized -PassThru
    }
    Start-Sleep -s 2
    Remove-Item c:\pdf\* -recurse
    Get-Process AcroRd32 | % { $_.CloseMainWindow() }
    sleep 15
} while ($true)
Avshalom
2#
Avshalom Reply to 2018-01-12 13:04:27Z

You can just use simple while block:

While (!(Test-Path C:\pdf\file.pdf -ErrorAction SilentlyContinue))
{
  # endless loop, when the file will be there, it will continue
}

# Next code block here #
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31033 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO