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

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

Related

Need Batch file help searching for specific string being created in another rolling open session?

Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman

Running files in the current window with batch

This is pretty hard to explain so I'll do my best.
When I use the start command in Batch, It'll create a new window etc.
I'm trying to make my own console batch script that isn't as annoying as cmd.exe.
Problem is running commands like help, echo and so on in the current window.
My current code is
:console
set COMMAND=n
set /p COMMAND=%~dp0console.exe^> %=%
if %COMMAND%==exit exit
start %COMMAND%
rem Wait 1000
goto console
I'm using it with Batch to EXE Converter so rem Wait 1000 is valid.
The big problem with the script is that if a user types help or echo and any other commands you can run normally in cmd.exe, it'll open a new cmd.exe window and run the script. I do not want this to happen, is there a way to solve the problem?
Things I've tried:
Instead of using start %COMMAND% I just used %COMMAND%. This
resulted in an error.
(Will add on this list when things that don't work are suggested)
There are two options:
call %command%
Which will wait until the command is over
start /b %command%
Which will start the command in the current window

Batch file - Output current cmd output to log?

I know > and >> redirect a command to a file, but how can I get every line of data from the batch file? I have many commands that echo stuff, but I want just 1 that will echo every single command that's been used in the window to a text document.
Batch file:
#echo off
Choice /n /c 12
If %errorlevel%==1 echo hi
Etc..
You know what works perfectly? Right click > edit > select all. HOW THE HELL DO I DO THAT IN CODE
Say your batch script is called myScript.bat, then redirect when you call it:
myScript >log.txt
You will want to add CALL if used from within another batch script.
You can do the redirection from within your script if you CALL a main routine:
#echo off
call :main >log.txt
exit /b
:main
rem rest of your code goes here.
You probably looking for the tee command. It allows for writing to both STDOUT and a textfile at the same time.
More info here : http://linux.101hacks.com/unix/tee-command-examples/

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

How to prevent batch window from closing when error occurs?

I'm trying to write batch script to create a folder if it does not already exist.
Following up the online examples, below is my script.
The problem is; first pause works, then probably due to syntax error the window closes even before reaches to the second pause, so I can't really tell which part of my script is wrong.
Could anyone show me how to prevent closing window so that I can see what's on the window?
#echo off
:copy theme images over
:designer
echo copying theme images over...
pause
if not exist "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text"
(
md "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text333"
)
pause
You could put this line at the beginning of the batch file:
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
What this line does is, the first time you run it, it re-launches itself in a subprocess that doesn't exit after it finishes running the batch file.
You need to pass the /K switch to CMD, or just open a Command Window and run the batch from the command line.
Press start and type cmd and press enter, you will launch a command prompt.
Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter.
(You might some time to run the cmd as admin: find the cmd in the start menu, right-click on it, choose run as admin).
I recorded the screen (bandicam) for when I couldn't quite read the error message, and then I could replay it; I suppose this is mainly helpful if you already have software on your computer.
using pause at end of batch paused the cmd screen, tanks!
How to prevent batch window from closing when error occurs?
I had the problem when using robocopy. My solution was:
if not %errorlevel% lss 8 pause
For Robocopy every exit code below 8 is a success:
https://ss64.com/nt/robocopy-exit.html

Resources