Solved

Directory Browsing

Posted on 2004-04-28
7
2,604 Views
Last Modified: 2010-04-24
I need code to access a directory's contents. I need it to take in a string holding the relative path and return a vector of strings holding the names of the files and directories.

Function header:
vector<string> getdirectorycontents(string & path);

Example:
If the directory with the relative path "files" contains:
dir1
   filea
   fileb
dir2
   filec
dir3
filed
filee
filef

then getdirectorycontents("file") would return a vector containing the strings:
dir1
dir2
dir3
filed
filee
filef

The strings in the vector can be in any order.

Also I need code to take in a relative path and return whether it is a directory or a file.

Function header:
bool isDirectory(string & path);
0
Comment
Question by:wertyk
  • 3
  • 3
7 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 125 total points
ID: 10937674
To enumerate files and directories use: FindFirstFile, FindNextFile.
To detect directory use GetFileAttributes attiblies:

bool isDirectory(string & path)
{
    DWORD dw =  GetFileAttributes(string.c_str());

    return ( dw != 0xffffffff  &&  dw & FILE_ATTRIBUTE_DIRECTORY);
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10937692
Something is wrong with my computer or my head. Is it possible to do such typo: attiblies instead of function? :-(
0
 
LVL 4

Author Comment

by:wertyk
ID: 10943614
what all libraries do i need to include for this, alexfm?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 48

Expert Comment

by:AlexFM
ID: 10945734
#include "Windows.h"

Library: Kernel32.lib
0
 
LVL 4

Author Comment

by:wertyk
ID: 10953327
sorry, i'm totally confused. i am a complete beginner. if you can send me the complete code that would be great. i got the isDirectory to work fine. how would i do the getDirectoryContents? i need to pass it a relative path and have it return all folders and files in that top level directory only. it should not return the files within the subdirectories.
0
 

Accepted Solution

by:
_uni_ earned 125 total points
ID: 11003277
this is an example from MSDN:

The application creates the \TEXTRO directory using the CreateDirectory function.

The application searches the current directory for all .TXT files by using the FindFirstFile and FindNextFile functions. Each .TXT file is copied to the \TEXTRO directory. After a file is copied, the GetFileAttributes function determines whether the file is read only. If the file is not read only, the application changes directories to \TEXTRO and converts the copied file to read only by using the SetFileAttributes function.

After all .TXT files in the current directory have been copied, the application closes the search handle by using the FindClose function.

#include <windows.h>
#include <stdio.h>

WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
 
BOOL fFinished = FALSE;
 
// Create a new directory.
 
if (!CreateDirectory(szDirPath, NULL))
{
    printf("Couldn't create new directory.");
    return;
}
 
// Start searching for .TXT files in the current directory.
 
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
    printf("No .TXT files found.");
    return;
}
 
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
 
while (!fFinished)
{
    lstrcpy(szNewPath, szDirPath);
    lstrcat(szNewPath, FileData.cFileName);
    if (CopyFile(FileData.cFileName, szNewPath, FALSE))
    {
        dwAttrs = GetFileAttributes(FileData.cFileName);
        if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
        {
            SetFileAttributes(szNewPath,
                dwAttrs | FILE_ATTRIBUTE_READONLY);
        }
    }
    else
    {
        printf("Couldn't copy file.");
        return;
    }
 
    if (!FindNextFile(hSearch, &FileData))
    {
        if (GetLastError() == ERROR_NO_MORE_FILES)
        {
            MessageBox(hwnd, "No more .TXT files.",
                "Search completed.", MB_OK);
            fFinished = TRUE;
        }
        else
        {
            printf("Couldn't find next file.");
            return;
        }
    }
}
 
// Close the search handle.
 
FindClose(hSearch);

0
 
LVL 4

Author Comment

by:wertyk
ID: 11008789
Thanks. Those pieces of code worked.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error when running VS apps 14 121
FTP Directory Listing 2 70
Implementing a ResolveEventHandler in C++ 13 133
MS Visual Studio 2015 - Install Error 5 93
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question