Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check files 1

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB
Hi,

I have a CSV file that contains a list of all the files to be downloaded from an FTP site. Files that get downloaded need to be checked against this list and once all the files have been downloaded I need to send an email out for all the files on the list that was not part of the download from the ftp site.

I'm using c#

Thanks in advance
 
Hi,

I have a CSV file that contains a list of all the files to be downloaded from an FTP site. Files that get downloaded need to be checked against this list and once all the files have been downloaded I need to send an email out for all the files on the list that was not part of the download from the ftp site. Does anyone have an example of some code that does something similar or tell me how I can achieve the above.

I'm using c#

Thanks in advance
 
you have all the pieces you need, isolate each action and create an object to complete that action.
for example you need to
1. Download files
2. validate each file
3. if file is invalid, set aside
4. send email about invalid files.

each of the operations can be done without a dependency on the other actions. so you can focus on 1 action at a time without interfering with how the other actions are implemented.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi,

Thank you so much for your reply.

The part that I do not know how to do is basically once each file gets downloaded is how do I check each one against the csv file and once all the files have been downloaded then check the csv file for the files that did not get downloaded from the FTP site so I suppose that is point 2 and 3
 
load the csv file into an array of strings named expected.
load the downloaded file names into an of strings named actual.
var missing = expected.Except(actual);
var invalid = actual.Except(expected);

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi Jason,

Thanks for your help.

I have created 2 arrays one contains all the files that need to be downloaded and the other array contains the files that have actaullay been downloaded.Im still not sure on how I compare the 2 arrays and create a list of all the files that have not been downloaded as they are not in the array that conatins the filenames of the files that have actaullay been downloaded.

I have attached my code
Code:
    public class FileChecker
    {

        public void Main()
        {


        }

        public ArrayList textFilenames()
        {


            
            StreamReader sr = new StreamReader(TestFileNames);
            sr.ReadLine();
            ArrayList textfileitems = new ArrayList();
    
            try
            {
                while (sr.Peek() >= 0)
                {
                    string row = sr.ReadLine();


                    string[] items = row.Split(delimiter.ToCharArray());

                    string filename = items[0].ToString();
                    //int numItems = int.Parse(row);
                    for (int i = 0; i < filename.Length; i++)
                    {
                        textfileitems.Add(filename);
                    }

                    return textfileitems;
                }
            }

            finally
            {
                sr.Close();
            }
            return textfileitems;
        }



        public ArrayList Filenames()
        {

          

            FileInfo[] files = i.GetFiles();
       
            ArrayList fileitems = new ArrayList();

            try
            {
              
                foreach (FileInfo f in iles)
                {
                    string fileName = f.Name;

                    fileitems.Add(fileName);
                }

     

                return fileitems;
            }
            finally
            {
                // ;

            }
        }
    }
}
Thanks
 
loop through the expected files, if the actual files array do not contain the expected file name add it to a list of "missing" files.

do the same for "invalid" files. foreach actual file, if expected files does not contain current actual file add it to invalid collection.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 


Hi

Thank you so much for your help.


The solution you provided works fine. I just have a issue with one of my arrays. I’m trying to get the first element in the csv file and which works fine the problem is if there are nine elements in a line its reading the first element nine times so I’m getting
111111111
222222222
333333333
444444444

Instead of
1
2
3
4

The code for the array is below

Thank you
Code:
string delimiter = ",";
            StreamReader sr = new StreamReader(csvFile);
            sr.ReadLine();
            ArrayList fileitems = new ArrayList();

            try
            {
                while (sr.Peek() >= 0)
                {
                    string r = sr.ReadLine();

                    string[] items = r.Split(delimiter.ToCharArray());

                    string filename = items[0].ToString();
                    for (int i = 0; i < filename.Length; i++)
                    {
                        fileitems.Add(filename);
                    }
 
this is the problem
Code:
string filename = items[0].ToString();
for (int i = 0; i < filename.Length; i++)
{
   fileitems.Add(filename);
}
you are getting the first item from the row, then you iterating over each character in the variable filename and adding the entire string to fileitems. producing
[tt]
11111
22222
33333
44444
[/tt]

to clean up the code I would do something like
Code:
(using var reader = new StreamReader(...))
{
   while(reader.EOF == false)
   {
      var line = reader.ReadLine();
      var items = r.Split('/');
      ....
   }
}
since you are parsing csv files I would also recommend filehelper. you work with strongly typed objects instead of arrays of objects. this way your code is much more expressive.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thank you so much for your help everything works fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top