views:

32

answers:

1

Can someone tell me what is going to happen in this code when an error is encountered? Ideally it should continue the foreach statement until it gets to the last record, but I suspect it's stopping in the middle of the operation because when I check the number of files moved it's off by 225. If it is in fact stopping because of an error, what can I do to make it continue the loop?

I'm creating a new upload manager for our software and need to clean the old files up. There are about 715 orphaned files equaling around 750 MB after a year and a half of use because the original developers didn't write the code to correctly overwrite old files when a new one was uploaded. They also saved the files in a single directory. I can't stand that so I'm moving all of the files into a structure - Vessel Name - ServiceRequesetID - files uploaded for that service. I'm also giving the users a gridview to view and delete files they no longer need as they work the service.

protected void Button1_Click(object sender, EventArgs e) {

    GridViewRow[] rowArray = new GridViewRow[gv_Files.Rows.Count];
    gv_Files.Rows.CopyTo(rowArray, 0);

    int i = -1;

    foreach(GridViewRow row in rowArray)
    {
        i++;
        string _serviceRequestID = ((Label)gv_Files.Rows[row.RowIndex].FindControl("lbl_SRID")).Text;
        string _vesselName = ((Label)gv_Files.Rows[row.RowIndex].FindControl("lbl_VesselID")).Text;
        string _uploadDIR = Server.MapPath("uploadedFiles");
        string _vesselDIR = Server.MapPath("uploadedFiles" + "\\" + _vesselName);
        string _fileName = ((Label)gv_Files.Rows[row.RowIndex].FindControl("lbl_FileName")).Text;
        DirectoryInfo dInfo = new DirectoryInfo(_uploadDIR);
        DirectoryInfo dVessel = new DirectoryInfo(_vesselDIR);
        DirectoryInfo dSRID = new DirectoryInfo(_serviceRequestID);
        dInfo.CreateSubdirectory(_vesselName);
        dVessel.CreateSubdirectory(_serviceRequestID);

        string _originalFile = _uploadDIR + "\\" + _fileName;
        string _fileFullPath = Path.Combine(Server.MapPath("uploadedFiles/" + _vesselName + "/" + _serviceRequestID + "/"), _fileName);
        FileInfo NewFile = new FileInfo(_fileFullPath);
        string _fileUploadPath = _vesselName + "/" + _serviceRequestID + "/" + _fileName;
        string sourceFile = _originalFile;
        FileInfo _source = new FileInfo(sourceFile);
        string destinationFile = _fileFullPath;

            try
            {
                {
                    File.Move(sourceFile, destinationFile);
                    movefiles.InsertNewUploadPath(Convert.ToDecimal(_serviceRequestID), 1, _fileUploadPath);
                }
            }
            catch (Exception ex)
            {
                CreateLogFiles Err = new CreateLogFiles();
                Err.ErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);

            }
    }

    _utility.MessageBox("Completed processing files.");
}
A: 

As long as the error encountered occurs within the try catch clause, the code will continue executing within the foreach loop. However, if the error occurs outside of the try catch, the function will exit and throw an error. How many files does your error log report??

DJ Quimby
I finally got a good read on the log. I decided to move the service requests first, move the log created by that process, and then move the invoices. There are 224 file cannot be found errors reported which would mean they are missing from the directory even though nobody has access to that folder outside of the server.
Infotech
Users seem to be uploading the same file for the invoice and the service request sometimes. However, the original upload code includes an autonumber generator that adds the generated number to the file name. It appears that it isn't doing it in some cases. Guess I'm going to have to check it out a bit closer to see what's going on. This could be a tough problem.
Infotech
I got everything working like it should - zero errors in the log so I'm fairly certain it's copying every file that it can. However, I'm still coming up with a difference in the number of files I should have left over. As a matter of fact, it's the same number I was getting yesterday. I created a gridview to see the file names in the directory. Is there an easy way to compare those files with the file names saved in the database?
Infotech
When you say database do you mean an actual database such as MySQL, or your original directory? Just want to make sure I understand what you're asking.
DJ Quimby