i need to sort files by date into different folders.
all files from januar must be moved from their current directory to a folder named januar and all files from february to a folder named february and so on..
the folder is not created, so the code have to create a folder from the date of the files..
Does any one of you know how to do this ?
:)
Loading

manuprasad kadavat8 reputationPosted Mar 4, 2013, 12:58 AM EST
FileInfo MyFile as new FileInfo("c:\\scale.txt") ;
string Strcreation = MyFile.creationTime.ToString();
VulpesRank #55 · 29.2K reputationPosted Mar 4, 2013, 1:34 AM EST
The new directories are created as sub-directories of the current directory:
jesper89 reputationPosted Mar 4, 2013, 1:46 AM EST
string source = @"C:\testFolder"; //Folder where all files are stored
string target = @"C:\";
foreach (var file in Directory.EnumerateFiles(source))
{
var dest = Path.Combine(target + DateTime.Now.ToString("MMMM"),
Path.GetFileName(file));
File.Move(file, dest);
}
is it bad programming ?
my directories are created before this code is executed.
VulpesRank #55 · 29.2K reputationPosted Mar 4, 2013, 2:26 AM EST
The way you couched the original question led manuprasad and myself to conclude that you wanted to group them by the month of creation?
jesper89 reputationPosted Mar 4, 2013, 2:51 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 4, 2013, 3:30 AM EST
jesper89 reputationPosted Mar 5, 2013, 3:18 AM EST
Folder structure:
year folder
month folder
files
right now i have only month folder and down..
if there is any files from 2013 og older, it needs to create a folder named 2010 and again put files from january 2010 in the folder named january for example. how do i create the upper structure.. i have tried to extract the year from the files but the output its everything else than a year :)
VulpesRank #55 · 29.2K reputationPosted Mar 5, 2013, 4:22 AM EST
You seem to be saying that if there are any files from 2013 or older, then you need to create a folder called 2010 and then have sub-folders within that for each of the 12 months.
However, currently, all files will be dated 2013 or older and so will go into the same 2010 'top level' folder. If that's the case, you might as well define the target folder to be:
string target = @"C:\testFolder\2010";
and then use the previous code.
I'd have thought it would make more sense to have top level folders for each year and then sub-folders for the months within each of those years.
jesper89 reputationPosted Mar 5, 2013, 4:36 AM EST
Its exact wat i wanted.. but the code have to create the folders on demand, is it still not clear :/ ?
VulpesRank #55 · 29.2K reputationPosted Mar 5, 2013, 5:20 AM EST
Try this: