14

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;
   }

}

1 Answer 1

28

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%
Sign up to request clarification or add additional context in comments.

11 Comments

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.
@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.
@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.
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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.