[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Directory Browsing

Posted on 2004-04-28
7
Medium Priority
?
2,612 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 500 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
Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

649 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