How to create new commands in batch - batch-file

Just like ipconfig, when typed in batch it executes a command, so how do I make a new command, without using CALL, and can be executed anywhere, not just that path.
So just like that this code-
#echo off
color fc
echo Example
echo :--:
pause
Can be executed by this command
EXEM
If you can help, thanks.

You can write your EXEM.BAT file as normal, then convert it to EXEM.EXE with something like this bat to exe converter.
You can then put it somewhere in your %Path% and it will run when running EXEM from CMD, without terminating execution.
Good luck!

Related

Calling command with variable through batch file

I have a batch file that copies and moves stuff, but I am getting stuck dealing with Certificates. I have to use a command the vendor provides called installrootcacert.cmd, but I also need to pass the file name of the cert which is aptly named rootca.cer. I have to use the script the vendor provides so there is no way around that.
Normally I would run this from the command like like so:
c:\vendor\Software\Conf\Security\installrootcacert.cmd rootca.cer
I have attempted to call the command from my batch file, but with no luck.
I tried to use a variable, but because that command calls several other processes, it is looking for "rootca.cer" after the command. If I place it in a variable, the other processes fail. I cannot modify the other processes.
echo #off
cd E:\vendor\Software\Conf\Security\trustedCA
e:
call "e:\vendor\Software\Conf\Security\installrootcacert.cmd rootCA.cer"
A possible solution, might be:
#echo off
cd /d "E:\vendor\Software\Conf\Security\trustedCA"
call "E:\vendor\Software\Conf\Security\installrootcacert.cmd" rootCA.cer
echo #off replaced with #echo off because echo #off will echo #off in cmd.
It is mentioned by Compo in comments, you are changing drives: use /d option with cd.
Finally, quote only the filename in the call command, else, windows will interpret this as a complete filename (i.e. E:\vendor\Software\Conf\Security\installrootcacert.cmd rootCA.cer, so filename will be installrootcacert.cmd rootCA.cer).
Try the following. You need to adjust the # in your echo statement and your quotations:
#echo off
cd E:\vendor\Software\Conf\Security\trustedCA
e:
call "e:\vendor\Software\Conf\Security\installrootcacert.cmd" rootCA.cer
The reason that this works is because putting the command #echo off in your script stops ALL commands from being output. If you just have echo #off, you're literally going to echo that. (credit to double-beep for initially suggesting that)
As for the quotes, what you are trying to do is pass that as a command, so when you call the rootCA.cer, you need to make sure you are passing it the proper parameters, which is why you place that filepath in quotes. If you place the WHOLE object in quotes, you aren't actually issuing a call to the rootCA.cer command. (credit to LotPings for initially suggesting that).

How to make batch file to run exe file which requires some text

I need to run 'a.exe'.
When I start 'a.exe' file, a console pops up, and I should type "go", then the program start.
If i want to make a batch file to run this program, how should i make this.
I tried as below:
///////////
%~d0
cd %~dp0
start a.exe > "go"
pause
///////////
but "go" appears on the batch console, and the "a.exe" program still requires "go" text.
How can i solve this?
Assuming you are coding a windows .bat script, you can execute any file by just passing the file with its path and its argument on the same line a.exe go, so your script would be something like this:
#echo off
c:\path\to\file\a.exe go
PAUSE
I am turning off CMD echo so the output of a.exe is easier to be read.
There is another question here were this is further explained: Create a batch file to run an .exe with an additional parameter.
On the other hand, if you need to call another script, you would need to use the method call
#echo off
call c:\path\to\other\script\script.bat
c:\path\to\file\a.exe go
PAUSE
You can use:
start echo go| a.exe
Just type in
a.exe go
Hope it helped you out.
You can also use it to run files through certain programs
photoshop img.png

Batch File Ending Prematurely

I have a batch file with the following commands (to set up a compiler):
del Yylex.java
jflex scanner.flex
del parser.java
java -jar java-cup-11a.jar parser.cup
However, for some reason, after the conclusion of jflex scanner.flex, the batch script ends and command prompt closes. If I just run that command separately, this does not happen. Does anyone know what's wrong?
Is jflex a batch file?
If so, try
CALL jflex ...
or
start /wait "" jflex ...
(well, actually - give it a whirl anyway, can't hurt...)
When bat is asked to run another batch, it merely transfers control to that other batch and has no idea of where to return. CALL or START gives it a ticket home...

create a Batch file with a file path as an input argument

I need to create a batch file which will run a program (which has been created in C#.Net) and also take a path of a text file as an input.
Not quite sure, how to achieve this.
So far, I have the below command working,
C:\>Folder Path to executable>xxxx.exe -console
-console is my predefined command argument to run this program in console mode.
The part until running the program from the console, with -console, works perfectly fine with a hard coded file path. However, I want to give the functionality to the user to give the file path as they want and create a batch file for the same command. Everytime user can update the batch file with new text file path and simply run it.
Thanks,
Are you just wanting to use the -console parameter? Are there any other parameters you wanted to pass in?
If wanting just what you have in your snippet, Save the following into a batch file. (e.g. StartMyProgram.bat)
start "C:\Folder path to executable\xxxx.exe" -console
See start /? for help and more options.
You need to add %~1 to your script: C:\Path\to\executable\xxxx.exe -console %~1
Now you can call it like this: StartMyApp.cmd C:\Docs\readme.txt
%~1 contains the full path of the text-file. You could also ensure that only text-files are passed to your application:
if "%~x1"==".txt" (
C:\Path\to\executable\xxxx.exe -console %~1
) else (
echo Not a textfile! & pause
)

Batch Closing before end of file?

I am trying to create a batch to automate commands for something. Right now I am just running a single command and the batch closes right after it prints the output from the command. I put a PAUSE in at the end but it keeps running past it. This is probably something very simple that I am just missing.
#echo
set /p ticket="Enter ticket number: "
tkt get %ticket%
PAUSE
The tkt get %ticket% part is from a custom utility I am using. That part is definitely formatted properly because I use it through command prompt every day almost. I want to automate a lot of my normal commands to make life easier.
Is "tkt" a batch file? Try making it "call tkt" instead.
If you call a batch file from another, the first one will exit after executing the 2nd one, unless it is called with "call".
Here's an example:
Foo1.bat
foo2.bat
echo foo1
Foo2.bat
echo foo2
It seems like if you run Foo1.bat, it will spit out both "foo1" and "foo2" but it does not:
C:\temp>foo1
C:\temp>foo2.bat
C:\temp>echo foo2
foo2
To change the behavior, Foo1.bat should look like this:
foo1.bat
call foo2.bat
echo foo1

Resources