Pausing a batch file for amount of time [duplicate] - command-line

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Sleeping in a DOS batch file
How to wait in a batch script
I have a program that is kicked off with a batch file.
The first module takes 10 seconds or so to initialize, and I want a way to "sleep" for 15 seconds before the second module is called, but I don't want it to require the user to hit a key like "pause" seems to require.
So, this is what I mean:
echo %PATH%
pause 10
echo %PATH%
In this example, I want there to be 10 seconds in between the echos. Is this possible? I've seen some examples using "ping 1.1.1.1" but it doesn't seem to work all the time correctly.

ping -n 11 -w 1000 127.0.0.1 > nul
Update
Beginner's mistake. Ping doesn't wait 1000 ms before or after an request, but inbetween requests. So to wait 10 seconds, you'll have to do 11 pings to have 10 'gaps' of a second inbetween.

If choice is available, use this:
choice /C X /T 10 /D X > nul
where /T 10 is the number of seconds to delay.
Note the syntax can vary depending on your Windows version, so use CHOICE /? to be sure.

Related

Run multiple MATLAB instances concurrently through a batch file

Is there a way to run multiple .m files in different instances of MATLAB concurrently through a batch file? The task that I want to accomplish is like below:
Open x instances of MATLAB;
Let different instances run different .m files simultaneously (so that my CPU power could be completely utilized);
When all instances finish, exit all MATLAB.
Could a single batch file accomplish this process? The reason I want to use a single batch file to do so is that I want to call this batch file in my MATLAB script. Essentially, I want to do parallel computation. (Since some of my scripts have to be run with MATLAB 2007, which doesn't have the parallel computing toolbox, I have to find a way around.)
Please explain the syntax of your code since I know little about the command prompt.
Currently, I only know how to do the task in sequence, just like the code shown below.
cd "C:\My_MATLAB_folder_path"
matlab r- "mfile01;exit"
matlab r- "mfile02;exit"
...
matlab r- "mfilexx;exit"
If a command prompt could not achieve this process, what alternative methods could I use? One important thing is that whatever method may be used, it must be able to be called in a MATLAB script.
When CMD executes a command or program from a batch file it waits for the launched program to exit, then executes the next command. To overcome this, you can prefix each invocation of MATLAB with start command: start "" matlab r- "mfilexx;exit"
Also making a slight delay between MATLAB invocations might be a good idea to prevent putting hard disk under excessive stress.
So the task could be done with something like this:
#echo off
setlocal
REM Delay is in seconds after /t switch
set "delay=timeout /t 1 /nobreak >nul"
REM Or this for Windows XP: (Delay is in milliseconds after -w switch)
set "delay=ping -n 1 -w 1000 127.255.255.255 >nul"
cd /d "C:\My_MATLAB_folder_path"
start "" matlab -r "this.m;exit"
%delay%
start "" matlab -r "that.m;exit"
%delay%
...
Or Alternatively it can simplified by witting the batch script this way:
cd /d "C:\My_MATLAB_folder_path"
for %%A in (
"this.m"
"that.m"
"other.m"
"add each file in a new line or on the same line separated with space.m" "one another.m"
) do (
start "" matlab -r "%%~A;exit"
%delay%
)

Delay in task scheduler job

I have a job that runs every 15 minutes and gets a datetime. The batch file contains the following PowerShell command to get the current datetime minus 15 minutes:
for /f "delims=" %%a in ('"powershell [DateTime]::Now.AddMinutes(-15).ToString('yyyy-MM-ddTHH:mm:ss')"') do set "dt=%%a"
I write the results of the PowerShell command in my logs. I've been able to notice that every 5 or so job runs, there is a small delay of anywhere between 1-7 seconds. For example, if the job is scheduled to run at 15:00:00, and there is a delay of 5 seconds, my logs will show 14:45:05 as the datetime returned by the PowerShell command.
What could be the reason for this delay? Perhaps the PowerShell command takes longer than expected sometimes? Or could it be that the OS is not running the task on time because it might be struggling to handle resources?
Please let me know if I should provide any more info - I'm having a hard time to be less general/open-ended with the question.

How do schedule a task in 1 hour using command prompt

I need some help setting up a batch file in Windows 7. I want the batch file to be able to create a scheduled task that would execute 1 hour from the moment I click it. I don't want to manually have to put the date and time in, just want it to schedule a task that will execute a set number of hours after I have run the batch file (I'm using 1 as an example).
Please can somebody help me out. I've been searching for an answer all day to no avail.
Recent versions of Windows come with a DOS utility called WAITFOR. Depending on how interactive you want your batch file, and whether it should run a single static command or run whatever you need at the time, you could easily make it work. Like for instance, you could create a batch file on your desktop and drag a program to it and drop it on the batch file. The first thing it would do is prompt for the number of minutes to delay, then it could run the program you dropped on it.
#echo off
setlocal enabledelayedexpansion
set /p _min=Enter the minutes to delay:
set /a _min*=60
waitfor /t !_min! delay
start "" %1
setlocal
Using the start command makes it possible to drop other things too, like a BMP or Word DOC. Anything that you can launch by double-clicking it from Windows Explorer should launch just fine.
After you enter the minutes to delay, just minimize the DOS window. It will close automatically after the delay and after it launches the program or file you dropped on the batch file.
invoke windows task scheduler directly from command line
schtasks /create /TN "Task Name" /TR script.bat /ST 18:00 /SD 21/03/2014 /SC ONCE

VB scripting set delay between commands

Is there any way to put delay between commands e.g we have 5 command i want that run first command and wait for 3 second to run new command, Then run next command and wait for 2 second, Then run 3rd command and wait for 5 second etc etc.
I found WScript.sleep or WScript.sleep()
vbscript:
WScript.sleep(3000) '3000 milliseconds are 3 seconds
powershell:
start-sleep -seconds 3

Sleep command in batch file?

When I'm writing a batch file to run automatically, how do I write it so that when the batch file is run, it can pause for a couple seconds in between commands?
Context:
psexec \\server -u user -p pass cmd
[there needs to be a pause here for psexec to establish a connection]
dir /s >output.txt \\server\shared
*Note: the reason I run the dir command server-side using psexec and not locally is because it's much faster to run dir on a local machine than remotely, and time is of the essence.
When I'm doing this by hand it's obviously easy, I just wait. But running a batch file makes it run all commands at near instant speeds next to each other, regardless of the completion status of the last command. How do I put in a pause?
On Windows Vista / Windows 7 you can use the timeout command:
timeout /T [delay in seconds] /NOBREAK > NUL
On previous versions of Windows, you can use the ping command (the ping command has 1000 ms of delay between each iteration):
ping -n [delay in seconds + 1] 127.0.0.1 > NUL
Some versions of Windows (like Windows Server 2003) has the sleep.exe executable:
sleep [delay in seconds]
Note: Windows Resource kit for 2003 contains sleep.exe command.
If you don't know the Windows version, simply use the ping hack since it'll be available.
There is timeout command in more recent version of Windows:
timeout /T 10
Windows Resource kit for 2003 will install on Windows XP. It contains SLEEP.EXE which can be used from a command batch file.
download is here http://www.microsoft.com/download/en/details.aspx?id=17657
I think the information here: http://malektips.com/xp_dos_0002.html would explain it better than I.
There's still the case of error handling though (what if the remote machine isn't up?). cmd.exe is quite useless for doing any remote activities for the most part, using powershell would enable so much more.
EDIT::
In fact, you can execute a program stored locally with psexec (it gets copied across and executed locally server-side) - would using that be a more viable alternative?
Without knowing what commands you're intending to run it's hard to take it much further.
EDIT(2)::
If it's just the one command you're running, simply store it in a dedicated file, like 'remote_dir_listing.cmd', and then use psexec with:
psexec \\server -u <user> -p <pass> -c -f remote_dir_listing.cmd
This will force a copy of the local file to the remote side each time you execute it (in case you want to expand it). In this way, you bypass the need for a pause at all - only when psexec has got the pipes open will it run, and once it completes, it closes itself silently.