Per Erik Strandberg /cv /kurser /blog

The code

I made a small program in C# that prints files and folders in the present working directory to test standard out and error out. To avoid printing all the files in my temp-folder I also added a wildcard-function - the first argument is used to match files and folder. Crude but it works.

using System;
using System.Collections.Generic;
using System.Text;

namespace myDir
{
  class Program
  {
    static void Main(string[] args)
    {
      string pattern = "*";
      
      try
      {
        pattern = args[0];
      }
      catch(Exception)
      {
        pattern = "*";
      }
      
      string cwd = System.IO.Directory.GetCurrentDirectory();

      string[] files = System.IO.Directory.GetFiles(cwd, pattern, System.IO.SearchOption.TopDirectoryOnly);
      string[] dirs = System.IO.Directory.GetDirectories(cwd, pattern, System.IO.SearchOption.TopDirectoryOnly);

      Console.Error.WriteLine("Folders");
      foreach (string folder in dirs)
        Console.WriteLine(folder);

      Console.Error.WriteLine("Files");
      foreach (string file in files)
        Console.WriteLine(file);
    }
  }
}

Regular usage

Like expected it prints "Folders" followed by all folders, then "Files" followed by all files.

C:\temp>mydir *file*
Folders
C:\temp\c_file
C:\temp\makefiletest
Files
C:\temp\cf-files.txet
C:\temp\cfiles.txt
C:\temp\cs-files.txet
C:\temp\c_files.txt
C:\temp\this_file_just_created.txt

"Advanced" usage

Now take a look at what happens if we want the result in a file:

C:\temp>mydir *file* > files_and_folders_called_file.txt
Folders
Files

Aha! Console.Error ends up on the console - I bet the rest ends up in the file...

C:\temp>dog files_and_folders_called_file.txt
C:\temp\c_file
C:\temp\makefiletest
C:\temp\cf-files.txet
C:\temp\cfiles.txt
C:\temp\cs-files.txet
C:\temp\c_files.txt
C:\temp\files_and_folders_called_file.txt
C:\temp\this_file_just_created.txt

Yup it did.

"Even more advanced" usage

If we want to get the output written in error out (Console.Error) we can use 2> like this:

C:\temp>mydir *file* > files_and_folders_called_file.txt 2>error.txt

Now error out is directed to a the file error.txt, and standard out to files_and_folders_called_file.txt.

C:\temp>dog files_and_folders_called_file.txt
C:\temp\c_file
C:\temp\makefiletest
C:\temp\cf-files.txet
C:\temp\cfiles.txt
C:\temp\cs-files.txet
C:\temp\c_files.txt
C:\temp\files_and_folders_called_file.txt
C:\temp\this_file_just_created.txt

C:\temp>dog error.txt
Folders
Files

Direct standarderror to standardout

It might seem a little tricky at first, but quite logical.

First we do the redirect:

C:\temp>mydir "*file*" > apafil2.txt 2>&1

Let's explain what happens here. > apafil2.txt directs to the file apafil2.txt, but also 2>&1 directs error-out to standard-out.

C:\temp>dog apafil2.txt
Folders
C:\temp\c_file
C:\temp\makefiletest
Files
C:\temp\cf-files.txet
C:\temp\cfiles.txt
C:\temp\cs-files.txet
C:\temp\c_files.txt
C:\temp\files_and_folders_called_file.txt
C:\temp\this_file_just_created.txt

And all of this in good old fashion command line.

Other Usage

Std.Error can also be useful for debugging purposes. Suppose you are developing program. In this developing phase you print lots and lots or debug-text.

My hint is to also make the critical and most important debug-prints on Std.Error. When you run the program as you notice no difference if you print or Std.Error or Std.Out, but when you want to run a series of tests and the debug-text is a little overwhelming you can direct the non-critical text to a crapfile. Like this:

>myDebugProgram > crap.txt
Prog >> critical message: 'Bobajojsosmomanonnonenon'
Prog >> critical message: 'opa!'

Neat info instead of screen after screen of junk. (As of today: I use it for work).


This page belongs in Kategori Programmering.