Get Files from folder and sub folders that contains a restricted folder or file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tyler Wiebe
    New Member
    • Mar 2011
    • 66

    Get Files from folder and sub folders that contains a restricted folder or file.

    Okay, I need to get every audio file on a hard drive, and move them to a single location sorted by the first letter of the artists name, artists name, album, and song title.

    I have the code to do that, and it worked on one of the hard drives I'm organizing files from, but the other two contain secured folders and files, and I get an exception when I try and get access to them, and my program justs stops and won't continue.

    I'm using something like the following
    Code:
            public System.Collections.Generic.List<string> GetFiles(string StartingDirectory)
            {
                System.Collections.Generic.List<string> Extensions = new System.Collections.Generic.List<string>();
                Extensions.Add("mp3");
                //More Extensions
    
                System.Collections.Generic.List<string> Files = new System.Collections.Generic.List<string>();
                foreach (string Extension in Extensions)
                {
                    foreach (string File in System.IO.Directory.GetFiles(StartingDirectory, "*." + Extension, System.IO.SearchOption.AllDirectories)) Files.Add(File);
                }
                return Files;
            }
    Any help would be appreciated, and sooner the better.
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    You could run the application in admin mode (not sure that would help though) other than that I cannot think of a way since the Windows installation has restricted folders & files that aren't accessible (for security purposes)

    Comment

    • Tyler Wiebe
      New Member
      • Mar 2011
      • 66

      #3
      Thank you, I'm assuming it worked since it's actually running longer then 1 second. I'm still going through all the files, but it's moved about 250 files that it wasn't before.

      Thanks you again.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Another option you might want to consider is catching your exceptions. In general, code that can generate exceptions should be placed within a try/catch block. This block can surround any code you wish, so there would be nothing stopping you from doing something like...

        Code:
        foreach (string folder in foldersToProcess)
        {
          try
          {
            // Do whatever...
          }
          catch (Exception exception)
          {
            someLog.AddMessage(string.Format("Could not process folder, '{0}', reason: {1}", folder, exception.Message));
          }
        }
        That might help your program recover more gracefully from exceptions :)

        Comment

        • Tyler Wiebe
          New Member
          • Mar 2011
          • 66

          #5
          Okay, I hadn't had a chance to reply yet, but it turns out that giving it administrator privileges doesn't help, the only reason it was working for a bit was because I accidentally set it to search only the top directory and not all of the directories.

          Comment

          Working...