Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create a destination folder in yyyy/MM/dd format and files with in it under dd folder?

Directory Creation done as below:
C#
Directory.CreateDirectory("yyyy/MM/dd");


But I am getting 3 folders for the same. I want just a single folder with date format yyyy/MM/dd

File to move to the destination folder as below:

C#
string dest = System.DateTime.Now.ToString("dd");
Posted
Updated 26-Apr-15 21:12pm
v2
Comments
Mario Z 27-Apr-15 6:39am    
As already mentioned in the provided two solutions, the slashes are reserved characters.
However just in case you're interested you can easily create a folder hierarchy that would result in "yyyy\MM\dd" path, like this:
Directory.CreateDirectory(today.Year.ToString("0000"))
         .CreateSubdirectory(today.Month.ToString("00"))
         .CreateSubdirectory(today.Day.ToString("00"));

You cannot use a forward slash in file/folder name. It is reserved character.
MSDN: Naming Files, Paths, and Namespaces[^]

I suggest you choose another separator, like a dot or underscore or hyphen.
C#
DateTime.Now.ToString("yyyy-MM-dd")

Alternatively, you can dump the separator altogether and name the folders "yyyyMMdd".
 
Share this answer
 
v2
Slashes are not allowed in directory names.

You must use a different delimiter. e.g. "_"

C#
DateTime today = DateTime.Now;

string path = string.Format("{0:0000}_{1:00}_{2:00}", today.Year, today.Month, today.Day);

Directory.CreateDirectory(path);

This will create a directory like this: e.g. 2015_04_27
in the folder of your programme.
 
Share this answer
 

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