UserGuide - Displaying text output (Console)

In the previous topic Input & Output you already saw an overview about the different possibilities to output text to the user, and in the topic Storing Data in Memory, we started to build a small application to display the properties of files in a particular folder to the debug window.

Now we're going to revisit this example to improve the data output section to resolve some issues with using the debug window. Firstly, this window is only available in the PureBasic IDE which means its only useful to programmers, secondly it doesn't really give us much control over how our output looks.

PureBasic provides a text mode window, or console window, which can be used in compiled programs. So let's update our example to use it instead.

First, we will need some extra working variables to make this work properly. Amend the variable definitions like this:
  ; Now we define a new list of files using the structure previously specified 
  NewList Files.FILEITEM()
  Define.s Access, Attrib, Create, Folder, Modify, Msg, Num, Size
  Define.l Result
Next, remove the output section of code completely, from the comment line:
  ; If there are some entries in the list, show the results in the debug window.
Now replace this with:
  ; Open a text mode screen to show the results.
  ; Display a title.
  ; PrintN displays the string given in the console window and moves the print position to the start of the next line afterwards.
  ; Space(n) returns n spaces in a string.
  PrintN("File list of " + Folder + ".")
  Msg = "Num Name"
  Msg = Space(4) + "Create" + Space(5) + "Access" + Space(5) + "Modify" + Space(5) + "Attrib Size"
  ; Loop through the list to display the results.
  ForEach Files()
    ; Tabulate the number of the list index.
    ; ListIndex() returns the current position in the list, counting from zero.
    ; StrU converts an unsigned number into a string.
    ; RSet pads a string to a set length with the necessary number of a specified character at the front.
    ; Here we use it to make sure all the index numbers are padded to 3 characters in length.
    Num = RSet(StrU(ListIndex(Files()) + 1), 3, " ")
    ; Display the item number and file name.
    Msg = Num + " " + Files()\Name
    ; These lines convert the three date values to something more familiar.
    Create = FormatDate("%dd/%mm/%yyyy", Files()\DateCreated)
    Access = FormatDate("%dd/%mm/%yyyy", Files()\DateAccessed)
    Modify = FormatDate("%dd/%mm/%yyyy", Files()\DateModified)
    ; Convert the file size to a padded string the same as with the index value above,
    ; but allow space for the maximum size of a quad.
    Size = RSet(StrU(Files()\Size), 19)
    ; Convert the attributes to a string, for now.
    Attrib = RSet(StrU(Files()\Attributes), 6, " ")
    ; Display the file's properties.
    Msg = Space(4) + Create + " " + Access + " " + Modify + " " + Attrib + " " + Size
    ; Display a blank line.
  Next Files()
  ; Wait for the return key to be displayed, so the results can be viewed before the screen closes.
  PrintN("Press return to exit")
All being well the output should appear in a console window looking something like this:
  File List of C:\Documents And Settings\user\.
  Num Name
      Create     Access     Modify     Attrib Size
      03/07/2008 04/04/2011 02/04/2011     34            18874368
    2 kunzip.dll
      14/07/2008 04/04/2011 14/07/2008     32               18432
    3 ntuser.dat.LOG
      03/07/2008 04/04/2011 04/04/2011     34                1024
    4 ntuser.ini
      03/07/2008 02/04/2011 02/04/2011      6                 278
  Press Return To exit
This output is from a Windows XP system, later versions of Windows and Linux or Mac OSX will show different files of course.
Note for Linux/MacOS: Please note, to select "Console" as executable format in the compiler options.

UserGuide Navigation

< Previous: Input & Output | Overview | Next: Building a graphical user interface (GUI) >