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

Community
  • 1
  • 1
Aan
  • 12,247
  • 36
  • 89
  • 150

2 Answers2

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);
}
};
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • 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 04 '11 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 04 '11 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 04 '11 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 04 '11 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 04 '11 at 11:58
  • What were the syntax errors? Chances are I did a typeo, all you need is a try and catch, to put an "I got an error but contiuned" message in the catch around that ScanMyDir command.. – BugFinder Apr 04 '11 at 12:01
  • i added my code you can see it :> i put it in your answer by mistake – Aan Apr 04 '11 at 12:12
  • Firstly, the try/catch would need to be in the recursive procedure as well, not just the main loop. The catch code needs to look something like "catch ( char * str ) { .. }" which is part of the errors. Please read: http://msdn.microsoft.com/en-us/library/6dekhbbc%28v=vs.80%29.aspx After your function you're missing a ; – BugFinder Apr 04 '11 at 13:14
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).

Community
  • 1
  • 1
plinth
  • 48,267
  • 11
  • 78
  • 120
  • Recursion elegantly solves tree recursion problems. Easy to see by comparing code size. It isn't his problem anyway. – Hans Passant Apr 05 '11 at 03:34
  • Modern filesystems aren't trees, they are directed graphs, possibly with cycles. Correct code for this is decidedly non-trivial. – Ben Voigt Apr 05 '11 at 04: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 05 '11 at 12:31