How to "replace" Command Prompt in Windows 8.1? - batch-file

I have a batch script in my Startup folder which begins with chcp 65001. This will crash CMD after the first command after chcp but it won't crash ConEmu.
So how do I make it run in ConEmu instead of CMD after logon?

Did you read docs?
ConEmu64.exe -run "path-to-your-batch.cmd"

Related

ConEmu: how to open a standard windows cmd.exe window from conemu (and execute a command - e.g. start a batch file)

I need to open a legacy app which requires a standard cmd.exe
I would like to be able to spawn it from a batch file (.bat) and run it from command line in ConEmu
Don't see a way to do that.
start and cmd open a new tab in ConEmu at best - while I need the legacy cmd.exe window.
OK, it looks like I found a way - rather non-obvious but it seems to work:
[start] conhost cmd /c <your command>
or (if you want cmd window to continue beyond the execution of your command):
[start] conhost cmd /k <your command>
start in front is optional (useful if you don't want to block the original conemu terminal until completion of your command).

How to change directory with BAT file?

I would expect the below code to open up in the C:\Users\zjafri\Desktop\Arthur\Runner2 directory when running this bat file, but instead it opens up to my desktop directory.
Does somebody have an idea why?
%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
CD C:\Users\zjafri\Desktop\Arthur\Runner2
The console output is:
C:\Users\zjafri\Desktop>C:\windows\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
(base) C:\Users\zjafri\Desktop>
The used command lines are wrong for the wanted behavior which is activating Anaconda environment with making a specific directory the current directory and keep the command process running for entering manually more commands to execute.
A batch file is a script. A script needs a script interpreter executable. The interpreter for Windows batch files is the Windows command processor cmd.exe which is with full qualified file name %SystemRoot%\System32\cmd.exe or %ComSpec%. SystemRoot and ComSpec are two predefined Windows Environment Variables.
Windows command processor halts the processing of a batch file on starting an executable until the started executable terminated itself.
What does happen on cmd.exe instance processing the batch file executes the following command line?
%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
Windows command processor starts one more command process to process the other batch file and keeps running after batch file execution finished because of option /K enclosed uselessly in double quotes. So the started second instance of cmd.exe does not terminate itself. For that reason the first instance of cmd.exe waits and waits and waits for termination of second instance of cmd.exe before it continues processing the batch file with reading the next command line.
Therefore the command line CD C:\Users\zjafri\Desktop\Arthur\Runner2 is executed by first command process only when the user typed exit in second command process to terminate second cmd.exe instance.
One solution would be the usage of following command lines in a batch file:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat" "C:\ProgramData\Anaconda3"
cd /D "%UserProfile%\Desktop\Arthur\Runner2"
This batch file results in executing the batch file activate.bat, next is executed command CD and then processing of batch file is finished.
What happens next depends on how the batch file was started. If just a double click was made on the batch file, Windows called cmd.exe with using implicit option /C to execute the batch file and then close the command process. So on double clicking the batch file, a console window opens, the two command lines are executed and the console window closes as cmd.exe terminates itself.
But if the user opened a command prompt which means starting cmd.exe with using implicit the option /K, and executes the batch file from within the command prompt window by typing its full qualified file name without or with the completion help by hitting one or more times TAB after entering just a few characters of a folder/file name, and hits RETURN or ENTER to execute the batch file, the command prompt window remains opened and Anaconda environment is activated with directory %UserProfile%\Desktop\Arthur\Runner2 being the current directory.
Well, first opening a command prompt window and manually running the batch file with full path is not really handy. The batch file could be stored with a short file name in one of the folders of which path is present in value of environment variable PATH like the Windows directory. This would make it possible to open a command prompt, type just the file name of the batch file and hit key RETURN or ENTER to execute it.
Another handy solution would be creating on user's desktop a shortcut file (*.lnk) with file name Anaconda3 on which in properties of the shortcut is configured:
Target: %SystemRoot%\System32\cmd.exe /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
Start in: C:\Users\zjafri\Desktop\Arthur\Runner2
Comment: Opens a command prompt and activates Anaconda3 in Runner2
There are multiple advantages on using a shortcut file. A shortcut key can be defined to start Windows command processor and execute the batch file to activate Anaconda environment independent on which application has currently the input focus and if the desktop is behind an application window or in front of all other windows. On the tabs Options, Font, Layout, Colors of the shortcut properties the options and the look can be defined which should be used on using Anaconda like using a console window with more lines/columns and with a larger font as by default.
Although the usage of a shortcut file would be definitely the best solution for this use case, it is also possible to run a batch file with a double click to open a command prompt window to execute the batch file to activate Anaconda environment and keep the command process running with current directory set to wanted directory.
Such a batch file would require just one command line:
#start "Anaconda3" /D"%UserProfile%\Desktop\Arthur\Runner2" %ComSpec% /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
In this case cmd.exe processing the batch file with this command line starts a new command process with option /K to keep the command process running after execution of the batch file by started cmd.exe with Anaconda3 as title for the new console window and with setting before starting cmd.exe the directory CD C:\Users\zjafri\Desktop\Arthur\Runner2 as current directory. cmd.exe does not wait for termination of the started second cmd.exe in this case and so the cmd.exe instance processing the batch file with this single command line terminates immediately.
The disadvantage of this batch file solution is that the user has no possibility to configure the options, font, layout, colors of the command process which keeps running after activating the Anaconda environment.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
cmd /?
start /?
PS: I have installed neither Anaconda nor read its documentation. So I don't know what happens on execution of C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3 and what is the current directory after execution of this batch file. If the batch file is good coded and the argument C:\ProgramData\Anaconda3 does not mean to change the current directory to this directory, then everything should work as written above. Otherwise the provided solutions need to be adapted depending on what activate.bat changes on environment, i.e. when it changes the current directory.
perform the cd command prior to the cmd command (concatenated) and wrap it all in a code block.
(CD C:\Users\zjafri\Desktop\Arthur\Runner2 & cmd.exe /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3)

how to run pass docker terminal commands using .bat file

I Want to invoke docker compose using bat file.I have tried to invoke using the following but its not executing the commands.
this is my .bat file
echo on
cd C:\Program Files\Docker Toolbox\
start start.sh cd desktop
cd test
docker-compose up
Is there any other way to execute docker commands using bat file.or any other file.
A batch file is a script file. A script needs an interpreter. For a batch file the interpreter is cmd.exe – the Windows command interpreter.
A *.sh file is also a script which needs an interpreter. The interpreter is on Unix/Linux systems sh, bash, ksh, ... which are also executables but without file extension .exe because on Unix/Linux executables usually do not have a file extension.
On Windows there is no interpreter installed by default for Unix/Linux shell scripts. If start start.sh works at all than because of having installed a shell interpreter on Windows which has been registered in Windows registry as application for opening *.sh files which means interpreting the commands in the shell script file by started application.
But commands in a batch script interpreted by cmd.exe and executed within a Windows command environment can't be executed in shell environment of the shell interpreter.
start start.sh starts a new process running parallel to the Windows command process created for execution of the batch file. The batch file processing immediately continues after this line with executing the next command in command process while the shell interpreter process interprets parallel the commands in start.sh.
So what you need here is a batch file which creates a shell script to call start.sh and executes other shell commands in shell environment by shell interpreter.
The batch code below might work. It is not tested by me as I don't have Docker Toolbox nor any shell interpreter installed on my Windows machine.
#echo off
set "ShellScriptFile=%TEMP%\%~n0.sh"
( echo start.sh
echo cd desktop/test
echo docker-compose up
) >"%ShellScriptFile%"
start "Docker Compose" /D"%ProgramFiles%\Docker Toolbox" /wait "%ShellScriptFile%"
del "%ShellScriptFile%"
set "ShellScriptFile="
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~n0 (name of batch file without file extension and path)
del /?
echo /?
set /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of redirection operator > used here to create the shell script to execute with the 3 lines:
start.sh
cd desktop/test
docker-compose up
Those 3 lines are executed in the shell environment by shell script interpreter.
To run docker-compose via a bat file,
1. create a bat file eg run-docker-compose-up.bat
cd c:\docker
docker-compose up -d
pause
c:\docker is where I placed my docker-compose.yml
-d run containers in the background docker compose up docs
docker-compose must run as Administrator.
While a bat file itself cannot be marked as run as Administrator, you can either
right click on the bat file and choose 'Run as administrator'
or create a shortcut to the bat file and mark it as run as Administrator
(right click on shortcut, choose Properties, choose Advance, choose 'Run as administrator')
As I solved this problem, in a docker-compose case:
Go to your docker-compose.yml folder
On that, create a batchfile, such as: mydockerbatchfile.bat, using notepad (or other)
On this file, type your docker commands sequence', such as:
echo on
docker-compose down
docker container prune --force
docker system prune --volumes --force
docker image rm xxx/web-api
docker system df
docker image ls
pause
OBS: the commando pause will allow you check the results execution, in the screen)
Save -> make sure you saved in batch format/extension (mydockerbatchfile .bat)
In your windows desktop, create a ShortCut for the 'item 3 batch-file' (here - do not set the shortcut to "run as administrator")

Run a command promt and run a command

I need to write a command which is calling other command prompt of Oracle and running a command on it. Currently, I am running it using below command
D:\Oracle\setenv.bat /k BuildApp
But it only runs setenv.bat and quits and doesn't run BuildApp. BuildApp is to run on a command prompt opened by setenv.bat. Can anyone please help on this?
Try CMD /c d:\Oracle\setenv.bat /k BuildApp.

issue commands to window/process started from batch file

I want to pass commands to a process started in a batch file, specifically Cygwin. For instance, if I start Cygwin with something like the following:
start "window1" cmd.exe /c cygwin
How might I execute 'ls' in "window1", in the same batch file from which I started Cygwin?
I have to ask why you want to run the commands from a batch file? Do you want to take different actions based on the results/output of the commands?
In either case, what you are asking is bordering on impossible. Why don't you simply write your logic in a bash script and run
start "window1" cmd.exe /c c:\cygwin\bin\bash.exe -c [script]
where [script] is the path of your bash script.
Documentation on bash scripting is available at http://tldp.org/LDP/abs/html/
If you don't have cmd arg to cygwin to execute command in startup (like cmd.exe /K or /C) you can automate such thing with some script like AutoHotKey.

Resources