Unable to launch multiple programs via a batch file - batch-file

Am trying to create a batch file, that would launch multiple programs. But unfortunately, things don't seem to work.
Kindly, find below my requirement:
Open InfluxDB server
Launch Grafana application.
Commands used in the batch:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
Start.cmd
timeout 5
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
grafana-server.exe
The above script, launches InfluxDB. But doesn't moves further.
Could you please suggest me, on how to proceed?

You need to use the call keyword to have control returned to the caller after invoking another batch script:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
call start.cmd
...
Should start.cmd run InfluxDB synchronously (i.e. not in the background) you need to launch it in a separate window:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start "InfluxDB" cmd /c start.cmd
...

#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start InfluxDB
ping -n 6 127.0.0.1 > nul
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
start grafana-server
Edit the "start InfluxDB" and "start grafana-server" to be the correct exe names, without .exe

Related

How to run batches NOT using cmd.exe (like Cmder.exe?)

I've recently installed cmder (or simply 'Commander') portable console emulator for windows, and really like the functionalities it comes with. But how can you (if at all) run batches through it like you can with the Windows command line?
The details:
If you write...
cd C:\test\
REM executeSomething.exe
Pause
... to a textfile and save it as a .bat file you can do can do pretty much anything just by double-clicking that file. I've got a work-flow where I launch a web-application through a batch file. Sometimes this applicaton launches automatically in Chrome, and sometimes it does not. In that case, I'll have to copy and paste a URL to Chrome manually. And that's a real pain. With cmder.exe that's much easier, but I'll have to manually navigate to a folder and start the application through Commander it manually without the luxury of a .bat file.
The functionality I'm trying to run automatically with cmder.exe is simply changing a folder and starting an executable file, specifically Jupyter Lab:
cd C:\jupyterlab\
jupyter lab
Pause
I've tried various approaches with:
#echo off
set CMDER_ROOT=C:\Cmder
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
as described here, but with no success. The example above does launch commander, but also raises a syntax error:
Current directory: C:\batches
Command to be executed: "C:\Windows\system32\cmd.exe" /k
"C:\Cmder\vendor\init.bat cd C:\batches && "
I'm obviously on to somehting here since I'm able to start Commander, but I'm not sure how to edit the remaining code to run the necessary steps. I do realize that the /title "Homestead VM" part just edits the title of the Commander window:
but I have no idea about the rest.
If this just isn't possible, I'm going to have to try to reassociate .bat files in Control Panel > Default Programs > Associate a filetype, but I'm hoping to avoid that.
Thank you for any suggestions!
System info:
Windows 7, 64 bit
Commander v1.3.12
Edit 1: My (failed) attempt trying to follow a suggestion from Gerhard Barnard
I've saved a file named please.cmd to C:\Windows\System32 that contains this:
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"
I've also saved a file named SObatch.bat in C:\batches that contains this:
if not defined myComs set myComs=0 && please %~0
#echo off
ping localhost
echo %userprofile%
pause
Upon double-clicking SObatch.bat a Windows prompt is opened, and the pings are run:
I just narrowed the window to leave out real-world user-names. But the ping functions are being run and the usual responses are returned.
So I guess it still seems that I've broken something.
Create a file called please.cmd and save it to C:\Windows\System32 as administrator. It should then exist as c:\Windows\System32\please.cmd
#echo off
if not defined CMDER_ROOT set "CMDER_ROOT=C:\Cmder"
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %*"
Now when you create a batch files you need to make some changes in the top of each batch file you want. You would need to add the below line to the very top of the script.
"%systemroot%\system32\please.cmd" "%~f0" && goto :eof
You can also from cmd please command to launch it from the cmder shell. Example:
please ping localhost

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

How to ping a server only once from within a batch file?

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:
ping www.google.de -t
and displays the ping, so it would look like this:
Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms
My problem is, that this will result in this when I execute this command as script:
My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.
I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.
So how to write the batch file to start this command only once and display the ping result?
I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.
my batch file contains below code only
ping 172.31.29.1 -t
with file name as ping.bat
with file name abc.bat
Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:
ping - latest Microsoft documentation for this Windows command
ping - Windows XP documentation for this Windows command
Explanation for option -t given by Microsoft:
Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.
You may want to use:
#%SystemRoot%\system32\ping.exe -n 1 www.google.de
Or to check first if a server is available:
#echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer
echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF
:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
For bash (OSX) ping google.com -c 1 (incase search brought you here)
if you want to use the name "ping.bat", a small trick is to use this code:
#echo off
cd\
ping google.com -t
Just add that "cd\" and you are fine... ;)
Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add #echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.
#echo off
ping www.google.de -t
To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like
%SystemRoot%\Windows\System32\PING.EXE www.google.de -t
Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below
where ping
If the command is recognizable; it will output the path like
C:\Windows\System32\PING.EXE
Else will result in error
I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!
From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1
i used Mofi sample, and change some parameters, no you can do -t
#%SystemRoot%\system32\ping.exe -n -1 4.2.2.4
The only thing you need to think about in this case is, in which directory you are on your computer.
Your command line window shows C:\users\rei0d\desktop\ as your current directory.
So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...
So the complete command would be:
cd ..
ping XXX.XXX.XXX.XXX -t
Having 2 scripts called test.bat and ping.bat in same folder:
Script test.bat contains one line:
ping google.com
Script ping.bat contains below lines:
#echo off
echo Hello!
pause
Executing "test.bat" the result on CMD will be:
Hello!
Press any key to continue . . .
Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script).
Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.
Easy ways to avoid this:
Do not name your script "ping.bat".
You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
Create a text file with text "#%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat".
Just click and run it and you will get the result.
So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).
The answer to your question is this
Ping -n 1 0.0.0.0
But if you want it to be faster than this, this will be your answer
Ping -n 1 -l 1 0.0.0.0
Note: Replace 0.0.0.0 with your desired IP address
Just
write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator

Can't kill program by taskkill

I use Tecplot to opem its script file (*mcr) to do visulization. After I write the batch codes to first open tecplot to launch the *mcr file and then kill it, I found the codes can't terminate the program. The codes in batch file is as following:
cd .\Re100\17\
cd .\0.001
tec360 extract.mcr
ping 192.0.2.2 -n 1 -w 100000
taskkill /F /IM tec360.exe
cd ..\
cd .\0.005
extract.mcr
ping 192.0.2.2 -n 1 -w 100000
taskkill /F /IM tec360.exe
But, if I manually closed the Tecplot then the batch file can work. Any help on this will be highly appreciated.
Try looking on this site http://technet.microsoft.com/en-us/library/bb491009.aspx It will show you how to use the task kill command.
You simply need to add a $!QUIT command in the end of your macro file "extract.mcr" that will close the tecplot after the macro has finished, this way your tecplot GUI will close itself. The macro file should look like this
#!MC 1410
# some other macro commands .....
$!QUIT
In my experience you don't really need the GUI of tecplot to load, you can run your macro in the batch mode which will be quick, and that won't require $!Quit command either. to run in batch mode your batch file look like this
cd .\Re100\17\0.001
tec360 -b extract.mcr
cd ..\0.005
tec360 -b extract.mcr
On an additional note, if you have same file name i.e. extract.mcr and a lot of folders where you want to run, then instead of hardcoding the bath like you did, in windows here is how it would be,
SET my_dir="Re100\17\0.001" "..\0.005"
FOR %%A IN (%my_dir%) DO (
cd %%A
tec360 -b extract.mcr
)
Now you can add as many folders in first line as you want with a space separating the paths for example
SET my_dir="path 1" "path_2" "path 3" "path 4"
and so on..

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Resources