Loop until file exists using windows batch command

30,020

This is a fairly straight-forward translation. The code should be pretty self-explanatory:

@ECHO OFF
SET LookForFile="C:\Path\To\File.txt"

:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt

REM If we get here, the file is not found.

REM Wait 60 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 60 >nul

GOTO CheckForFile


:FoundIt
ECHO Found: %LookForFile%
Share:
30,020
Mihir
Author by

Mihir

Updated on January 13, 2020

Comments

  • Mihir
    Mihir over 4 years

    how can i convert following code into windows batch command?

    Here is a perl script which is searching for a file in a while loop, if found it Exits.

    use strict;
    use warnings;
    my $filename = 'something.txt'; 
    while (1) {
    
    if (-e $filename) {
    print "File Exists!";
       exit;
       }
    
    }
    
  • dbenham
    dbenham over 9 years
    IF EXIST is missing a %. The OP original code did not have a delay, so why do you? If you do introduce a delay using TIMEOUT, then you should redirect stdout to nul and use /NOBREAK option.
  • Jason Faulkner
    Jason Faulkner over 9 years
    @dbenham - Updated. I made an assumption about the delay. Batch is easy enough to modify so I figured why not just go ahead and add it.
  • Mihir
    Mihir over 9 years
    @dbenham - above asking this question i have had tried with subroutines and i was getting "Batch Recursion exeeds stack limits". i guess TIMEOUT /T 60 fixed that issue.
  • Magoo
    Magoo over 9 years
    I'd not remove that timeout if I were you. Reduce it a tad - say to 1 sec, but not remove it. If you remove it, CMD will enter a hard loop and absolutely eat CPU.
  • Magoo
    Magoo over 9 years
    @Mihir: If you alter things, show your code by editing it into your original question as supplemental data.
  • dbenham
    dbenham over 9 years
    There is no way TIMEOUT would eliminate a recursion limit error, except that it would take a lot longer to reach the limit. The easiest way to transform your code into something that would give that error would be to change GOTO CheckForFile into CALL :CheckForFile.
  • Jason Faulkner
    Jason Faulkner over 9 years
    I don't see how a stack limit could be reached because nothing is added to the stack. GOTO just changes the execution position within the same script; it doesn't add a recursive call.
  • dbenham
    dbenham over 9 years
    @Magoo - I agree a delay might be a good idea. But a typical modern computer with multiple cores should handle the absense of a delay quite well, especially if you expect the file to becomem available quickly.
  • dbenham
    dbenham over 9 years
    @JasonFaulkner - Yes, there is nothing in the code you posted (with or without TIMEOUT) that could lead to a recursion stack limit error. But you are the one that reported you were getting one, and that TIMEOUT fixed it. I'm just pointing out that TIMEOUT could not possibly have fixed the recursion problem you were seeing. I also tried to offer up a scenario that could generate the error you reported.
  • Jason Faulkner
    Jason Faulkner over 9 years
    @dbenham - I never said that, the OP reported that issue (4th comment in this thread) but it appears he was using sub calls and not a GOTO command.
  • dbenham
    dbenham over 9 years
    Doh, sorry, I need to look closer at the comment signature.