As the title says what parameters would you use in a batch file to continually execute a command e.g.
start notepad
loop
Another option in a single line (which will also work from the command line):
for /l %x in (1,0,2) do (start /wait notepad)
If you're using that in a batch file, use
for /l %%x in (1,0,2) do (start /wait notepad)
instead.
Use goto:
:loop
start /wait notepad
goto loop
Note that I have used start /wait here. If you don't do that, your batch file won't wait for notepad to exit, and you'll start a zillion notepads and probably eventually crash.
Related
How can I write a batch file that runs cmd.exe, enters the partial command robocopy then waits for user input to complete and execute the robocopy command? It seems like it should be the simplest thing to do but no method I've tried enters the command in such a way as to hold and wait for user input and then successfully execute the completed robocopy command
For example:
#echo off
set var="robocopy "
cmd.exe %var%
appears to work but then, for example, user-inputting /? to bring up robocopy's info instead brings up cmd.exe's info
Another example:
#echo off
cmd /k robocopy
Runs robocopy with no destination/source folders or switches, then closes robocopy and waits for a new user-inputted command.
what I'm trying to do is have a batch file that when I click it a cmd window will open with the partial command robocopy already entered ready for me to complete the command with source/destination/switches and execute it with an enter key press - I use robocopy all day long so this would be a big time saver.
it's not possible in the way you seem to think. That would mean to mess with the keyboard buffer (was pretty common with the C64 back in those times, but it's not possible in (pure) batch).
But the following should give you a good start for automation. It assumes, you use the same parameters each time and just want to give it a source and a destination folder. Adjust to your needs (especially the params - I added /L to prevent it from unwanted actions while testing):
#echo off
set "command=robocopy"
set "params=/L /E /MOV /MT:12 /FP /log+:robocopy.log /TEE"
set /p "source=Enter Source: "
set /p "destin=Enter Destination: "
%command% "%source%" "%destin%" %params%
The cmd.exe or cmd.exe /k isn't necessary, until you want robocopy to work in a new window.
I'm currently working on a batch file that should download files via their URL and them run formatting script on them however I don't know how to to delay the batch-file during downloading, however since it's a direct download the window doesn't stay open.
here's where I'm at :
START "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.oui.oui.fr/oui
MOVE C:\\Users\\*\\downloads\\*.csv %~dp0
EXIT
I would like to wait for the first line to finish before continuing.
Thanks for your consideration
From cmdline (cmd.exe) run start /? and you will find some help. There is a specific line in the help file for the /wait switch which reads:
WAIT Start application and wait for it to terminate.
So simply start chrome with the /wait switch:
Start /wait "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.oui.oui.fr/oui
As per your comment, the above will not work. It is probably best to test if the file exists, chrome will have a .crdownload extension when still downloading. So let's test that the *.csv.crdownload does not exist.
start /wait "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.oui.oui.fr/oui
:hold
timeout 5
if /i not exist *.csv.crdownload (MOVE "C:\\Users\\*\\downloads\\*.csv" %~dp0) else ( goto :hold)
exit
What I got is a list of bat files:
file1.bat
file2.bat
…
file29.bat
I need them to run one after each other. Meaning when the file1.bat closes file2.bat starts and so on.
I tried this, but it doesn't work properly:
start /wait call file1.bat
start /wait call file2.bat
You might want to add more to your question to make it easier to understand. My guess is that you want the bat file to open the next one then close itself after that.
If that's what you want to do; add these commands to each of the files:
start file2.bat
exit
Of course, you'll want to change start file2.bat to start file3.bat and so on for each file.
If you want file1.bat to manage all of the files, I don't think that's possible in batch.
You didn't describe how exactly it's not doing what you expected. I'm guessing that what happens is you're having to shut down each script before the next one will continue.
Documentation on start says:
WAIT Start application and wait for it to terminate.
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If you must use start then you could force it to use the /c switch which will automatically close the window once it's done:
start /wait cmd /c call file1.bat
I'm not really sure you accomplish anything by using call so that ought to be equivalent to just:
start /wait cmd /c file1.bat
Using start creates a new window for each program and you may just want to have it all run in a single command processor window.
As noted by Biffin you can just list them all out in a master script and they will run in order.
call file1.bat
call file2.bat
...
call file29.bat
And a shorthand for that is:
for /l %%f in (1; 1; 29) do call file%%f.bat
Remember to double up those percent characters inside a batch script but not on the command line.
This question might explain some of the unexpected behavior you were seeing.
How to run multiple .BAT files within a .BAT file
Apologies if duplicate, I want to run a jar file from command prompt, So instead of going to the specific path can i create a batch file,
Simply double click on batch file will do my task,
I found something at [1]: Run a Command Prompt command from Desktop Shortcut but it do not work in my case, I want to run java -jar squirrel-sql.jar
I would like to make a batch file that:
1)Opens cmd.exe
2)Within that Command Prompt runs java -jar squirrel-sql.jar which is present on desktop
3)Leaves the window open so that I can run additional commands if I wish to
How can I do this?
Save the following as squirrel.bat:
java -jar squirrel-sql.jar
REM /P causes SET to prompt and wait for Enter.
REM The underscore is used to ensure that "sure" is not already
REM an environment variable.
set /p _sure="Do you want to exit? [press Enter]"
REM /I causes a case-insensitive comparison
if /I NOT "_sure"=="y" (
REM /B exits the batch script but not the CMD window
exit /b
) else (
REM Any other modifications
)
i need to make a batch that looks for file
and once it exist it will run a windows cmd batch
i tried the below code but its not working
#ECHO off
IF EXIST C:\file.txt CALL batch.cmd
I interpret "and once it exist..." as "keep looking and as soon as the file exists...". Use a loop:
#echo off
look:
timeout 1 >nul
IF NOT EXIST C:\file.txt goto :look
call batch.cmd
Note: never run a loop without a delay (timeout here) to avoid high CPU load)
If you like to write code all in a line, you should do it like this
#ECHO off & IF EXIST C:\file.txt CALL batch.cmd