Creating and Deleting Directories

< BACK  NEXT >
[oR]

Typically a user creates a directory with the Explorer. There are many reasons why you might need to do the same thing inside of an application. For example, if you are writing an application that installs another application or a set of data files, you will need to create directories to hold the files that you are installing. Listing 3.2 uses the CreateDirectory function to create a new directory.

Listing 3.2 Creates the specified directory
 void Listing3_2() {   TCHAR szPath[MAX_PATH + 1];   if(!GetTextResponse(_T("Enter Directory to Create:"),         szPath, MAX_PATH))      return;   if(!CreateDirectory(szPath, 0))      cout   _T("Could not create directory: ")             GetLastError(); } 

Table 3.2. CreateDirectory Creates a new directory
CreateDirectory
LPTSTR dirName Name/path of the directory to create
LPSECURITY_ATTRIBUTES security Security attributes (not supported, use NULL)
BOOL Return Value Returns TRUE on success, otherwise FALSE

The dirName parameter accepts either a name or a path. If it receives just a name, it forms the new directory as a child of the root directory in the Object Store. If it receives a path (for example, "\mydir\temp\new"), it traverses the path ("\mydir\temp") and creates the new directory ("new") there. If the path is invalid, it fails. The GetLastError function contains a detailed error code following any failure.

It is just as easy to delete a directory using the RemoveDirectory function, as shown in Listing 3.3.

Listing 3.3 Deletes the specified directory
 void Listing3_3() {   TCHAR szPath[MAX_PATH + 1];   if(!GetTextResponse(_T("Enter Directory to Remove:"),          szPath, MAX_PATH))       return;   if(!RemoveDirectory(szPath))       cout    _T("Could not remove directory: ")               GetLastError(); } 

Table 3.3. RemoveDirectory Removes an empty directory
RemoveDirectory
LPTSTR dirName Name/path of the directory to remove
BOOL Return Value Returns TRUE on success, or FALSE on failure

The RemoveDirectory function can remove a directory only if it is empty. It accepts the same name and/or path information described for CreateDirectory above.

Traversing Directory Trees

The Windows CE API provides a set of three functions that let you easily traverse a directory. Using these same functions recursively you can traverse entire directory trees. Listing 3.4 demonstrates the use of the directory walking functions in their simplest form. This code lists all the file and directory names found in a single directory.

Listing 3.4 Lists directory contents
 void PrintFindData(WIN32_FIND_DATA *fdData) {   // Directory and temporary means removable media   if ((fdData->dwFileAttributes           & FILE_ATTRIBUTE_TEMPORARY)           && (fdData->dwFileAttributes           & FILE_ATTRIBUTE_DIRECTORY) )   {      cout    _T("Removable Media: ")              fdData->cFileName   endl;   }   // If it's a directory, print the name   else if(fdData->dwFileAttributes           & FILE_ATTRIBUTE_DIRECTORY)   {      cout    _T("Directory: ")              fdData->cFileName   endl;   }   else  // it's a file, print name and size   {      cout   fdData->cFileName;      cout   tab   _T("(")             fdData->nFileSizeLow   _T(")")   endl;   } } void ListDirectoryContents(LPTSTR lpFileMask) {   HANDLE hFindFile;   WIN32_FIND_DATA fdData;   // get first file   hFindFile = FindFirstFile(lpFileMask, &fdData);   if(hFindFile != INVALID_HANDLE_VALUE)   {      PrintFindData(&fdData);      while(FindNextFile(hFindFile, &fdData))      {        PrintFindData(&fdData);      }      FindClose(hFindFile);   }   else      cout   _T("Call to FindFirstFile failed: ")             GetLastError(); } void Listing3_4() {   ListDirectoryContents(_T("\\*.*")); } 

In Listing 3.4, the ListDirectoryContents function starts by calling the API's FindFirstFile function.

Table 3.4. FindFirstFile Finds the specified file in the current directory
FindFirstFile
LPTSTR searchFile The file to search for (wild cards are OK)
LPWIN32_FIND_DATA findData Information about the file it finds
HANDLE Return Value Returns a search handle to the first matching file found, or INVALID_HANDLE_VALUE on failure

The FindFirstFile function accepts the name of the file to find and returns a HANDLE to the file if it is found, as well as a structure describing the file. The file handle is not a normal file handle like the ones produced by CreateFile (see Chapter 2). It is specific to the Find functions described in this section. The WIN32_FIND_DATA structure returns the following information:

 typedef struct _WIN32_FIND_DATA {    DWORD dwFileAttributes;    FILETIME ftCreationTime;    FILETIME ftLastAccessTime;    FILETIME ftLastWriteTime;    DWORD    nFileSizeHigh;    DWORD    nFileSizeLow;    DWORD    dwReserved0;    DWORD    dwReserved1;    TCHAR   cFileName[ MAX_PATH ];    TCHAR   cAlternateFileName[ 14 ]; } WIN32_FIND_DATA; 

A great deal of this information duplicates the information returned by the GetFileInformationByHandle function (see Chapter 2), as well as the fully qualified file name. Windows CE does not use the 8.3 DOS file notation, so cAlternateFilename is not used.

You can pass to the FindFirstFile function a specific file name, a file name containing wild cards, or a path with or without a file name. If it finds a file that matches the file name you have passed, it returns the handle and information about the file. If it cannot find the file, it returns INVALID_FILE_ HANDLE for the handle.

In Listing 3.4, the program is searching for every file in the root directory. It passes the structure returned by FindFileFirst to PrintFindData, which decides whether or not it is a directory name and prints out some of the information. The program then continues looking for other files in the directory using the FindNextFile function.

Storage devices are represented as directories in the Object Store. Such directories have the attributes "directory" and "temporary." PrintFindData uses these attributes to determine if a directory represents a storage device.

Table 3.5. FindNextFile Finds the next file following a FindFileFirst
FindNextFile
HANDLE findFile File handle returned by FindFileFirst
LPWIN32_FIND_DATA finData Information about the file it finds
BOOL Return Value Returns TRUE on success, otherwise FALSE

FindNextFile accepts a handle produced by either FindFirstFile or a previous call to FindNextFile. It finds the next file in the directory that matches the file name description first passed to FindFirstFile. If no match is found, the returned Boolean value will be false, and the GetLastError function will contain the error code. Once no match is found, it means that the code has reached the end of the directory. At this point, the program callsFindClose to clean up the file handle used by the previous Find functions.

Table 3.6. FindClose Closes the search handle
FindClose
HANDLE File handle returned by FindFileFirst
BOOL Return Value Returns TRUE on success, otherwise FALSE


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net