Start batch file from terminal and pass a specific argument - batch-file

I have a batch file with the following code which is starting a jarfile.
#ECHO OFF
IF NOT EXIST jarFile.jar GOTO ERROR
IF EXIST jarFile.jar Start java -jar jarFile.jar
ECHO Successfully started!
PAUSE
EXIT
:ERROR
ECHO Jar file doesn't found!
PAUSE
EXIT
I would like to ask you when i start the bat file from CMD prompt, to use a parameter "-force" which can skip the pause after starting it.
Example: start someBatFile.bat -force(the new parameter) and when it is used the "-force" parameter i want to skip the pause. Is it possible to do that and if it is can you help me ?

Change the line with PAUSE to the following:
IF NOT !%1!==!-force! PAUSE

Related

pause in batch file not working

I have a batch file that tests to see if a file exists.
After that, it has to pause.
But pause is not working for some reason.
Any help please. thanks much
if exist C:\autoexec.bat
ECHO Folder C:\autoexec.bat exists
PAUSE
Pause works at command prompt.
The file exists for sure. But I can't see Echo as well as Pause
The screen just disappears once I run the .bat file
Do you want to write:
if exist C:\autoexec.bat ECHO Folder C:\autoexec.bat exists
PAUSE
Note that shell after the if command is expecting a command to execute. If there is no command after de condition it should abort batch with:
"The syntax of the command is incorrect."
Edit. Mr.Helpy Got it first.
Note if you want multiple lines use a block
if exist C:\autoexec.bat (
ECHO Folder C:\autoexec.bat exists
REM other commands...
)
PAUSE
The answer is in the command. The valid syntax for if command is
IF [NOT] EXIST filename command
OR
IF [NOT] EXIST filename (command) ELSE (command)
The word command at the last is very important. What I did was this
#Echo off
if exist C:\autoexec.bat goto hi
if not exist C:\autoexec.bat goto bye
:hi
ECHO Folder C:\autoexec.bat exists
PAUSE
:bye
Echo Folder C:\autoexec.bat does not exists
pause
And it worked like a charm
Regards,
See http://ss64.com/nt/if.html
Basic troubleshooting: run from a cmdwindow, not per doubleclick.
Then you see, that
if exist C:\autoexec.bat
give you a syntax error, that breaks the execution of your batchfile.
if expects a command to execute (at the same line), but there isn't one. So the echo line is never reached.

How to open a .bat file from another in a certain situation

So I am using the following code:
:begin
SET /P runscript= [Question Here]
if %runscript%==:100 goto run blahblah.bat
if %runscript%==EXIT goto :A
pause
I am trying to make there be an option to open another .bat file in a different window, but when I answer :100, command prompt just shuts down. I am trying to be as clear as possible as to what I am trying to do, but this is just a snip of a very big project I am working on.
Try start cmd /k to run the bat in new window. For goto, notice no colon before A. Lastly, your set command as written will include the space. I closed it here.
:begin
SET /P runscript=[Question Here]
if %runscript%==:100 start cmd /k blahblah.bat
if %runscript%==EXIT goto A
pause
if %runscript%==:100 goto run blahblah.bat
will attempt to find the label run on entry of :100
If you want to transfer execution to the batch blahblah.bat then remove the goto run

Open command window via batch file when cmd.exe is blocked

cmd.exe is blocked at school. However batch files runs normally and sometimes it's very annoying to type the command in the batch file, write pause>nul and run the batch file to execute a command. Is there anyway to input commands from the user and execute them as cmd.exe does?
#echo off
:Loop
echo %cd%^>
set /p cmd=SkYWAGz Enter Command to Run (Press Ctrl + C to exit)
%cmd%
Goto Loop
Your very own command processor
I found the solution, the code in the batch file would be start
This works for me:
#echo off
break off
title Command Prompt
cls
:cmd
set /p cmd="%cd%>"
%cmd%
echo.
goto cmd

Execute batch file on separate volume - System cannot find the path specified

I have a batch file that executes another batch file with a couple command line arguments. This is the command:
call "C:/Program Files (x86)/salesforce.com/Data Loader/bin/process.bat", "D:/Scripts/CS Dashboard/DataLoader", "casesByCategory_LM"
My problem is that upon execution, it says "System cannot find the path specified". I realize it is because I am specifying a filepath on a separate drive D:
I tried adding the filepath to the system environment variables but that didn't do any good.
I also tried running this from a command prompt opened from the D: drive which also didn't work.
process.bat is a file provided to me by SalesForce to use their DataLoader. These are the contents:
#echo off
if not [%1]==[] goto run
echo.
echo Usage: process ^<configuration directory^> ^[process name^]
echo.
echo configuration directory -- directory that contains configuration files,
echo i.e. config.properties, process-conf.xml, database-conf.xml
echo.
echo process name -- optional name of a batch process bean in process-conf.xml,
echo for example:
echo.
echo process ../myconfigdir AccountInsert
echo.
echo If process name is not specified, the parameter values from config.properties
echo will be used to run the process instead of process-conf.xml,
echo for example:
echo.
echo process ../myconfigdir
echo.
goto end
:run
set PROCESS_OPTION=
if not [%2]==[] set PROCESS_OPTION=process.name=%2
..\Java\bin\java.exe -cp ..\dataloader-29.0.0-uber.jar -Dsalesforce.config.dir=%1 com.salesforce.dataloader.process.ProcessRunner %PROCESS_OPTION%
:end
Remove the commas and use backslash as path separator.
call "C:\Program Files (x86)\salesforce.com\Data Loader\bin\process.bat" "D:\Scripts\CS Dashboard\DataLoader" casesByCategory_LM
If this does not work then you should post your process.bat code

Run commands in a batch file which is inside another batch file

How to run commands in a batch file which is inside another batch file......
I am trying to run commands in different console other than command prompt in a batch file but not able to do so.I am able to start the other console in batch file but not able to pass commands on to it.
My first interpretation of the question led me to believe that Sampath wanted one batch script that has two sets of commands. Calling it would run the 1st set of commands in the parent window, and a second window would open that would run the same script with thd 2nd set of commands.
"%~f0" will give the full path to the currently executing batch script. A simple command line argument serves as a switch to determine which code to run.
#echo off
if "%~1"==":PART2" goto %~1
::use this line if 2nd window is to remain open upon completion
::start "%~f0" :PART2
::use this line if 2nd window is to close upon completion
start cmd /c "%~f0" :PART2
echo Test parent output
pause
exit /b
:PART2
echo Test child output
pause
exit /b
Andriy M suggests Sampath wants to be able to dynamically send commands to the 2nd window. This can be done with 2 scripts that I will call master.bat and slave.bat.
The slave.bat simply reads commands from stdin and executes them. The master.bat launches the slave with input redirected to a command file and then appends commands to the command file.
Here is an example of master.bat that demonstrates dymamically sending commands to the slave. Note that the master prompts for a command, but the slave window will have the focus. Make sure you click on the master so you can enter the command of your choice.
#echo off
:: create an empty command file
type nul >cmds.txt
:: start the slave with input redirected to the command file
start slave.bat ^<cmds.txt
:: issue some commands by appending them to the command file
>>cmds.txt echo echo command 1
>>cmds.txt echo echo command 2
>>cmds.txt echo echo(
>>cmds.txt echo rem /?
:: ask for a command to send to the slave
set /p "cmd=Enter a command to be sent to the slave: "
:: send the command
>>cmds.txt echo %cmd%
::pause so we can see the results in the slave window
for /l %%n in (1 1 1000000) do rem
::tell the slave to exit
>>cmds.txt echo exit
And here is the slave.bat
#echo off
:top
set "cmd="
set /p "cmd="
%cmd%
goto :top
You could try a call statement:
call batchname.bat
this will run the specified batch file in the current open prompt
It almost sounds like what you want is a file that holds commands that you want to run, and to use a batch script to call on those commands when you want?
I've implemented this by creating a batch file that holds all the commands (code snippets) that I find useful, and then using my other batch scripts to call on that "master" file for my snippets.
For example, in my MASTER_BAT.BAT file, an example of a snippet to create dates in different format for usage look like this:
GOTO:%~1
:GET_CURRENT_DATE
:: Created: 1/19/2012
:: Creates variables for the date format in different forms.
:: No additional arguments required
SET DISABLED=0
IF [%DISABLED%] == [1] GOTO:EOF
:: Created: 11/30/11
:: Creates date formats.
Set mdy=%date:~4,2%-%date:~7,2%-%date:~12,4%
Set mdY=%date:~4,2%-%date:~7,2%-%date:~10,4%
Set Dmdy=%date:~0,4%%date:~4,2%-%date:~7,2%-%date:~12,4%
Set DmdY=%date:~0,4%%date:~4,2%-%date:~7,2%-%date:~10,4%
Set ymd=%date:~12,4%-%date:~4,2%-%date:~7,2%
Set ymd=%date:~10,4%-%date:~4,2%-%date:~7,2%
GOTO:EOF
And in my CHILD_BAT.BAT, I want to use that snippet to create the date formats... lets say I want to make it so that I can call the date by the current date in mm/dd/yy format:
CALL MASTER_BAT.BAT "GET_CURRENT_DATE"
ECHO %mdy%
PAUSE
Your output for CHILD_BAT.BAT would be:
1-23-12
Press any key to continue...
Also, any variables created in your CHILD_BAT.BAT prior to the CALL command will be passed to the MASTER_BAT.BAT script as well. However, for loop interation that includes a CALL will not pass the for loop temporary variable.
Hope this is helpful.
EDIT: Note that my snippet is usable for the U.S. date format.

Resources