How to create an empty folder when a file with the folder's name already exists?

Mike Sportsman

I am sure there is something simple that I am overlooking here but I have never ran into this problem before and I cannot seam to find the cause of the problem.

This an example of the root directory on my hard-drive:

C:\Folder-1          ( this is a folder )
C:\Folder-2          ( this is a folder )
C:\somename          ( this is a file with no extension )
c:\somename.txt      ( this is a file with an extension )

The code I want to use to create a new folder is this method:

static void Main( string[] args )
{
    try
    {
        // try to create a folder in a directory 
        // where a file with the same name exsists
        createDirectory( "c:\somename" );
    }
    catch
    {
       // this catches an exception "Cannot create directory, because it already exsist
    }
}

private static void createDirectory(string filePath)
      {
         string directoryPath = Path.GetDirectoryName(filePath);

         // if the directory path does not exsists then create it
         if (!Directory.Exists(directoryPath))
         {
            Directory.CreateDirectory(directoryPath );
         }
      }

Does anyone see what is wrong here? Please help and thank you!

Lasse V. Karlsen

You can't.

You can't have two file system entries with the same name.

So either:

  1. Delete the file, then reuse the old name for the new directory
  2. Rename the file, then reuse the old name for the new directory
  3. Create the directory with a different name

Note that NTFS has its roots in POSIX, and POSIX allows multiple file system entries that differ by case, because the file system is case sensitive regarding object names. NTFS inherited this trait, but Win32 will block you from doing this. Using Win32 it is thus not possible to create a file called X together with a file called x in the same directory, but using lower level system calls it may be possible.

See https://superuser.com/questions/364057/why-is-ntfs-case-sensitive for more information on this topic.

However, identical names for multiple file system entries is not allowed and never has been.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create an empty folder when a file with the folder's name already exists?

From Dev

Windows refuses to create folder or file when a file respective folder with the same name exists

From Dev

Cannot create a folder because such folder already exists?

From Dev

Create Empty Text File In All Folders With Folder Name

From Dev

How to create a new output file in R if a file with that name already exists?

From Dev

How to create a new output file in R if a file with that name already exists?

From Dev

How to copy into with cp if destination folder already exists?

From Dev

Create folder when folder name is in a cell in the sheet

From Dev

Creating numbered folder if one with the same name already exists using a batch file

From Dev

How to create folder name or file name with special characters like \ / : * ? " < > |

From Dev

How to create file in partially known folder name using batch file

From Dev

How do I create an empty csv file on a specific folder?

From Dev

Powershell Script to Create folder, if file exists

From Dev

Android error cannot create "assets" folder because it already exists

From Dev

How do i create main folder subfolder and file name

From Dev

How to create a folder from file name and moved it in PHP?

From Dev

How to create a zip file from contents of a folder with certain name pattern?

From Dev

Is mkdir -p totally safe when creating folder already exists

From Dev

Script to create folder with same name as file and move file into folder

From Dev

Create folder for each file name and move the file

From Dev

When copying a file/folder, strange characters are added to the file or folder name

From Dev

How do I check that every file in folder A exists in folder B?

From Dev

Check if file exists in a folder

From Dev

How to check if in a folder exists some file in PHP

From Dev

How to check if file name already exists?

From Dev

How to move the file in the folder to outside and change the name to the folder name?

From Dev

How to create empty folder in azure blob storage

From Dev

How do I create an empty folder in java?

From Dev

How do I create a folder with a username and date as the folder name?

Related Related

  1. 1

    How to create an empty folder when a file with the folder's name already exists?

  2. 2

    Windows refuses to create folder or file when a file respective folder with the same name exists

  3. 3

    Cannot create a folder because such folder already exists?

  4. 4

    Create Empty Text File In All Folders With Folder Name

  5. 5

    How to create a new output file in R if a file with that name already exists?

  6. 6

    How to create a new output file in R if a file with that name already exists?

  7. 7

    How to copy into with cp if destination folder already exists?

  8. 8

    Create folder when folder name is in a cell in the sheet

  9. 9

    Creating numbered folder if one with the same name already exists using a batch file

  10. 10

    How to create folder name or file name with special characters like \ / : * ? " < > |

  11. 11

    How to create file in partially known folder name using batch file

  12. 12

    How do I create an empty csv file on a specific folder?

  13. 13

    Powershell Script to Create folder, if file exists

  14. 14

    Android error cannot create "assets" folder because it already exists

  15. 15

    How do i create main folder subfolder and file name

  16. 16

    How to create a folder from file name and moved it in PHP?

  17. 17

    How to create a zip file from contents of a folder with certain name pattern?

  18. 18

    Is mkdir -p totally safe when creating folder already exists

  19. 19

    Script to create folder with same name as file and move file into folder

  20. 20

    Create folder for each file name and move the file

  21. 21

    When copying a file/folder, strange characters are added to the file or folder name

  22. 22

    How do I check that every file in folder A exists in folder B?

  23. 23

    Check if file exists in a folder

  24. 24

    How to check if in a folder exists some file in PHP

  25. 25

    How to check if file name already exists?

  26. 26

    How to move the file in the folder to outside and change the name to the folder name?

  27. 27

    How to create empty folder in azure blob storage

  28. 28

    How do I create an empty folder in java?

  29. 29

    How do I create a folder with a username and date as the folder name?

HotTag

Archive