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

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

Related

Start batch file from terminal and pass a specific argument

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

How do I "echo" a command in a batch script?

I'm trying to shutdown my PC using a batch script, but when I type "#echo shutdown /r", CMD just displays "shutdown /r" on the screen, instead of doing the command. I'm currently new to batch scripting, so forgive me on my knowledge on the command prompt and batch scripting.
Here is my code (I don't think this will help):
#echo off
title Shutdown.bat
color 0a
echo:
echo Your PC will shutdown (testing).
pause>nul
#echo shutdown /r
pause>nul
command echo text show you only text on command line. Use only shutdown /r for shutdown you pc.

Batch file that run jar file on cmd

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
)

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

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