Solved

Directory Browsing

Posted on 2004-04-28
7
2,599 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
Comment Utility
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
Comment Utility
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
Comment Utility
what all libraries do i need to include for this, alexfm?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
#include "Windows.h"

Library: Kernel32.lib
0
 
LVL 4

Author Comment

by:wertyk
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks. Those pieces of code worked.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now