Drew Kennedy
Drew Kennedy

Reputation: 4168

DirectoryNotFoundException - Could Not Find Part of the Path

I have a console application I built for myself to rename .mp3 files I download. This application has worked flawlessly for quite a few months, but is all of a sudden tossing the titled exception at me with one particular directory. Not only is it this one directory, but is happening on only a select number of files - 3 of them were successfully renamed.

Here is my directory path and files:

enter image description here

Here is a snippet of the directory path where the exception is thrown, along with the exception's message:

enter image description here

And here is my code:

static void Main(string[] args) {
    string dir = @"M:\Temp Downloading Folder";

    var files = new DirectoryInfo(dir + @"\Gregory Alan Isakov Discography [2005 - 2013]\Rust Colored Stones").GetFiles("*.mp3").ToList();

    foreach (var item in files) {
        if (item.Name.Substring(0, 2).All(char.IsDigit)) {
            //string fullName = item.FullName.Replace("\\", "/");
            string newName = "Gregory Alan Isakov ";
            //exception thrown here
            File.Move(item.FullName, item.FullName.Replace(item.Name.Substring(0, 3), newName));
        }
    }
    Console.WriteLine("Done!");
    Console.ReadKey();
}

I tried changing the path to have / instead of \\ with the same result.

According to the Documentation on MSDN:

The exception that is thrown when part of a file or directory cannot be found.

However, the directory is found and correctly renamed the first 3 files (as depicted in the first image).

Can anyone explain to me why this is happening?

Upvotes: 3

Views: 5026

Answers (2)

Chase
Chase

Reputation: 944

The problem may be due to the length of your folder path. Shorten the length and try again.

Microsoft's documentation on file naming and path lengths mentions that Windows imposes a 260 character limit for the total length of a path plus it's filename; this is referenced as the Maximum Path Length Limitation, quoted here for easier reference:

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

However, if you are insistent on using the path names as-is, you can explore using the extended-length path name convention by prefixing paths with the "\?" notation.

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "\?\" prefix. For example, "\?\D:\very long path".

Upvotes: 1

user5766999
user5766999

Reputation:

You have a bug in your code which changes the folder instead of the name of the file, running your code:

C:\Temp Downloading Folder\Gregory Alan Isakov Discography [2005 - 2013]\Rust Colored Stones\05 - Only Ghosts.mp3

is changed to:

C:\Temp Downloading Folder\Gregory Alan Isakov Discography [20Gregory Alan Isakov - 2013]\Rust Colored Stones\Gregory Alan Isakov - Only Ghosts.mp3

I let you debug and fix it yourself.

Upvotes: 4

Related Questions