101

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.

Guy Avraham
  • 2,830
  • 2
  • 31
  • 43
patel
  • 1,273
  • 4
  • 13
  • 15

8 Answers8

119

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
Blorgbeard
  • 93,378
  • 43
  • 217
  • 263
  • 1
    thanks @Blorgbeard. it is working except 4th line. it is not opening given url. – patel May 24 '13 at 05:19
  • does the browser open at all? – Blorgbeard May 24 '13 at 05:28
  • Oh, I think I know what's happening - see my edit, I added "start" before the `iisexpress` line. – Blorgbeard May 24 '13 at 05:33
  • @Blorgbeard, I'm trying your approach with `"C:\Program Files\MongoDB\Server\3.3\bin\mongod.exe" --install --dbpath=C:\data\db --logpath=C:\data\log\log.txt --auth --service` and it doesn't work. When I execute this line in `CMD` everything is OK. In both cases, I execute it as admin. Tell me please, what I do wrong? – Mike B. Feb 14 '16 at 11:59
  • 4
    @MikeB. `start` takes the first quoted argument as window title. Try `start "" "C:\Program Files\MongoDB\Server\3.3\bin\mongod.exe" --install --dbpath=C:\data\db --logpath=C:\data\log\log.txt --auth --service` – Stephan May 03 '17 at 15:09
  • an explanation would be useful – Sapnesh Naik Jul 10 '17 at 13:23
27

I think the correct syntax is:

cmd /k "cd c:\<folder name>"
argentum47
  • 2,237
  • 1
  • 14
  • 17
17

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
Community
  • 1
  • 1
foxidrive
  • 37,659
  • 8
  • 47
  • 67
9

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"" 
Feety
  • 195
  • 1
  • 4
  • 10
7
@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
Peter O.
  • 28,965
  • 14
  • 72
  • 87
Ethan
  • 91
  • 1
  • 1
  • 3
    "How do I execute cmd commands through a batch file?" Heh, technically correct. – Vivek Chavda Dec 07 '16 at 16:36
  • 2
    Technically correct..yes...but also exactly what you need if you're at a school computer and command prompt is disabled but batch files aren't. – RetroCraft Jul 04 '17 at 16:58
  • I've been looking for this, so I'm glad you posted this even though its not what the OP wanted :) – dandeto Jul 21 '18 at 21:16
2

cmd /k cd c:\ is the right answer

frank
  • 29
  • 1
2
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.

VictorLee
  • 328
  • 2
  • 11
-9

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

  • Changing the Folder Name? You must be kidding. – Werner Henze Jan 05 '17 at 17:17
  • 1
    correct for any command that uses parameters with spaces - *except* `cd`. It's the only command I know, that actually takes parameters with spaces *without* quotes. – Stephan May 03 '17 at 15:13
  • When you change "Program files".... xD. I agree with Werner. Here's what happens: **Your apps that hardcodes the file location no longer works, some system functions may not work, you system breaks down** –  May 27 '17 at 12:50