Home

Microsoft Visual C++/MFC File Processing: Directories

        

Introduction to Directories

 

Description

 

A directory is a section of a medium (floppy disc, flash drive, hard drive, CD, DVD, etc) used to delimit a group of files. Because it appears like a "physical" area, it can handle operations not available on a drive and there are differences between both:

  • A drive can contain one or more directories. A directory cannot contain drives (this is some type of exception: a user can create different directories on a drive in computer A, then map each of these directories in another computer. These mapped drives would appear as different drives in computer B, giving the illusion that the directory contain many drives)
  • A directory can contain other directories. A drive cannot really contain other drives
  • A (sub) directory can be moved from one directory to another. This operation is not possible vice-versa since a drive cannot contain another drive
  • A user can easily delete or remove a directory (if she has the right permissions). The equivalent operation for a drive is rather handled by the operating system

The similarities of both types are:

  • A computer cannot have two drives with the same drive letter (name). In a drive and at the same level, a directory cannot have two (sub) directories with the same name. That is, two directories cannot have the same name inside of the same parent directory
  • A drive or a directory can be copied (this operation, although possible, is rather complex for a drive; for example, you can copy a whole hard drive from the computer to an external backup hard drive, but some things, for example from the registry, will not be copied)

Directory Creation

To create a directory, you can call the CreateDirectory() method of the Win32 library. Its syntax is:

BOOL WINAPI CreateDirectory(
  __in      LPCTSTR lpPathName,
  __in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes);

When calling this method, pass the complete path as a string. Here is an example of calling this method:

void CExerciseDlg::OnBnClickedCreatedirectoryBtn()
{
    CreateDirectory(L"C:\\Exercise1", NULL);
}

When the CreateDirectory() function is called:

  1. It first checks the drive. If the drive doesn't exist, nothing would happen
  2. If the drive exists, the compiler checks if the directory exists:
    1. If the directory doesn't exist, if would be created
    2. If the directory exists already, nothing would happen

The CreateDirectory() function returns a Boolean value. If the function created the directory succcessfully, it returns TRUE. If it fails, it returns FALSE.

The second argument specifies the permissions to apply when the function is called. The permissions are controlled through a SECURITY_ATTRIBUTES object. The SECURITY_ATTRIBUTES structure is defined as follows:

typedef struct _SECURITY_ATTRIBUTES
{
    DWORD nLength;
    LPVOID lpSecurityDescriptor;
    BOOL bInheritHandle;
} SECURITY_ATTRIBUTES,  *PSECURITY_ATTRIBUTES;

Here is an example of using it:

void CExerciseDlg::OnBnClickedCreatedirectoryBtn()
{
    SECURITY_ATTRIBUTES saPermissions;

    saPermissions.nLength = sizeof( SECURITY_ATTRIBUTES);
    saPermissions.lpSecurityDescriptor = NULL;
    saPermissions.bInheritHandle = TRUE;

    if( CreateDirectory(L"C:\\Exercise1\\Exercise2", &saPermissions) == TRUE )
		AfxMessageBox(L"The directory was created.");
}
 
 
 

Operations on Directories

 

Deleting a Directory

To get rid of a directory, you can call the RemoveDirectory() function of the Win32 library. Its syntax is:

BOOL WINAPI RemoveDirectory(__in  LPCTSTR lpPathName);

When calling this function, pass the complete path as argument. If the path exists, the function would delete it. Here is an example:

void CExerciseDlg::OnBnClickedDeleteDirectoryBtn()
{
	if( RemoveDirectory(L"C:\\Exercise1\\Exercise2") == TRUE )
		AfxMessageBox(L"The directory has been  deleted");
}

An alternative is to call the DeleteFile() function of the Win32 library.

Renaming a Directory

To rename a directory, you can call the MoveFile() function of the win32 library. Its syntax is:

BOOL WINAPI MoveFile(__in  LPCTSTR lpExistingFileName,
                     __in  LPCTSTR lpNewFileName);

The first argument is the name and path of the directory you want to rename. To rename a directory, provide the same first part for the second argument. Change only the last that involves the actual directory to rename. Here is an example:

void CExerciseDlg::OnBnClickedRenameDirectoryBtn()
{
	if( MoveFile(L"C:\\Exercise\\Exercise1",
		     L"C:\\Exercise\\Exercise2") == TRUE )
		AfxMessageBox(L"The directory has been renamed");
}

Moving a Directory

If you want to move a directory, you can also call the same MoveFile() function. Pass the second argument as the complete and path for the new location:

  • If you want to move a sub-directory from one parent directory to another parent directory, in the second argument, change only the directory (or directories) before the name of the sub-directory and keep the same name for the sub-directory
void CExerciseDlg::OnBnClickedMoveDirectoryBtn()
{
	if( MoveFile(L"C:\\Exercise1\\Exercise2",
		     L"C:\\Exercise2\\Exercise2") == TRUE )
		AfxMessageBox(L"The directory has been moved");
}
  • If you want to move a sub-directory from a parent directory to another but with a new name at the new location, use a different path in both arguments
 
 
   
 

Home Copyright © 2010-2016, FunctionX