Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I've gota problem which I can't seem to get solved in the simplest possible way... Maybe its because its Friday. Is there someone who can help me solve it please?

I'm searching all directories and sub directories for *.doc files, as shown below and saving them to a List.

The docfiles which I'm searching for is as such:

Unit testing folder for: Blah.c
1590-VMM-C-2004_010 SCIC Rev 00.doc
1590-VMM-C-2004_010 SCIC Rev 01.doc
1590-VMM-C-2004_010 SCIC Rev 02.doc
1590-VMM-C-2004_010 SUTC Rev 00.doc
1590-VMM-C-2004_010 SUTC Rev 01.doc
1590-VMM-C-2004_010 SUTC Rev 02.doc

Unit testing folder for: Foo.c

1590-VMM-C-2004_096 SCIC Rev 00.doc
1590-VMM-C-2004_096 SCIC Rev 01.doc
1590-VMM-C-2004_096 SUTC Rev 00.doc
1590-VMM-C-2004_096 SUTC Rev 01.doc

In each of my unit testing folders there are a bunch of these *.doc files, which revision depends on howmany times the unit tests was updated. The 010 and 096 number that was bolded above is a unique number that gets assigned to docs from the same unit test.

The Question: I want to search for all the latest revisions of the various scic and sutc docs in the various unit testing folders and save them in a list. How can I do this without having to write a whole bunch of nested if-statements?

Example of what list output should look like:
List[0] = "1590-VMM-C-2004_010 SCIC Rev 02.doc"
List[1] = "1590-VMM-C-2004_010 SUTC Rev 02.doc"
List[2] = "1590-VMM-C-2004_096 SCIC Rev 01.doc"
List[3] = "1590-VMM-C-2004_096 SUTC Rev 01.doc"


FUNCTION EXAMPLE:

/*--[ Function ]-----------------------------------------------------------*/
/*
  Function Name   : SearchLatestDocPaths
  Description     : This function searches for all the latest rev. *.doc file
                    paths in the respective unit testing folders.

  Access Modifier : public
  Param           : string sDir

  Retval          : -none-
*/
/*-------------------------------------------------------------------------*/
public void SearchLatestDocPaths(string sDir)
{
  list_utLatestDocFilePath.Clear();

  try
  {
    // Search sub directories and save *.doc files
    foreach (string d1 in Directory.GetDirectories(sDir))
    {
      foreach (string d2 in Directory.GetDirectories(d1))
      {
        foreach (string f in Directory.GetFiles(d2, "*.doc"))
        {
          if (f.Contains(".doc") == true)
          {
            list_utLatestDocFilePath.Add(f);
          }
        }
      }
      WriteToBufferSwSrcFilePaths(d1, ".doc", false);
    }
    // Search current directories and save *.doc files
    foreach (string f in Directory.GetFiles(sDir, "*.doc"))
    {
      list_utLatestDocFilePath.Add(f);
    }
    // Print all documents to see correctness
    foreach (string f in list_utDocFilePath)
    {
      MessageBox.Show(f);
    }
  }
  catch (System.Exception excpt)
  {
    Console.WriteLine(excpt.Message);
  }
}


;-)
Posted
Updated 12-Nov-10 4:00am
v3

You could just use this search pattern:
"*SCIC*.doc" and "*SUTC*.doc" and select only the two highest revision numbers. I also changed your search method so it operates recursively which also means less code.
C#
public void SearchLatestDocPaths(string sDir, li)
{
  // list_utLatestDocFilePath.Clear(); <- would make the recursion useless
  try
  {
    // Search sub directories and save *.doc files
    foreach (string d1 in Directory.GetDirectories(sDir))
    {
      SearchLatestDocPaths(d1);
      WriteToBufferSwSrcFilePaths(d1, ".doc", false); // <- adding file path?
    }
    // Search current directories and save *.doc files
    foreach (string f in Directory.GetFiles(sDir, "*.doc"))
    {
      list_utLatestDocFilePath.Add(f);
      // Print all documents to see correctness
      MessageBox.Show(f);
    }
  }
  catch (System.Exception excpt)
  {
    Console.WriteLine(excpt.Message);
  }
}

http://msdn.microsoft.com/en-us/library/8he88b63.aspx[^]

Good luck!
 
Share this answer
 
This does the trick thx:

/*--[ Function ]-----------------------------------------------------------*/
/*
  Function Name   : SearchDocPaths
  Description     : This function searches for all the docs
                    in the various unit testing subfolders
                    and saves their paths to a list.

  Access Modifier : public
  Param           : string sDir

  Retval          : -none-
*/
/*-------------------------------------------------------------------------*/
public void SearchDocPaths(string sDir)
{
  try
  {
    // Search sub directories and save *.doc files
    foreach (string d1 in Directory.GetDirectories(sDir))
    {
      // recursion
      SearchDocPaths(d1);

      // Search current directories and save *.doc files
      foreach (string f in Directory.GetFiles(d1, "*.doc"))
      {
        list_utDocFilePath.Add(f);
      }
    }
  }
  catch (System.Exception excpt)
  {
    Console.WriteLine(excpt.Message);
  }
}
/*--[ Function ]-----------------------------------------------------------*/
/*
  Function Name   : ExtractLatestDocPaths
  Description     : This function searches for all the latest rev. *.doc file
                    paths in the respective unit testing folders and saved it
                    to a list.

  Access Modifier : public
  Param           : string sDir

  Retval          : -none-
*/
/*-------------------------------------------------------------------------*/
public void ExtractLatestDocPaths(string sDir)
{
  int idx = 0;
  string latest_rev_string = "";

  // Fill list buffer containing all document, paths
  SearchDocPaths(sDir);

  // Extract only the latest revisions
  for (idx = 0; idx < list_utDocFilePath.Count; idx++)
  {
    latest_rev_string = list_utDocFilePath[idx];
    if (((idx + 1) == list_utDocFilePath.Count) ||
        (list_utDocFilePath[idx].Substring(
         list_utDocFilePath[idx].Length - 35, 24)) !=
        (list_utDocFilePath[idx + 1].Substring(
         list_utDocFilePath[idx + 1].Length - 35, 24)))
    {
      list_utLatestDocFilePath.Add(latest_rev_string);
    }
    else
    {
      // do nothing
    }
  }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900