Infinitely loop a command after it returns an error

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xvagosx
Posts: 1
Joined: 28 Dec 2013 17:40

Infinitely loop a command after it returns an error

#1 Post by xvagosx » 28 Dec 2013 17:51

There's a useful script on GitHub for downloading wallpapers from Interfacelift (based on node.js). One of them functions by using the command " interfacelift-downloader 1920x1080 ". The script downloads the images to your current working directory. Upon re-running the command, it will download only what was not downloaded before (no duplicates or overwriting).

A redirection error from their host, though, interrupts the script, and returns an error.

Code: Select all

C:\Users\Vangelis\AppData\Roaming\npm\node_modules\interfacelift-downloader\lib\
downloader.js:41
      filePath = path.join(downloadPath, fileName[0]);
                                                 ^
TypeError: Cannot read property '0' of null
    at EventEmitter.<anonymous> (C:\Users\Vangelis\AppData\Roaming\npm\node_modu
les\interfacelift-downloader\lib\downloader.js:41:50)
    at EventEmitter.emit (events.js:95:17)
    at IncomingMessage.<anonymous> (C:\Users\Vangelis\AppData\Roaming\npm\node_m
odules\interfacelift-downloader\lib\downloader.js:68:18)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)


I want to create a batch file to re-run the command infinitely overnight, and have it download all images.

I have tried the following .bat file (test2 directory exists)

Code: Select all

D:
D:\test2
:loop
interfacelift-downloader 1920x1080
goto loop


However, when the error pops up, it doesn't loop, but it exits.

My question is how can I infinitely re-run a command after it returns an error?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Infinitely loop a command after it returns an error

#2 Post by penpen » 28 Dec 2013 18:56

This may help you, although it depends on which method is used to exit the program (throwing an exception, call WScript.Quit (int), ...):

Code: Select all

:: loopy.bat
@echo off
if "%~1" == "" goto :initiating
if "%~1" == "initiated" goto :endlessLoop
if "%~1" == "terminate" goto :terminating
echo wrong arguments, see bat source for more.
goto :eof


:initiating
cmd /K "test initiated"
goto :eof

:endlessLoop
copy nul file.lock
for /L %%a in (1,0,2) do (
   cscript //nologo //e:JScript "fail.js" %*
   if not exist "file.lock" exit
)
goto :eof

:terminating
del "file.lock"
goto :eof
Be sure not to use a file named "file.lock" as competitive use of this file is not implemented.

You start it with by entering its name into a shell:

Code: Select all

Z:\>loopy.bat

It may be terminated by entering into another shell:

Code: Select all

Z:\>loopy.bat terminate


I've tested it with this JScript code fail.js:

Code: Select all

var O = null;
O.Item (0);

penpen

Post Reply