0

i have found C# code can loop through any directory we specify, i tried to convert it to C++/CLI and make it looping through all system files & folders. The problem is the code let you to determine the level of depth need to scan! What I need is to make it loops all files & folders regardless the level of depth... This is what I did ... There are some exceptions!

#include "stdafx.h"

using namespace System;
using namespace System::IO;

ref class Scan {
public:
    static int MaxPath=260000000;

    static void ScanMyDir( String^ SourceDir,int RecursiveLevel) {
        if(RecursiveLevel<=MaxPath) {
            array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
            for each (String^ fileName in fileEntries) {
                Console::WriteLine(fileName);
            }
        }
        array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
        for each (String^ subdir in SubDirEntries)
            if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
                ScanMyDir(subdir,RecursiveLevel+1);
    }
};

int main(array<System::String ^> ^args) {
    Scan::ScanMyDir("c://",1);
    Console::Read();
    return 0;
}

After some modifications (still with some errors):

using namespace System;
using namespace System::IO;

static void ScanMyDir( String^ SourceDir) {
    array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
    for each  (String^ fileName in fileEntries) {
        Console::WriteLine(fileName);
    }
    array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
    for each (String^ subdir in SubDirEntries)
        if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
            ScanMyDir(subdir);
}

int main(array<System::String ^> ^args) {
    try {
        ScanMyDir("C://Windows"); 
    }
    catch {
        Console::WriteLine("Some Denied files or Folders");
    }
    return 0;
}

These are the errors:

Error 1 error C2059: syntax error : '{' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 31

Error 6 error C2059: syntax error : '}' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 37

Error 8 error C2059: syntax error : '}' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 37

Error 5 error C2059: syntax error : 'return' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 36

Error 4 error C2061: syntax error : identifier 'WriteLine' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 32

Error 7 error C2143: syntax error : missing ';' before '}' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 37

Error 2 error C2309: catch handler expected a parenthesized exception declaration c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 31

Error 3 error C2319: 'catch' must be followed by a compound statement. Missing '{' c:\Users\Ahmed\documents\visual studio 2010\Projects\eeee\eeee\eeee.cpp 32

2 Answers 2

2

If you remove the recurrsion level parts of that code, you should have what you wanted. You mentioned 3 exceptions, but I dont see anything clarifying what they were.

static void ScanMyDir( String^ SourceDir)
{
array <String^> ^fileEntries = Directory::GetFiles(SourceDir);
for each  (String^ fileName in fileEntries)
{
Console::WriteLine(fileName);
}
array<String^> ^SubDirEntries = Directory::GetDirectories(SourceDir);
for each (String^ subdir in SubDirEntries)
if ((File::GetAttributes(subdir) & FileAttributes::ReparsePoint)!= FileAttributes::ReparsePoint)
            ScanMyDir(subdir);
}
};
8
  • there is a problem saying "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access to the path 'c:\found.000\' is denied." and also how can the code manage the end of looping! when will stop!
    – Aan
    Apr 4, 2011 at 10:43
  • Error trapping is always a good idea, some folders such as that are protected so, what you would do is put a try round the call to ScanMyDir, if it fails, you want it to try the next. As for how it stops, it stops when it runs out of files and directories, which is what you wanted (or at least thats how I read it)
    – BugFinder
    Apr 4, 2011 at 10:49
  • I am not really familiar with dealing with error trapping and exceptions. if you can share your knowledge to solve the problem i will be very glad
    – Aan
    Apr 4, 2011 at 11:01
  • try { ScanMyDir(subdir); } catch { // add useful code to display unreadable directory etc} If you do that, should the call to that directory faill it will continue on
    – BugFinder
    Apr 4, 2011 at 11:34
  • But i am using C++ CLI ... i mean what i know that we need a handle when throwing a C++ CLI exception. I tried the "try\catch" block you suggested but it shows alot of errors! (syntax ones).
    – Aan
    Apr 4, 2011 at 11:58
0

For the love of all things holy, please don't use recursion. I answered this question for C# here. The conversion to C++/CLI is straight forward. If you don't want to do that, compile it into its own assembly (and optionally ilmerge it into yours).

3
  • Recursion elegantly solves tree recursion problems. Easy to see by comparing code size. It isn't his problem anyway. Apr 5, 2011 at 3:34
  • Modern filesystems aren't trees, they are directed graphs, possibly with cycles. Correct code for this is decidedly non-trivial.
    – Ben Voigt
    Apr 5, 2011 at 4:17
  • Hans, you write your recursive code and I'll send you a disk that will crash it that won't crash mine. Care to make it a wager?
    – plinth
    Apr 5, 2011 at 12:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.