'Using call <file.bat> results in "sleep is not recognized as an internal or external command.."

I have a script that calls other commands in a for loop:

for %%x in (%CMDS::= %) do (
  call C:\%%x %1%
  echo "%%x complete"
)

However, running this results the console spitting out :

'sleep' is not recognized as an internal or external command,
operable program or batch file.

This is because the files i loop through and run have these commands in them. Why is it that if i run these files one by one they work, but when chained using call they don't? I can sleep in my terminal outside of this script..

Regards



Solution 1:[1]

There is no sleep command in batch. That's why you are getting this error.

EDIT: There is no sleep command in Windows CMD or Batch. BUT: as you can use the command in your console, I suppose there might be a script or a program called sleep. This script or program might be situated in your working directory or in some other directory included in your %PATH% variable. If this is the case, it's possible that your script gives you this error because of a path issue.

Say, you are in C:\SomeFolder and there is a sleep.exe in there. You are calling another script or command which changes the current directory to D:\AnotherFolder. Now another script or command tries to execute your mysterious sleep command assuming the working dir to be C:\SomeFolder but as you are in a different folder (D:\SnotherFolder) now, sleep can't be found. Further, when using call the variable scope of the calling script becomes also the scope for the called script. So it's also possible that variables are being overwritten by different scripts. Such a variable might contain the path to your sleep command. This could also cause an error.

Solution 2:[2]

Thanks to another answer, I solved this error by replacing sleep 5 in my .bat file with:

powershell -Command "& {sleep 5}"

Works fine now. Better still, also tested Stephan's suggestion:

timeout 5

Simpler, and shows a nice message like

Waiting for 0 seconds, press a key to continue ...

Note that some Windows versions require the /t option to define the time.

timeout /t 5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2