0

Existing App: I have a C# app that runs a search and fetches matching lead files and throws them into a specificied directory.

Adding functionality to: have the system check to see if a subdirectory exists for the file's zip code and if it doesn't exist create it, then i want the file transfered into that respective directory.

Problem: Right now, none of the files are transferring and none of the subdirectories are being created. Nothing happens. Is it a syntax issue or am I calling it wrong???

Please help!

Here's what I added:

            string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP;
            // If the zip code subdirectory doesn't exist, create it.

            if (!Directory.Exists(targetzipdir))
            {
                 Directory.CreateDirectory(targetzipdir);
            } 

         private void TransferFile(string sourceDir, string filename, string ZIP)     
         { 
            string targetFileAndPath = m_sc.get_TargetPath() + "\\" + ZIP + "\\" + fullFileName;

Here's more of the code in the SearchProcess.cs file for the bigger picture (with some stuff left off):

    public void Run()
    {
        m_sc = (SearchCriteria)m_form.Invoke(m_form.m_DelegateGetSearchCriteria);
        // Display parameters
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Corp: " + m_sc.get_Corp() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on OrderNumber: " + m_sc.get_OrderNumber() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Campaign: " + m_sc.get_Campaign() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on City: " + m_sc.get_City() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on State: " + m_sc.get_State() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Zip: " + m_sc.get_Zip() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Source Path: " + m_sc.get_SourcePath() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Target Path: " + m_sc.get_TargetPath() });

        DirSearch(m_sc.get_SourcePath());

        // Make asynchronous call to main form
        // to inform it that thread finished
        m_form.Invoke(m_form.m_DelegateThreadFinished, null);
    }

            for (int colNum = 0; colNum < expectedTypes.Count; colNum++)
            {
            if (m_sc.get_SearchOR().Equals(true))
                // Check for the Zip match
                if (m_sc.get_Zip() != "" && ZIP.Contains(m_sc.get_Zip()) == true)
                {
                    found = true;

                    string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP;
                    // If the zip code subdirectory doesn't exist, create it.

                    if (!Directory.Exists(targetzipdir))
                    {
                        Directory.CreateDirectory(targetzipdir);
                    } 

                }     // ending if (m_sc.get_SearchOR().Equals(true))
            }         //ending for loop  

    private void TransferFile(string sourceDir, string filename, string ZIP)
    {
        string fullFileName = filename + ZIP + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;

        //copy matching source file into the specified subdirectory based on zip
        string targetFileAndPath = m_sc.get_TargetPath() + "\\" + ZIP + "\\" + fullFileName;
        // Copy the file if the source file exists
        if (File.Exists(fullFileNameAndPath))
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"COPYING FROM: " + fullFileNameAndPath});
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"TO: " + targetFileAndPath});
            // Do the copy, overrite the target file if it exists
            File.Copy(fullFileNameAndPath, targetFileAndPath, true);
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"Source file does not exist: " + fullFileNameAndPath});
        }
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] {""});
    }

Thanks for looking! Any ideas would be greatly appreciated! :)

Brian McCarthy
  • 4,658
  • 16
  • 49
  • 66
  • 3
    Can debug and trace the code? Are there any error messages? Where does the code appear to not be working? – mellamokb Feb 24 '11 at 15:02
  • (Advice) : Dont use multiple invokes. Invoke is expensive operation. Instead of multiple invokes use one – Stecya Feb 24 '11 at 15:06
  • There are no errors or warnings in the code. The problem is no files are transferred and no directories are created. Here's the part i added: string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP; // If the zip code subdirectory doesn't exist, create it. if (!Directory.Exists(targetzipdir)) { Directory.CreateDirectory(targetzipdir); } – Brian McCarthy Feb 24 '11 at 15:06
  • Can you show samples of targetzipdir wich was generated? – Stecya Feb 24 '11 at 15:09
  • No subdirectories were created in the target directory but the name of each subdirectory should be the same as the value of the zip code column in the .csv excel file. Examples of directories would be: 32563-8906, 95330-9525, 97530-9439 – Brian McCarthy Feb 24 '11 at 15:17
  • var info = Directory.CreateDirectory(targetzipdir); Console.WriteLine(info.FullName); add this code to see path where files directories are created – Stecya Feb 24 '11 at 15:33
  • 1
    Needs more details. Either give us a testcase to run or a specific failure description (runs until X, values are .., I expect...). And please use Path.Combine(). – Benjamin Podszun Feb 24 '11 at 15:35
  • is the string declared correctly? - string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP - Perhaps I'm not pulling in the Zip Code value correctly for each file? – Brian McCarthy Feb 24 '11 at 16:03
  • try string targetzipdir = Path.Combine(m_sc.get_TargetPath(),Zip). But still see my previous comment and check saving destination – Stecya Feb 24 '11 at 16:12

1 Answers1

0

I found the problem! I had the code for creating the directory in the wrong place. It was under the If statement for Zip Code Matching and would only execute if the zip code was a match and the zip code is an optional field in the search. I moved all the code under the TransferFile Method and it worked fine. Also the data's source directory had to be pointing to C:\Documents and Settings\bmccarthy\Desktop\sourcedir AND the files had to be in a special format under a subdirectory of that directory at C:\Documents and Settings\bmccarthy\Desktop\sourcedir\AMLDailyHotLeads20101001

Here's the rewritten code:

            // Copy the file if ANY of the search criteria have been met
            if (found)
            {

                m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"FOUND: Order_No: " + Order_No +
                                                                        " barcode: " + barcode +
                                                                        " MailerCode: " + MailerCode +
                                                                        " AgentID: " + AgentID +
                                                                        " City: " + City +
                                                                        " State: " + State +
                                                                        " ZIP: " + ZIP});
                //passes values to TransferFile 
                TransferFile(directory, barcode, AgentID, ZIP);
            }
        } // end for that finds each matching record 

    }

    // find and copy the file to the target directory string ZIP
    private void TransferFile(string sourceDir, string filename, string AgentID, string ZIP)
    {
        string fullFileName = filename + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;
        string targetFileAndPath;

            // adds the given lead's agentid and zip code to the targetpath string 
            string targetzipdir = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP;

            // If the given lead's zip code subdirectory doesn't exist, create it.
            if (!Directory.Exists(targetzipdir))
            {
                Directory.CreateDirectory(targetzipdir);
            }

            targetFileAndPath = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP + "\\" + fullFileName;

        // Copy the file if the source file exists
        if (File.Exists(fullFileNameAndPath))
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"COPYING FROM: " + fullFileNameAndPath});
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"TO: " + targetFileAndPath});

            // Copy the file and over-write the target file if it exists
            File.Copy(fullFileNameAndPath, targetFileAndPath, true);
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"Source file does not exist: " + fullFileNameAndPath});
        }
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] {""});
    }
Brian McCarthy
  • 4,658
  • 16
  • 49
  • 66