0

So I have a set of files that I am trying to get a word count on. I am trying to enter a file path in command line and then run an executable on them to see the word count in the command line.

Code for word count that I have:

string[] words = File.ReadAllText(@"path"/*I want the path here to be read from what I enter in the command line.*/).Split(' ');

Then to find the word count:

int wordcount= words.length-1;

I then want wordcount to be returned to me in the commandline. So, to reiterate, I need to be able to enter a file into the command line, run the word counting exe on it, and return the wordcount number to the commandline. This is a homework assignment for me, so if possible it would be great if you could refer me to a place to understand how to do this if you answer. Thanks in advance to anyone that tries to help.

  • What specifically do you not know how to do? Do you know how to read in from the console, do you know how to write out to the console? – Scott Chamberlain Jun 22 '16 at 18:57
  • Use either `Environment.GetCommandLineArgs` or the `args` parameter to your main method to get arguments from the command line. – D Stanley Jun 22 '16 at 18:59
  • @Scott both. I can open them command prompt and run a exe, but I don't know how to read and write to the command prompt. – HandsomeJack Jun 22 '16 at 19:02
  • Are you expecting to do `yourprogram.exe countThisFile.txt` as a single line on the command line or do you expect to do `yourprogram.exe` then once the program starts do `countThisFile.txt` from inside the program? – Scott Chamberlain Jun 22 '16 at 19:03
  • @scot single line on the command line if I can. I want to enter the text file path, then enter the exe, and get the word count back to me in the command line. – HandsomeJack Jun 22 '16 at 19:10
  • Commands on the command line don't work like that, you have to enter the exe then enter the text file path – Scott Chamberlain Jun 22 '16 at 19:17
  • @scott So I could do something like: – HandsomeJack Jun 22 '16 at 19:18

2 Answers2

0

You can read and write to the console like this :

var fileName = Console.ReadLine()

...

Console.WriteLine(wordcount);

also please note , fileName should be full path to the locaiton file

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
  • 1
    The line *"I am trying to enter a file path in command line and then run an executable on them..."* makes me believe `Console.ReadLine()` is *not* what the OP wants, he wants command line args. – Scott Chamberlain Jun 22 '16 at 19:01
0

Use Console.ReadLine to do this. There are a variety of other things one can do with the Console class, including writing lines and reading keys.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
  • The line *"I am trying to enter a file path in command line and then run an executable on them..."* makes me believe this is *not* what the OP wants, he wants command line args. – Scott Chamberlain Jun 22 '16 at 19:00
  • @ScottChamberlain I interpret this same phrase as not expecting a command line argument, but expecting interactive input on the console, *especially* since this is a homework assignment and most will be running directly from Visual Studio with no arguments. – Alyssa Haroldsen Jun 22 '16 at 19:03