File or Folder rename to lower case In C# using DirectoryInfo/FileInfo.MoveTo()
Asked Answered
B

4

3

I have a program that renames files or folders to lower case names. I have written this code:

    private void Replace(string FolderLocation, string lastText, string NewText)
    {
        if (lastText == "")
        {
            lastText = " ";
        }
        if (NewText == "")
        {
            NewText = " ";
        }

        DirectoryInfo i = new DirectoryInfo(FolderLocation);
        string NewName = "";
        if (checkBox2.Checked)
        {
            if (i.Parent.FullName[i.Parent.FullName.Length - 1].ToString() != "\\") //For parents like E:/
            {
                NewName = i.Parent.FullName + "\\" + i.Name.Replace(lastText, NewText);
            }
            else
            {
                NewName = i.Parent.FullName + i.Name.Replace(lastText, NewText);
            }

                NewName = NewName.ToLower();


            if (NewName != i.FullName)
            {
                 i.MoveTo(NewName);
            }
            foreach (DirectoryInfo sd in i.GetDirectories())
            {
                Replace(sd.FullName, lastText, NewText);
            }
        }
        if (checkBox1.Checked)
        {
            foreach (FileInfo fi in i.GetFiles())
            {
                NewName = fi.Directory + "\\" + fi.Name.Replace(lastText, NewText);

                    NewName = NewName.ToLower();

                if (NewName != fi.FullName)
                {
                    fi.MoveTo(NewName);
                }
            }
        }
    }

But I get the following exception:

"Source and destination path must be different."

How can I solve this issue?

Bridge answered 16/11, 2011 at 13:52 Comment(5)
Windows is case insensitive, as far as file names are concerned.Inexpugnable
ok.but how does it show file names?Bridge
It uses what is saved. But when comparing filenames - "FILENAME" == "filename" == "FileName", as far as it is concerned.Inexpugnable
I think it is not allowed by Windows OS to have a given file eg "test.txt" and "Test.txt" in the same folder. That is why your geht an exception. MoveTo copies the file to a new position and deletes the old one. Try FileSystem.Rename instead (msdn.microsoft.com/en-gb/library/…)Galliard
@PilgerstorferFranz although in standard cases you are right Windows/NTFS actually can be configured to allow that.Reger
I
6

Since Windows is case insensitive, as far as file names are concerned, you will need to rename the file to a temporary name then rename back with lowercase characters.

Inexpugnable answered 16/11, 2011 at 13:54 Comment(4)
now I got "Access denied" eception.but I run it as administrator.what is happening??Bridge
@ahmadalishafiee - "access denied" always means lack of permissions. Make sure the path is permitted to the user executing the application (might be your user).Inexpugnable
It means there is no access to files/folders for programs.But I want to publish my program.How can I fix this error?Bridge
Most probably the directory is in use. This can be a simple Windows Explorer instance. Directory operations with DirectoryInfo are very sensible to that.Lenzi
R
2

Although Windows Filesystems store names case-senstivie they behave case-insensitive on name comparison thus your renaming operation won't work...

IF you really need/want to do that you will need to first rename temporarily the file/directory to something different and unique, then rename it "back" to the "lower case name" you want.

For reference see http://msdn.microsoft.com/en-us/library/ee681827%28v=vs.85%29.aspx and http://support.microsoft.com/kb/100108/en-us .

IF you need NTFS to be case-sensitive you can set the dword ObCaseInsensitive under HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\ to 0 (NOT RECOMMENDED!).

IF you are dealing with NFS then see http://technet.microsoft.com/en-us/library/cc783185%28WS.10%29.aspx .

Reger answered 16/11, 2011 at 13:55 Comment(0)
K
1

This works:

File.Move(destinationFilePath, destinationFilePath);
Karlow answered 28/4, 2020 at 2:53 Comment(0)
G
0

Unfortunately this is a windows issue as it is case insensitive as Oded mentions in the comments. What you would have to do is to rename the folder twice. By moving the folder to a new temporary name then back to the lowercase of the original name.

Grayback answered 16/11, 2011 at 13:56 Comment(1)
This is not a Windows issue, this is a .Net issue. Win32's MoveFileEx supports case-only renames; .Net gets this one wrong.Scorpaenoid

© 2022 - 2024 — McMap. All rights reserved.