This must be a real dumb question. And I cant figure out how.
What I want to do is launch a new command line with some arguments and excute some commands there.
Here is what I have in my .cmd
CMD /k %EnvInstallPath% %wo% %var%
cd /d %wo%\src
When I execute test.cmd, I see that the directory changes to %wo%, but the further cd src is not executed. I need to cd to a directory and execute few other commands.
When you run cmd with /k the console runs the command and then resumes with the prompt. I'm guessing that what you want is to run the command and resume with the next one, so you need to run cmd with /c instead.
put the other commands in a different bat file and
start AFewOtherCommands.bat
Related
i'm trying to make a batch script for running my Java files. I've found out that there is no way to prevent auto-closure of a batch script(except using pause keyword, tho it just waits for key press). I've also discovered that starting a new window will cause only main windows to close, not the new one too so i want a way that the command SET /P file=Java file: is executed in the new window(I got the new window by using the start keyword. Is there any way to accomplish this without downloading other softwares? this is the code i came up with yet:
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
set /P file=Java file to execute:
java %file%^.jar
start
I guess you're looking for that :
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
start cmd /V:ON /K "#set /P "file=Java file to execute: " && java -jar !file!^.jar"
EDIT: Using expansion with /V and use of /K instead of /C to keep the cmd windows open.
Explanations : To launch a console process in another windows and keep it open after the end of this process console we launch another cmd process with the start command. We use /V:ON to use delayed expansion by default, meaning modified variables (like the one we prompt, %file%) will be expanded on the fly with ! (so !file! instead of %file%). We use /K to tell to this cmd process to not close when provided commands end. To this cmd process, we provide the following commands :
#set /P "file=Java file to execute: "
This one will ask for the jar filename (without extension) to launch.
The # means "do not echo the command itself on the console stdout" for a nice display.
java -jar %file%^.jar
This one launch the java interpreter (JVM) with the filename of a jar file to execute through the -jar parameter, filename build from the previous prompt and the .jar extension. The ^ here escapes the ., it seems not useful here but maybe your script/env requires it.
We link the both commands with && which means : _if left command from && is successful (it exits with ERRORLEVEL 0) then execute the right command from &&.
Lets say I have 2 services I want to start and a property file.
-svc1.cmd
-svc2.bat
-svc2prop.properties
Lets have directories as C:\program1\bin\ and C:\program2\bin\ + C:\program2\config\
then I tried to do it like this:
start cmd /k call C:\program1\bin\svc1.cmd
start cmd /k cd C:\program2\ .\bin\svc2.bat .\config\svc2prop.properties
I can start them both separately by opening a local CMD. The thing is, if I am in the directory C:\program2\ I can open up a local CMD and run this statement without problems " .\bin\svc2.bat .\config\svc2prop.properties"
But I want to create a batch file that: first, opens a new cmd and starts the svc1.cmd, then opens another cmd in which it goes to C:\program2\ and runs the "" .\bin\svc2.bat .\config\svc2prop.properties" " statement ... but for some reason it doesnt work.
Any possible solution ?
the second line should use /D option to start the process in the required directory:
start /D C:\program2 cmd /k .\bin\svc2.bat .\config\svc2prop.properties
(in your example, you were just passing a lot of arguments to an useless cd command)
If you have some current directory problems with the first line, just do the same:
start /D C:\program1\bin cmd /k call svc1.cmd
Imagine the following cmd
c:\myscript.cmd
java.exe myclass.class
Where the file it is running is in:
c:\temp\myclass.class
I want to run a cmd file as if it is being run in the current directory, ie my command line is open in c:\temp\
I want a shorter way of running:
c:\temp\>mv ../myscript.cmd .
c:\temp\>myscript.cmd
c:\temp\>mv myscript.cmd ..\
My question is: How do I execute a *.cmd in another dir as if it is running in the current dir?
If you just want to run it, then just type ..\myscript.cmd at the command line. It gets your current directory as its current directory. You can verify this by putting echo %CD% as the first line of the batch file. It should print c:\temp\
You can spawn a child cmd instance that then executes cd to change the working directory followed by the command to execute, this is done by running cmd directly, along with && to concatenate commands:
cmd /c "cd PATH_OF_DESIRED_WORKING_DIRECTORY" && "myscript.cmd"
Is there a way to run any command (eg: cd D:\test) on Command Prompt start and to have this instance of Command Prompt as a different shortcut/instance.
You can create a shortcut that has a target of e.g.:
%comspec% /k ""C:\Full_Path_to\VsDevCmd.bat""
Where the .bat file is the command(s) that you want to run on startup. This will then leave you with an open command prompt after those commands have executed.
(In case it's not obvious, this is just shamelessly ripped from one of the Developer Command Prompt shortcuts that Visual Studio installs in the Start Menu)
%comspec% is a nice way of getting cmd to execute.
Cmd:
/k : Carries out the command specified by string and continues.
Yes, I tried googling this, by the way.
Basically, what is the dos command that will open a dos command window?
In other words, if you have a .bat file full of script commands, what command can you do that will open a command window?
You can try this maybe. In the command line it starts it so I assume in a bat file it'll do the same.
start cmd
If you want it to run another bat or something you can do:
start script.bat
If you want it to close then:
start cmd /c script.bat