Running External Commands on an .exe from a Batch Code - batch-file

I am trying to run an .exe program entirely from batch commands. This program is in cmd prompt format (the .exe opens a command prompt and the user types in various commands to run). After using this command:
START /b [path] [.exe file]
The desired program is running in the command window. My problem arises next when the program prompts the user for commands (ex: "Enter Name - "). To these prompts I wish to respond with commands that will be recognized by the program (ex: to the prompt "Enter Name - ", I wish to respond "NAME" and click enter/return to display the next prompt).
I have tried using the echo command but after successfully printing what I want into the command line, I need the program to hit enter/return to move on to the next prompt. Any suggestions?
Thanks

The START command still admits stdin redirects. Therefore it should be possible for you to run,
START /b path\to\exe\file <path\to\canned\responses\file
For your specific case you could try,
#echo off
>tempinputfile echo reply foo
>>tempinputfile echo reply bar
start /b path\to\exe\file <tempinputfile

Related

cmd.exe interprets ♪ as ? or ΓÖ¬ when reading .bat file on codepage 437

I wish to use the ♪ character inside a custom prompt for command prompt. This needs to be done from within my AutoRun batch file. chcp gives output Active code page: 437. If I type into the console via cmd.exe running on the Windows Terminal app prompt ♪$G, it works as expected, changing the prompt to ♪>.
However, if I type prompt ♪$G into a .bat file with vscode with encoding CP 437 or Windows 1252, the output is now:
prompt ?$G
?>
When saved with encoding UTF-8, the output is:
prompt ΓÖ¬$G
ΓÖ¬>
How can I change the prompt to ♪> from within a batch file? What encoding should the file be saved in? Will I need to use a hex editor in any way?
To use the ♪ character in a batch file, you need to save the file using the UTF-8 encoding.
You can do this in most text editors, including VS Code.
When you run the batch file, you may need to change the console code page to UTF-8 to ensure that the character is displayed correctly.
You can do this by adding the following line to your batch file:
chcp 65001
This will change the console code page to UTF-8. After that, you can set the prompt using the ♪ character as follows:
prompt ♪$G
This should set the prompt to ♪>.
You should not need to use a hex editor.
For example you can test with batch file below :
#echo off & cls
Chcp 65001>nul
prompt ♪$G
CMD /K
#echo on
This batch file sets up the command prompt with a custom prompt character and then launches a new instance of the command prompt with the /K switch, which keeps the new command prompt window open after executing any commands.
Here's a breakdown of the commands:
#echo off turns off the display of each command in the command prompt, so they are not displayed as they are executed.
cls clears the command prompt screen.
Chcp 65001 sets the console code page to UTF-8, so that it can display special characters like the music note symbol (♪).
prompt ♪$G sets the command prompt to display a music note symbol (♪) followed by a greater-than symbol (>) as the command prompt.
CMD /K launches a new instance of the command prompt and keeps it open.
#echo on turns on the display of each command in the new command prompt window.
Edit :
#echo off
REM Get the current code page and save it in the ORIGINAL_CODEPAGE variable
for /F "tokens=*" %%A in ('%SystemRoot%\System32\chcp.com') do for %%B in (%%A) do set /A "ORIGINAL_CODEPAGE=%%~nB" 2>nul
REM Display the original code page to the user and wait for a key press
echo My default current page is :%ORIGINAL_CODEPAGE%
pause
REM Set the console code page to UTF-8 (65001) and wait for a key press
chcp 65001
pause
REM Set the command prompt to display a musical note (U+266A) followed by a greater-than symbol and wait for a key press
prompt ♪$G
pause
REM Restore the original code page and launch a new command prompt that inherits the current environment variables
chcp %ORIGINAL_CODEPAGE%
CMD /K
Changing the console code page to UTF-8 (65001) can cause issues when piping text between applications that use the C standard library, as some of those applications may not support UTF-8 encoding.
This can result in characters being displayed incorrectly or not at all.
However, in the batch file I provided in the edit section, the original console code page is saved at the beginning of the batch file, and then restored at the end of the batch file using the chcp command.
This means that any negative side effects of changing the console code page to UTF-8 will be limited to the duration of the batch file, and the console code page will be restored to its original value before launching a new command prompt.
So, as long as you are only using the UTF-8 console code page within the batch file itself, and not piping text to other applications that do not support UTF-8, you should not experience any negative side effects.

Get all info through one command prompt

I know when I start a bat file and have pause at the end it will stay open.
Then when pressing the spacebar the bat file ends, and the command prompt will be closed.
Also I know when we use cmd /k it will stay opened, and there is even a command prompt left to enter some bat file code.
Without pause or cmd /k there will be a new window opened and it closes itself.
What I really want to is to have a console window opened, and every time I run a bat file I want the output to be seen on the cmd that is already opened.
I like the way that I can see all the code that was running, without to having close the windows every time, and like it as if all the code will be seen in one opened cmd prompt.
edit: 2
first_file.bat
#echo first
call "C:\ProgramData\Cool\second_file.bat"
#echo thirth
second_file.bat
#echo second

How run batch script to get output in text file

Below executable return string values in command but when i execute below batch script it pop up different command line console and exits hence i am not getting any values in output.txt
how to capture this result ?
c:\
cd C:\Windows\System32
start usbinvoke.exe argument >c:\result\outpput.txt
pause
usbinvoke.exe argument > C:\result\output.txt
Start starts programs in unusual ways. See start /?
See Command to run a .bat file
Your other commands are unnecessary.
You right click a shortcut to cmd and tick Run As Administrator on the compatibility tab
c:\
cd C:\Windows\System32
usbinvoke.exe argument >c:\result\output.txt
pause
start does not wait unless you use /wait argument. Suggest remove start and just run the executable.
You cannot redirect streams with a process that does not wait as the no handle is attached to the process.
If you require start then use arguments /b (same window) and /w (same as /wait).

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

Unable to pause the running batch file for waiting user input intermediately

I couldn't able to pause the running batch file for asking user input intermediately. I tried the commands like PAUSE and SET /P aaa="Press enter to exit" and also as following but nothing worked out.
#ECHO OFF
SET /p aaa="Press enter to exit"
ECHO you typed %aaa%
PAUSE
My batch file internally invokes another batch to build all visual studio projects under the root of the baseline.
One of the project build is getting failed and finally it returns '1' as BUILD FAILED to the main batch file processor. This terminates the MS DOS console immediately and not waiting for user input.
I'm unable to see the MS DOS console window with the end results.
There are various steps to see the end results(Writing the execution output to a text file as like C:\CurrentApp\MyBuild>ABC.bat MyApp.New.BUILD > output.txt) but i would like to see in MS DOS console screen itself with blinking cursor waiting for any user input key.
My batch file looks like:
cd C:\CurrentApp\MyBuild
ABC.bat MyApp.New.BUILD
SET /p aaa="Press enter key to exit"
PAUSE
#ECHO OFF
SET /p aaa="Press enter key to exit"
ECHO you typed %aaa%
PAUSE
You must call your second batch: call ABC.bat MyApp.New.BUILD. Otherwise control passes to ABC.bat but never returns so your pause is never executed. You may think of it as of ABC.bat overwriting your current batch contents and then executing itself.

Resources