# do until $false

**URL:** <https://forums.powershell.org/t/do-until-false/3017>\
**Category:** PowerShell Help\
**Created:** [August 25, 2014, 11:42pm UTC](https://forums.powershell.org/t/do-until-false/3017 "2014-08-25T23:42:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marco-vanwyngaarden](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@marco-vanwyngaarden](https://forums.powershell.org/u/marco-vanwyngaarden)\
**Post date:** [August 25, 2014, 11:42pm UTC](https://forums.powershell.org/t/do-until-false/3017/1 "2014-08-25T23:42:57Z")

</div>

Hello Folks,

I got a question about do and until

i’m creating a small script that checking if a file exist.  
when the file no longer exist i need to copy a file to a new location.

for now i have.

```
do{
    Test-Path d:\test\test.log}
until($false)
{
    Copy-Item -path 'd:\test\test.txt' -Destination 'd:\test\backup\test.txt'
}
```

for now its do not care about true of false its just running endless till i stop the script.  
can someone tell me how i need to fix this.

Thanks for the feedback

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 26, 2014, 12:14am UTC](https://forums.powershell.org/t/do-until-false/3017/2 "2014-08-26T00:14:24Z")

</div>

do {} until ($false) is an infinite loop; it’ll only exit if you use something like break, return or throw from inside the loop body. do { } until ($true) would execute the loop body exactly once (which would make the loop rather pointless.)

It looks like what you intended to do was wait for a file to exist. For that, the code would look something like this:

```
while (-not (Test-Path d:\test\test.log))
{
    Start-Sleep -Milliseconds 100
}

# Do something with d:\test\test.log
```

I used a while loop instead of a do loop here, but the idea is the same; the loop condition is the result from Test-Path. I highly recommend putting a small sleep statement inside the loop body, as I’ve done here, so you don’t just spin needlessly. A tenth of a second isn’t much time in terms of user experience, but makes a massive difference to how fast you’re banging away on the disk or CPU.

---

<div class="post-metadata">

**Author:** ![mikefrobbins](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mikefrobbins/32/272_2.png) [@mikefrobbins](https://forums.powershell.org/u/mikefrobbins)\
**Post date:** [August 26, 2014, 12:15am UTC](https://forums.powershell.org/t/do-until-false/3017/3 "2014-08-26T00:15:19Z")

</div>

Based on the information you’ve provided, I think a while loop would work better for what you’re trying to accomplish:

```
while (Test-Path -Path d:\test\test.log) {
    Copy-Item -Path 'd:\test\test.txt' -Destination 'd:\test\backup\test.txt'
}
```

With the while statement (or loop), the loop only runs if the condition evaluates to true.

A do-until loop checks the condition at the bottom of the loop, runs at least once, and is designed to continue as long as the condition is false. A do-while loop is similar to a do-until loop but repeats as long as the condition is true.

---

<div class="post-metadata">

**Author:** ![mikefrobbins](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mikefrobbins/32/272_2.png) [@mikefrobbins](https://forums.powershell.org/u/mikefrobbins)\
**Post date:** [August 26, 2014, 1:27am UTC](https://forums.powershell.org/t/do-until-false/3017/4 "2014-08-26T01:27:00Z")

</div>

I modified my previous code to reverse the condition but it appears that it will not work because the loop only runs once and doesn’t continue to test the condition to see if the file exists or not:

```
while (-not(Test-Path -Path d:\test\test.log)) {
    Copy-Item -Path 'd:\test\test.txt' -Destination 'd:\test\backup\test.txt'
}
```

I believe this will do the trick though:

```
do {
    if (Test-Path -Path d:\test\test.log -OutVariable Result) {
        Start-Sleep -Milliseconds 100
    }
    else {
        Copy-Item -Path 'd:\test\test.txt' -Destination 'd:\test\backup\test.txt'
    }
}
while ($Result)
```

---

<div class="post-metadata">

**Author:** ![marco-vanwyngaarden](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@marco-vanwyngaarden](https://forums.powershell.org/u/marco-vanwyngaarden)\
**Post date:** [August 26, 2014, 5:50pm UTC](https://forums.powershell.org/t/do-until-false/3017/5 "2014-08-26T17:50:00Z")

</div>

Thanks Dave and Mike,

It seems i’m using the wrong condition for the loop.  
The following code is working i my case.

```
while (Test-Path -Path 'D:\test\test.loc')
{
    Start-Sleep -Milliseconds 100
}
Copy-Item -path 'D:\test\test.mdb' -Destination 'D:\Test\backup.mdb'
```

it was needed to check for a access DB lock file to disappear and copy the file for backup.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/do-until-false/3017/6 "2024-05-16T20:47:36Z")

</div>

Closed on May 16, 2024, 8:47pm UTC


