Solved

Directory Browsing

Posted on 2004-04-28
7
2,608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 450
Create a progress bar for timer 4 300
vc++ 6.0  Gui not loading 12 113
a reliable reference for c+11? 3 83
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…

730 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