-1

I'm working on a little fun school project.

I want to create a windows form application, which runs commands in cmd by opening batch-file. I do not have much experience in using batch-files.

I need help with the following:

  1. Batch file which opens cmd
  2. Runs a command ONCE only, such as ipconfig, ping, tracert etc.
  3. Keeps cmd open with the information

Otherwise good guides for working and learning batch-files are much appreciated.

zionik
  • 3
  • 1
  • 2
  • 2
    Welcome to Stack Overflow! Please take a tour [How do I ask a good question?](//stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](//stackoverflow.com/help/mcve). Remember we are not a free coding service ;) – Olaia Apr 06 '17 at 08:13
  • 2
    To get helped, you shoud prove some effort from you. [Edit your question](http://stackoverflow.com/posts/43249355/edit) and add what did you tried until now as code ! – Hackoo Apr 06 '17 at 08:40

1 Answers1

0

I'll give you some hints:

  1. Batch files open with cmd by default
  2. Commands will only run once by default
  3. To keep the window open after running the command, use the /k switch i.e. cmd /k ipconfig

EDIT:

Make this the contents of your batch file:

@echo off
ipconfig

Then call the batch file like this (from command line/run etc):

cmd /k iptest.bat

Or from Winform (C#):

Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k iptest.bat"
p.Start();
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Thank you sir! So far i have found out how to create functions and create a console program by using a batch file. But i still have problems. The problem is that i can make my batch-file enter the command in cmd but it does not execute it. – zionik Apr 06 '17 at 08:56
  • Right now i got it like: @echo off cmd /k ipconfig pause This seems to just write ipconfig infinite time on the name of the cmd window. I tried cmd /k ipconfig with no pause after, this leaves me with an cmd with nothing in it. Is a better solution to cd to system32 and run the ipconfig.exe? – zionik Apr 06 '17 at 09:11
  • You need to call your batch file with `cmd /k`, rather than in the file itself. So in your batch just use `ipconfig`, but from your winform call the process `cmd /k yourbatch.bat`. – Bali C Apr 06 '17 at 09:13
  • It might be worth mentioning that i'm using win10 and testing in a virtual enviroment, to avoid having to restart my pc(learn by doing) :D. – zionik Apr 06 '17 at 09:14
  • That's fine, should work just the same, best way of learning! :) – Bali C Apr 06 '17 at 09:16
  • The windows forms is not made yet i want the batch-file to work first. I tried just to run the batch-file containing ipconfig from cmd, this is what i get: https://i.imgur.com/2aFRjfE.png The command still does not execute it is only written into cmd and displayed. – zionik Apr 06 '17 at 09:38
  • I've updated my answer, just try running the batch from the run prompt rather than a cmd window first to see if it works? – Bali C Apr 06 '17 at 10:00
  • Seems like the problem was, that i was not running the batch file as admin haha. Thanks for your help, now i need to figure out how to run the batch-file as admin. – zionik Apr 06 '17 at 10:49
  • Ah right, I wouldn't have guessed that. That should be quite easy if you are doing through the winform, look here http://stackoverflow.com/questions/4624113/start-a-net-process-as-a-different-user – Bali C Apr 06 '17 at 11:12
  • I have been trying to get it to work for 2 days now on/off, so i feel pretty stupid. But thanks for the link, you have been a great help! – zionik Apr 06 '17 at 12:53
  • Don't worry, I've spent longer on things :) No worries, good luck! – Bali C Apr 06 '17 at 13:04