Displaying Process Percentage on DOS Screen

I am using 7zip in command line mode.

When the operation takes a long time, the percentage of the process is sometimes displayed.

I wonder if we want to do this with C # / Java, which library to use?

Hello,

+2


a source to share


3 answers


You can type "carriage return", also known as '\r'

to reset "cursor" to the beginning of the line.

System.out.printf("Progress: %3d %% \r", percentComplete);

      



Now every time you print a string, you go back to the beginning, so the next percentage overwrites the previous one.

+5


a source


no library. You just print System.out.print (not println!) To the console, then send back characters to clear the line.

System.out("Progress 5");
System.out("\b\b\b\b\b\b\b\b\b\b");
System.out("Progress 10");

      



I haven't done this in awhile, but this should do it.

+3


a source


You can clear the console of bumps ... oh and don't forget that string.format has percentage support ...

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
          var perc = 0.0;
          while(perc <= 1.0)
          {

            Threading.Thread.Sleep(50); //simulate doing some work

            //EDIT:
            //Console.Clear();
            Console.Write(String.Format("{0:P}\r", perc)); //as per suggestion

            perc += 0.01;
          }
          Console.WriteLine("Press any key to exit");
          var exit = Console.ReadKey();
      }

  }

      

}

0


a source







All Articles