Click here to Skip to main content
15,937,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
please help me how can we create a subfolder(with date) in a folder where we need to move the files from folder to subfolder.written below code to upload files but i am unable to create a new date folder and move files to that folder.

What I have tried:

Move upload
                                          if(!Directory.Exists(_SelectedPath+\\"UPLOAD"))
							{
						    Directory.CreateDirectory(_SelectedPath "UPLOAD");
							}			
                           // var Folder = Directory.CreateDirectory(_SelectedPath+"UPLOAD");
                          // string fileName = SourceFileName;
                          //  String extension = Path.GetExtension(file1.Extension);
                            string sourcePath = @_SelectedPath;
					        string targetPath = @_SelectedPath+"UPLOAD" +@"\"+ FileName+ "_" + ISO_Date();
					        // Use Path class to manipulate file and directory paths.
					        string sourceFile = System.IO.Path.Combine(targetPath, FileName);
					        string destFile = System.IO.Path.Combine(sourceFile, FileName);					
					        // To copy a folder's contents to a new location:
					        // Create a new target folder, if necessary.
					        if (System.IO.Directory.Exists(sourcePath))
					        {
					            string[] files = System.IO.Directory.GetFiles(sourcePath);
					
					            // Copy the files and overwrite destination files if they already exist.
					            foreach (string s in files)
					            {
					                // Use static Path methods to extract only the file name from the path.
					                FileName = System.IO.Path.GetFileName(s);
					                //destFile = System.IO.Path.Combine(targetPath, FileName);
					                System.IO.File.Copy(s, targetPath, true);
					            }
					        
					        }					       						
Posted
Updated 15-May-18 2:02am
Comments
F-ES Sitecore 15-May-18 7:12am    
Not sure what the question is, but google "c# create directory" and you'll find code samples, then google "c# move file" and you'll find code for that. Just put the two together.
Member 13818142 15-May-18 7:17am    
yes i have done like this c:/folder1/subfolder1/11-05-2018 i have done till folder path i need to create one more directory with date when i upload file every time i need to create current date directory. can you suggest

Quote:
yes i have done like this c:/folder1/subfolder1/11-05-2018 i have done till folder path i need to create one more directory with date when i upload file every time i need to create current date directory. can you suggest
You mean:
string dirName = Path.Combine(@"C:\folder1\subfolder1", DateTime.Now.ToString("dd-MM-yyyy"));
Directory.CreateDirectory(dirName);
But if you are talking upload, then you probably can't create folders on the C: drive directly as your website almost certainly doesn;t have the required permissions. You should probably use Server.MapPath to convert a web-site relative path to a file system path and store your files under your website folder structure.
 
Share this answer
 
v2
Comments
Member 13818142 15-May-18 7:43am    
@orginalGriff i used E drive to save the files the required date folder was created and i can move the files.i have written below code.
String Todaysdate = DateTime.Now.ToString("dd-MMM-yyyy");
string datefolder = Path.Combine(subFolder,Todaysdate);
if(!Directory.Exists(datefolder))
{
Directory.CreateDirectory(datefolder);
}
string sourcePath = firstFolder;
string targetPath = datefolder; }
string sourcePath = firstFolder;
string targetPath = datefolder;
string sourceFile = System.IO.Path.Combine(sourcePath, FileName);
string destFile = System.IO.Path.Combine(targetPath, FileName)
String Todaysdate = DateTime.Now.ToString("dd-MMM-yyyy");
string datefolder = Path.Combine(subFolder,Todaysdate);
if(!Directory.Exists(datefolder))
{
Directory.CreateDirectory(datefolder);
}
string sourcePath = firstFolder;
string targetPath = datefolder; }
string sourcePath = firstFolder;
string targetPath = datefolder;
 
Share this answer
 
Comments
CHill60 15-May-18 8:27am    
Is this meant to be a solution?
Member 13818142 15-May-18 8:50am    
this way i created date directory to a sub dirctory

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