在继续执行脚本之前,如何继续检查文件夹直到其为空

时间:2019-04-23 05:40:50

标签: powershell

我的Powershell脚本将文件从一个文件夹移动到另一个文件夹,并且需要等到该文件被另一个应用程序使用后再移动下一个文件。

我尝试了Do ... Until和Do ... While,但都失败了。我现在正尝试使用自身循环的函数来完成此操作。

当我尝试执行Do ... Until和Do ... while时,一旦我删除了文件,它什么也不做(powershell窗口不会关闭,而只是放在那儿)。如果我尝试使用该功能,则无论是否有文件,它都将直接进入脚本的结尾。

这是功能:

$directoryInfo = Get-ChildItem C:\Temp\Tester1 | Measure-Object
$directoryInfo.count

Function FoldCheck(){
if ($directoryInfo.count -eq 0){return}else{foldcheck}
}
Write-Host "Empty"

这是...直到我尝试为止:

$directoryInfo = Get-ChildItem C:\Temp\Tester1 | Measure-Object
$directoryInfo.count

Do {start-sleep 1}
until ($directoryInfo.count -eq 0)

if ($directoryInfo.count -eq 0)
{move-item C:\Temp\Tester2\Test2.txt -Destination C:\Temp\Tester1}
else
{Write-Host "failed"
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")} 

1 个答案:

答案 0 :(得分:0)

应该重新安排为

do {
   $directoryInfo = Get-ChildItem C:\Temp\Tester1 | Measure-Object
   $directoryInfo.count
   start-sleep 1
}until ($directoryInfo.count -ne 0)

Write-Host "Folder is empty now."