3

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

enrm
  • 645
  • 1
  • 8
  • 22

2 Answers2

3

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
Nagev
  • 10,835
  • 4
  • 58
  • 69
  • 4
    no need to employ PowerShell. Simply replace the `sleep` with `timeout` (see `timeout /?`) (PS: `sleep.exe` once was part of the 2003 resource kit) – Stephan Jan 20 '22 at 10:36
  • 2
    I see, I'd have removed it if someone gave me a reason :) I left it to keep my original answer for context and extra info, who knows, it might be useful in some cases. Very often search engines take me to SO and I find useful answers that may deviate slightly from the OP's question. Anyway, I rarely do Windows stuff and I just used this in a casual personal utility script so whatever works is fine. Thanks for the extra tip, I actually like the countdown, it's cool and informative. – Nagev Jan 20 '22 at 12:01
  • 1
    I like both answers since the powershell one doesn't have specialized output. While the timeout tells you how long you have left. You can also add `/NOBREAK` to the `timeout` to not risk the next command being unintentionally executed early. The powershell command doesn't need the extra update. – ScrappyDev Apr 23 '22 at 05:32
2

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.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Yet if i do `sleep 5` the console window sleeps.. How then do I chain these scripts to make them run as call if they were called one by one? – enrm May 30 '17 at 12:33
  • THERE IS NO SLEEP COMMAND IN WINDOWS CMD / BATCH. – MichaelS May 30 '17 at 12:34
  • Nevermind, sleep was powershell. I retract that sleep works. However, the scripts im calling all depend on it, so I assume, since they work, that they have access to these commands. – enrm May 30 '17 at 12:51
  • @enrm Now we're talking :) But the problem is: your BATCH file is trying to execute `sleep` command. And as it is a PowerShell command, it gives you an error. Can't help you without seeing the other scripts. Have you checked possible path issues as described in my edited post? – MichaelS May 30 '17 at 12:59
  • I just found out the folder where the scripts are running contain a `sleep.exe`. I'm sorry for the confusion. So I just have to cd to the path before running the script? Because i want to have it on my desktop but run it in C:\scripts for example? – enrm May 30 '17 at 13:11
  • You don't have to change directory, just change `sleep.exe` to `"%~dp0sleep.exe"` – Compo May 30 '17 at 13:52