Solved

Directory Browsing

Posted on 2004-04-28
7
2,603 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cchar * Concating 7 91
C# - Function parentheses 11 53
auto dialer software c# 1 67
Visual Studio Community 2015 updates failed 5 84
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now