Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to organize the copied text file from the other server to our server that will be saved in by daily folder, weekly folder or by date folder.

Below is my code on copying files from the other computer. But I need to organize the copied text files in the folder by daily,weekly and by date.

Please help. Thank you and God bless.. - Jess

// Here's the program in c#
C#
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        Copier();
    }

    // Method Copier
    public static void Copier()
    {
        File.Copy(@"\\193.35.208.17\con_d_MeanAct_Inst\201102\
                    sample-data.txt", @"c:\sonix\sample-newdata.txt");

        Console.ReadKey();

    }
}
Posted
Updated 3-Feb-11 14:09pm
v2

Something like this should do:
C#
// Method Copier
public static void Copier()
{
  string sourceFile = @"\\193.35.208.17\con_d_MeanAct_Inst\201102\sample-data.txt";
  string dir = Path.GetDirectoryName(sourceFile);
  string[] parts= dir.Split('\\');
  string destPath = Path.Combine(@"C:\WhereverYouWant\", parts[parts.Length - 1]);
  if (!Directory.Exists(destPath))
  {
    Directory.CreateDirectory(destPath);
  }
  string destFile = Path.Combine(destPath, @"\newData.txt");
  File.Copy(sourceFile, destFile);
}


Modified to ensure that destination exists.
 
Share this answer
 
v2
Comments
Silver Lightning 3-Feb-11 20:30pm    
Hi Sir Henry,
Thanks for the answer but, how could I organize and determine the text files to be copied in by weekly and daily to be saved in particular folder?
Henry Minute 3-Feb-11 20:36pm    
Well, if you don't know how they are organized, how can I know?

From the code you posted it looks as if the source directory name consists of the 4 digit year combined with the 2 digit month. Is that all there is to it? Or are they sometimes somewhere else?

How are the destination folders supposed to be organized? Only you know that and you haven't said.

Henry Minute 3-Feb-11 20:58pm    
If it helps you, you can get strings for the destination folder names like this:
string monthDirName = DateTime.Now.ToString("yyyyMM"); // gives "201102"
string dayDirName = monthDirName + Path.Combine(@"\", DateTime.Now.ToString("dd")); // gives "201102\04"
Silver Lightning 3-Feb-11 21:03pm    
Opps sorry sir Henry. Your code really helps me. Can I ask one more? how can I or What would I use to automate this program just like an scheduler program that would get this text files daily? sorry for disturbing you sir, just I need it urgent. (just give a sample or pattern code for this in c#. Thank you and God bless =)
Henry Minute 3-Feb-11 21:09pm    
Well there is always the Windows Task Scheduler, which comes with many editions of Windows.
Look here: http://www.iopus.com/guides/winscheduler.htm

Or there are many other alternatives. Do an internet search on "windows scheduler" (without the quotes). That will get you lots of suggestions like:
http://www.splinterware.com/products/wincron.htm.

Some will be free, some at a cost.
well, once its downloaded where is it saved?on the application, or on the computer? And do you have some sort of GUI?
 
Share this answer
 
v2
Comments
Henry Minute 3-Feb-11 20:10pm    
The OPs code shows you where it is copied. Why ask?
vlad781 3-Feb-11 20:19pm    
Well, depending on where he wants to organize the files. Whether its directly on the computer or within the application.
fjdiewornncalwe 3-Feb-11 21:19pm    
What do you mean by "within the application"?
vlad781 4-Feb-11 18:01pm    
whether the application will save files to itself or the computer.

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