Question : Read matching files in a folder, read one of them and insert the Data in a Table

Helo Everyone!!!

I really have a big Problem and no solution or idea how to solve it. I really need help from Experts like you... I am really not very goog with files and directories and I have no one to help me. So, I need a really good Program for this problem.

In a folder for example in c:\files there will be transfering 4 files with the following names
1.1234_a.txt
2.1234_b.txt
3.1234_c.pdf
4.1234_d.txt

The 4 files will also have the same prefix name before the underscore and the only difference is the letters a,b,c,d after the underscore and the _c file will be a pdf file.
So, when the 4 files are in the folder the program will check if the files belong together(with the name before underscore) and when  all 4 are there (a,b,c.pdf,d) then it should run. It shall also ignore all other files that not belong together. Then the Programm should take the fullpath for each one of the files. I mean the fullpath where the files will be moving.. I will move the files in the folder  c:\files\old_files\current_date. The moving of the files should be the last step (I think).
So, we will take the archiving fullpath of each file and then I want to read the _d file to take some info. This file is a csv file with one line -->

invNo ; GID  ; bla; blabla ;blablabla

From the _d file in need the first 2 rows invNo and GID.
So with the archiving fullpath of the 4 files and the invno and GID that we have read from the _d file, then it should be running a store procedure 4 Times with 3 parameters -->

@GID  = The First row that we have read from the d file
@invNo =The second row that we have read from the d file
@Path  = The archiving fullpath of all 4 files

The stored procedure than -->

INSERT INTO [Demo].[dbo].[ES00Documents]
           ([TableName]
           ,[Caption]
           ,[fGID]
           ,[UNCPath])
     VALUES
           ('IMP_eDocArchive'
           ,@invNo
           ,@GID                                    
          ,@Path)                    
This store procedure will run 4 times for all 4 files. At the end the files should be moved in the archiving  folder.

Please, I know its really a difficult program but I need help.

Thank you for any help!!!

Rena24

Answer : Read matching files in a folder, read one of them and insert the Data in a Table

I helped with the first part (archive the files into a folder). Let me know if it works.
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void handleFiles(string folderPath, string prefix, List suffixes)
        {
            //create a folder with current date
            string newFolderName = folderPath + "\\old_files\\" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            System.IO.Directory.CreateDirectory(newFolderName);
            foreach (string suffix in suffixes)
            {
                string srcFilename = string.Format("{0}\\{1}_{2}", folderPath, prefix, suffix);
                string dstFilename = string.Format("{0}\\{1}_{2}", newFolderName, prefix, suffix);
                System.IO.File.Move(srcFilename, dstFilename);
            }
            //then handle the d file
            //TODO
        }

        void checkFile(string folderPath)
        {
            string[] files = System.IO.Directory.GetFiles(folderPath);
            Dictionary> dict = new Dictionary>();
            foreach (string file in files)
            {
                string filename = System.IO.Path.GetFileName(file);
                string[] parts = filename.Split(new char[] { '_' });
                //parse the prefix
                if (parts.Length == 2)
                {
                    if (!dict.ContainsKey(parts[0]))
                    {
                        dict[parts[0]] = new List();
                        dict[parts[0]].Add(parts[1]);
                    }
                    else
                    {
                        dict[parts[0]].Add(parts[1]);
                    }
                }
            }
            foreach (string key in dict.Keys)
            {
                //check if a.txt,b.txt,c.pdf,d.txt are all ready.
                List suffixes = dict[key];
                if (suffixes.Contains("a.txt") &&
                    suffixes.Contains("b.txt") &&
                    suffixes.Contains("c.pdf") &&
                    suffixes.Contains("d.txt"))
                {
                    handleFiles(folderPath, key, suffixes);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Put this into a timer, and you can have this program monitoring the src folder every several seconds.
            checkFile("c:\\files");
        }
    }
Random Solutions  
 
programming4us programming4us