How can I run batch in a shell? - batch-file

I want to have a batch file that opens the command prompt, launches the iex shell within it and then starts my elixir program. The issue I'm having is that as soon as I invoke iex -S mix, which compiles the code and opens the elixir shell, then I am unable to write more commands into it.
:: Start iex and compile with mix
iex -S mix
:: Start elevators
Elevator.Supervisor.start
pause
The last part Elevator.Supervisor.start never runs, for some reason. I guess this is because I opened a shell within the command prompt. Is there a way to feed commands into the iex?

TL;DR use .iex.exs file which is loaded by iex upon start.
Create a file named .iex.exs in the project directory root with the content you want to be run:
Elevator.Supervisor.start()
remove any reference to elixir code (which is now located in .iex.exs) from your .bat file
run .bat file
enjoy.

Related

how to run multiple command inside other terminal using batch file

i am using nvs, and there is a nvs command line using which i can change my node version.
currently i am using .bat file from Desktop and then go to nvs terminal
cd C:\nvs
start "C:\nvs\nvs.cmd"
this opens the terminal, and then i need to type more commands inside nvs terminal like
nvs use node/10.16.3/x64
cd ../code/api
node -v
code .
i need to automate this and all the executables to be part of the .bat file.
i tried using /c or /k to concatenate the commands. but the terminal closes before executing anything. how to run these commands as a part of batch file together.

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)

Starting a batch file from another batch file only opens CMD

When I start a batch file from another batch file, it just opens a new CMD window named just "TEST.bat", and doesn't run the actual batch. Running it manually works fine.
cd %~dp0\Colours\TEST.bat
start "TEST.bat"
I have tried many different ways to run the batch, but it all does the same thing. I've also tried to run the batch as administrator but same result again.
Full code(not finished): http://pastebin.com/GE8yJP0J
To run another batch file, use call not start. Also: cd expects a directory, not a filename.
cd "%~dp0\Colours"
call TEST.bat

How to run several batch files without open shell windows?

I've got some .bat files with start xxx and I need to run them all in another file
so if do start xxx.bat there I will get running xxx and command line shell. I don't need another shell so how to run batch file without opening another shell? or close it's shell after executing.
If I understand correctly, you just want to use call instead of start.
Example:
call xxx.bat

Logging into Cygwin and executing commands using bat file in windows

I am using windows XP operating system and cygwin is installed in my C drive.
I need to login to cygwin directly to my directory path which contains a makefile and also a bash script called build.sh in the same directory. So i modified the original cygwin.bat file and added the line as shown below.
#echo off
C:
chdir C:\cygwin\bin
bash --login "/cygdrive/E/scheme_31july/build/build.sh"
When i double click on this bat file i could see my script executing but not on cygwin shell but on windows cmd shell as a result I get errors for "make" command like "No rule to make target" as make comes bundled with cygwin.
And when I explicitly login to cygwin using default cygwin.bat file and execute my script by giving following commands in cygwin shell the script executes without errors.
Basically I want to write a bat file so that I can keep it anywhere in my PC and instead of manually openeing the cygwin prompt and typing commands like:
$ cd /cygdrive/E/scheme_31july/build/
$ sh build.sh
it should happen automatically. I sit possible to do so.
Regards,
Harshit
No rule to make target sounds more like make being executed in the wrong directory. make itself seems to be available and running as intended.
Try this:
bash --login -c "cd /cygdrive/E/scheme_31july/build/ && sh build.sh"
This should start a --login session (which should give you access to all the settings and tools you'd expect in a cygwin prompt environment), then execute the given shell command, which is the cd and sh you asked for. You could also write those two lines to a separate script file, and pass the name of that to bash instead of the full path to build.sh.
You could also try to cd into C:\scheme_31july\build in the bat file and then execute bash from there. Not sure whether bash will try to change path upon entering the login session. You can try whether things work without the --login, both for this approach and the one above.
#echo off
C:
cd C:\scheme_31july\build
C:\cygwin\bin\bash.exe ./build.sh
I'm not sure whether you want the session to turn interactive after that or not. In the above case, bash will terminate after the script completed, and might even close the window. You might have to add a read into build.sh to avoid that. If you want bash to turn interactive after executing some command, you can try using the --rcfile option of bash to execute some commands and then turn interactive.

Resources