Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

c# - Recursively resume directory scan

I have all this directory's created:

dirdemojob3: C:	empdirdemodir01
dirdemojob3: C:	empdirdemodir01dir0101
dirdemojob3: C:	empdirdemodir01dir0102
dirdemojob3: C:	empdirdemodir01dir0102dir010201
dirdemojob3: C:	empdirdemodir02
dirdemojob3: C:	empdirdemodir02dir0201
dirdemojob3: C:	empdirdemodir03
dirdemojob3: C:	empdirdemodir03apalqoei
dirdemojob3: C:	empdirdemodir03xpt01

The program recursively searches for directories and works well.

If the scan encounters an error, for example, in "dir02dir0201", it stops the scan. I would like to start the scan in another run of the program, or next day, but don't start from the dir01. I want to resume in "dir02dir0201" and continue like a normal scan.

If you start again from "dir02dir0201", ain't gonna scan dir03 and so on. I'm looking for this ability to resume directory scans.

Thank you.


    public static void DirSearch(string _job, string _sDir, int _side)
    {
        try
        {
            foreach (string _d in Directory.GetDirectories(_sDir))
            {
               // here save to txt or anything else
                DirSearch(_job, _d, _side);
            }
        }
        catch (System.Exception excpt)
        {
              // show error
        }

This is important part of the code: recursion. This get's all directories and ignores those common errors of access denied, etc.

Nor GetDirectories nor EnumerateDirectories (http://msdn.microsoft.com/en-us/library/dd383573.aspx) have the ability to do what I want. So I'm trying to see if somebody had the same problem: getdirectories() and be able to resume if vpn drops or in another day.

edit: thank you ChrisF for the edit.

Anyway, this code is usefull but there is no direct solution for my problem. I'm coding a function to do what I need, and later will post it here.

// **************************

UPDATE: I don't know how to add this other way, so here goes.

This next code reads the directories and saves it to a sql server and writes some info on the console. It is a recursive function but it controls the level in a special way. This way can be used to do do file scan for backup or re-sync and resume the job, if it fails in anyway. I found that it is no possible to resume directory search. But you can always get the full list and then backup the files based on the saved list. This way, you can always resume the job.

public static void DirSearchByLevel(string _job, string _sDir, int _side)
{
    //
    // verify that root dir exists
    // code here
    if (!Directory.Exists(_sDir)) { KONSOLE("Directory does not exists: " + _sDir); return; }
    //
    // save root dir - complete 0 (not yet); dirlevel = 0 (root of search directory)
    KONSOLE("Scanning " + _sDir + " - complete=" + _complete.ToString() + " level=" + _dirlevel.ToString());
    errodb = tools.SaveDirToDB(_job, _side, _sDir,
                                      DateTime.MinValue, DateTime.MinValue, DateTime.MinValue,
                                      _complete, _dirlevel);
    //
    // save base dir - complete 0 (not yet); dirlevel = 1 (base of search directory)
    // *****************************************************************************
    try
    {
        foreach (string _d in Directory.EnumerateDirectories(_sDir, "*.*", SearchOption.TopDirectoryOnly))
        {
            //KONSOLE(_d);
            errodb = tools.SaveDirToDB(_job, _side, _d,
                                       DateTime.MinValue, DateTime.MinValue, DateTime.MinValue,
                                       _complete, _dirlevel);
            // get dirlevel +1 to scan next level; recursion will downlevel it next
            _dirlevel += 1;
            DirSearchByLevel(_job, _d, _side);
        }
        // if none directory was found, we still at 0; otherwise, go back one level
        if (0 != _dirlevel) _dirlevel -= 1;
        // *****************************************************************************
        //
        // save base dir; complete = 1;
        _complete = 1;
        errodb = tools.UpdateDir(_job, _side, _sDir, _complete, _dirlevel);
        KONSOLE("Update " + _sDir + " - complete=" + _complete.ToString() + " level=" + _dirlevel.ToString());
    }
    catch (Exception exp)
    {
        KONSOLE("Error in " + _sDir + " - complete=" + _complete.ToString() + " level=" + _dirlevel.ToString());
    }
}

And this is a demo result of this function:

Scanning C:	empdirdemo - complete=0 level=0 Scanning
C:	empdirdemodir01 - complete=0 level=1 Scanning
C:	empdirdemodir01dir0101 - complete=0 level=2 Update
C:	empdirdemodir01dir0101 - complete=1 level=1 Scanning
C:	empdirdemodir01dir0102 - complete=1 level=2 Scanning
C:	empdirdemodir01dir0102dir010201 - complete=1 level=3 Update
C:	empdirdemodir01dir0102dir010201 - complete=1 level=2 Update
C:	empdirdemodir01dir0102 - complete=1 level=1 Update
C:	empdirdemodir01 - complete=1 level=0 Scanning
C:	empdirdemodir02 - complete=1 level=1 Scanning
C:	empdirdemodir02dir0201 - complete=1 level=2 Update
C:	empdirdemodir02dir0201 - complete=1 level=1 Update
C:	empdirdemodir02 - complete=1 level=0 Scanning
C:	empdirdemodir03 - complete=1 level=1 Scanning
C:	empdirdemodir03apalqoei - complete=1 level=2 Update
C:	empdirdemodir03apalqoei - complete=1 level=1 Scanning
C:	empdirdemodir03xpt01 - complete=1 level=2 Update
C:	empdirdemodir03xpt01 - complete=1 level=1 Update
C:	empdirdemodir03 - complete=1 level=0 Update C:	empdirdemo -
complete=1 level=0

That's all. Thank you for your time and patience.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • Designate a particular file to be a temp file used by this program.

  • When the program starts, don't scan any files that are listed in that temp directory.

  • When you finish scanning a directory add it to the end of the file.

  • When you finish scanning all of the directories (or whatever condition indicates that they should be scanned again when the program is run next time), wipe the file (so that they can be scanned again the next day).

Another option that is the same general idea, just slightly modified, is to do this:

  • Designate a particular file to be a temp file used by this program.

  • When the program starts, if the temp file is empty, populate it with all of the directories that need to be processed

  • Go through each path in the file and process it

  • When you process a directory, remove it from the file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...