0

How to implement this pseudo code in a C# Windows console app?

for i=1 to 100
    rename filei newFilei

The key goal is to loop and execute any cmd command within a Windows console app.

class Program
{
    static void Main(string[] args)
    {
        string strCmdLine;
        System.Diagnostics.Process process1;
        process1 = new System.Diagnostics.Process();


        Int16 n = Convert.ToInt16(args[1]);
        int i;
        for (i = 1; i < n; i++)
        {
            strCmdLine = "/C xxxxx xxxx" + args[0] + i.ToString();
            System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
            process1.Close();
        }


    }
}
4
  • 1
    show the code you have written... Commented Jul 23, 2011 at 6:00
  • 1
    Does this even compile? This looks like a gross translation from Java Commented Jul 23, 2011 at 6:57
  • You may consider changing your question to Can I repeatedly execute commands on windows command prompt "DOC" using C#?. Commented Jul 23, 2011 at 7:32
  • Please stop prefacing your questions with tags. Commented Jul 25, 2011 at 14:17

3 Answers 3

3

Diagnostics.Process

            System.Diagnostics.Process proc;

            cmd = @"strCmdLine = "/C xxxxx xxxx" + args[0] + i.ToString();

            proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "cmd";
            proc.StartInfo.Arguments = cmd;
            proc.Start();
Sign up to request clarification or add additional context in comments.

Comments

1

C# supports four different loop constructs:

  1. for
  2. foreach
  3. while
  4. do

The documentation for each of these is sufficiently detailed that I won't explain these again here.

File related operations can be performed with the File and Directory classes respectively. For example to rename a file you could use the Move method of the File class like so.

File.Move("oldName","NewName");

Because both oldName and NewName are assumed to be in the same directory, the file named oldName is renamed to NewName.

With respect to launching other applications the Process class offers the ability to launch a process and monitor it's execution. I'll leave investigating this class and it's capabilities to the reader.

The pseudo-code included in the question could be translated to the following code in C#. Please note that this sample does not include any error handling which one will always want to include in production code.

string[] sourceFileNames=new string[100];
string[] destFileNames = new string[sourceFileNames.Length];
//fill arrays with file names.
for (int i=0; i < fileNames.Length; i++)
{
File.Move(sourceFileNames[i], destFileNames[i]);
}

Comments

0

If all you're doing is renaming files you can do that by using the System.IO.File class with the Move method

http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx

1 Comment

No, renaming is just an example. I have added a portion of code that I am working on to the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.