directory


The os_directory class provides a portable abstraction of common file directory operations. A directory can be created, destroyed, and renamed, and a directory's contents can be searched and manipulated with the standards-compatible class interface.

To create or open a directory, use either the os_directory class' constructor interface or a specialized method. The open() method opens existing directories for manipulation, while the create() method creates new directories. Directories created using the constructor interface are automatically opened, which immediately enables the os_directory object to manipulate the directory.

The following example uses the os_directory constructor to:

Note that a directory can only be removed after its files are removed.

Example <ospace/file/examples/dir1.cpp>
#include <iostream>
#include <ospace/file.h>

void
main()
  {
  os_file_toolkit initialize;

  // Create a new directory.
  os_directory dir( "newdir1", os_open_control::create_new );

  // Create a file in the new directory.
  os_file file( "newdir1/newfile.txt", os_open_control::create_new );
  file.close();

  // List the new directory contents.
  cout << "newdir1 contains the following entries:" << endl;
  vector< string > names = dir.filenames();
  vector< string >::iterator i;
  for ( i = names.begin(); i != names.end(); i++ )
    cout << "  " << *i << endl;

  cout << "newdir1 contains newfile.txt = ";
  cout << dir.contains( "newfile.txt" ) << endl;

  // Remove the new file and directory.
  os_file_system::remove( "newdir1/newfile.txt" );
  os_file_system::remove( "newdir1" );
  }

newdir1 contains the following entries:
  .
  ..
  newfile.txt
newdir1 contains newfile.txt = 1

The following example uses the static class interface to create and manipulate a new directory. These static methods are equivalent to those found in the os_file_system class.

Example <ospace/file/examples/dir2.cpp>
#include <iostream>
#include <ospace/file.h>

void
main()
  {
  os_file_toolkit initialize;

 
  // Create a directory using the static function.
  os_directory::create( "newdir2" );
  cout << "newdir2 exists = ";
  cout << os_directory::exists( "newdir2" ) << endl;


  // Rename the directory.
  os_directory::rename( "newdir2", "newdir2.old" );
  cout << "newdir2.old exists = ";
  cout << os_directory::exists( "newdir2.old" ) << endl;

  // Remove the directory.
  os_directory::remove( "newdir2.old" );
  cout << "newdir2.old exists = ";
  cout << os_directory::exists( "newdir2.old" ) << endl;

#if defined OS_WIN32

  // create directory ?-?, \u2f7a-\u2f81
  // it is \xe2\xbd\xba-\xe2\xbe\x81 in utf-8
  try
  {
    os_directory::create( "\xe2\xbd\xba-\xe2\xbe\x81", Utf8 );
 
    cout << "utf-8 directory exists = ";

    bool flag = os_directory::exists( "\xe2\xbd\xba-\xe2\xbe\x81", Utf8 );
    cout << flag << endl;

	// rename directory ?-? \u2f7a-\u2f81 into ?-? \2fc5-\u2f81
	// from \u2f7a-\u2f81 into \2fc5-\u2f81
	// in utf-8 from \xe2\xbd\xba-\xe2\xbe\x81 to \xe2\xbf\x85-\xe2\xbe\x81
	os_directory::rename( "\xe2\xbd\xba-\xe2\xbe\x81", "\xe2\xbf\x85-\xe2\xbe\x81", false, Utf8 );

	cout << "utf-8  exists = ";
	cout << os_directory::exists( "\xe2\xbf\x85-\xe2\xbe\x81", Utf8 ) << endl;

	// remove directory ?-?
	os_directory::remove( "\xe2\xbf\x85-\xe2\xbe\x81", Utf8 );
	cout << "utf-8 exists after remove = ";
	cout <<  os_directory::exists( "\xe2\xbf\x85-\xe2\xbe\x81", Utf8 ) << endl; 
	
  }
  catch(os_file_toolkit_error &error)
  {		  
	cout << "Caught exception: " << endl;
    cout << error.what() << endl;
    cout << error.native() << endl;
    cout << error.code() << endl;
  }
  cout << "done!" << endl;
#endif
 
  }

newdir2 exists = 1
newdir2.old exists = 1
newdir2.old exists = 0
utf-8 directory exists = 1
utf-8  exists = 1
utf-8 exists after remove = 0
done!


When a directory is open, you can use filenames() to return a vector of string names that represent the directory's contents. The following example shows how to display the contents of a directory using filenames() and the os_io_status class.

Example <ospace/file/examples/dir4.cpp>
#include <iostream>
#include <ospace/file.h>
#include <ospace/security.h>
#include <ospace/time.h>

void
main()
  {
  os_file_toolkit init_file;
  os_security_toolkit init_security;
  os_time_toolkit init_time;

  // Get all filenames in the directory.
  os_path path( "." );
  os_directory dir( path ); // Open directory.

  vector< string > filenames = dir.filenames();
  vector< string >::iterator i;
  for ( i = filenames.begin(); i != filenames.end(); i++ )
    {
    os_path file( path, *i );
    os_io_status status( file );
    bool is_dir = S_ISDIR( status.mode() );
    cout
      << os_time_and_date( status.modify_time() ) << "  " << '\t'
      << ( is_dir ? "<DIR>" : "     " ) << '\t'
      << os_user( status.user() ).name() << '\t'
      << os	_group( status.group() ).name() << '\t';

    if ( is_dir )
      cout << "       ";
    else
      cout << status.size();
    cout << '\t' << *i << endl;
    }
  }

10/04/96 11:18:40 <DIR>  psteph      eku              .
10/04/96 09:03:37 <DIR>  psteph      eku              ..
10/04/96 09:40:03 <DIR>  psteph      eku              CVS
10/04/96 09:46:48        psteph      eku      4146    Makefile
10/01/96 16:02:14        psteph      eku      1025    bstream1.cpp
10/01/96 17:29:49        psteph      eku      1007    dir1.cpp
10/04/96 11:08:12        psteph      eku      803     dir2.cpp
...

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.