检查文件是否存在然后继续使用Powershell中的代码

时间:2018-01-12 12:45:05

标签: file powershell loops exists

我需要检查c:\ pdf中是否存在某些pdf,如果存在则继续执行,如果不存在,则每隔15秒再次检查一次。

我的实际代码只检查文件夹中的文件并打印是否有东西,如果不是一次又一次重复。我的问题是,有时我的代码会在打印前删除这些项目,这就是为什么我想循环文件检查并且只继续使用我的代码(如果文件存在)。

我的代码:

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)

1 个答案:

答案 0 :(得分:1)

你可以只使用简单的块:

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

# Next code block here #
相关问题