How do I execute cmd commands through a batch file?

Batch FileCmdWindows Scripting

Batch File Problem Overview


I want to write a batch file that will do following things in given order:

  1. Open cmd
  2. Run cmd command cd c:\Program files\IIS Express
  3. Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
  4. Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx

Note: The cmd window should not be closed after executing the commands.

I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.

Batch File Solutions


Solution 1 - Batch File

So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.

Try something like this for a start:

c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause

Solution 2 - Batch File

I think the correct syntax is:

cmd /k "cd c:\<folder name>"

Solution 3 - Batch File

This fixes some issues with Blorgbeard's answer (but is untested):

@echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause

Solution 4 - Batch File

cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.

cmd /c ""path to executable""

and if your executable needs a file input with a space in the path a another set

cmd /c ""path to executable" -f "path to file"" 

Solution 5 - Batch File

@echo off
title Command Executer
color 1b

echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute

Solution 6 - Batch File

start cmd /k "your cmd command1"
start cmd /k "your cmd command2"

It works in Windows server2012 while I use these command in one batch file.

Solution 7 - Batch File

cmd /k cd c:
is the right answer

Solution 8 - Batch File

I was trying to run a couple of batch files parallely at startup, if a condition was true.

For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.

I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves

The thing which worked for me was by using a combination of START and CALL

As an example

condition ...

start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"

start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"

start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"

Solution 9 - Batch File

I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with

cd c:\Program files\IIS Express

and it's trying to go to c:\Program in stead of C:"Program Files"

Change the folder name and *.exe name. Hope this helps

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpatelView Question on Stackoverflow
Solution 1 - Batch FileBlorgbeardView Answer on Stackoverflow
Solution 2 - Batch Fileargentum47View Answer on Stackoverflow
Solution 3 - Batch FilefoxidriveView Answer on Stackoverflow
Solution 4 - Batch FileFeetyView Answer on Stackoverflow
Solution 5 - Batch FileEthanView Answer on Stackoverflow
Solution 6 - Batch FileVictor LeeView Answer on Stackoverflow
Solution 7 - Batch FilefrankView Answer on Stackoverflow
Solution 8 - Batch FileAnshu Kumar GuptaView Answer on Stackoverflow
Solution 9 - Batch Fileuser7380371View Answer on Stackoverflow