Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
AnswerRe: slope of a line Pin
Abhinav S13-Mar-13 6:25
Abhinav S13-Mar-13 6:25 
QuestionPresentation Controller(PPT viewer) Pin
Sachin k Rajput 10-Mar-13 19:30
Sachin k Rajput 10-Mar-13 19:30 
AnswerRe: Presentation Controller(PPT viewer) Pin
DaveyM6910-Mar-13 20:52
professionalDaveyM6910-Mar-13 20:52 
GeneralRe: Presentation Controller(PPT viewer) Pin
Sachin k Rajput 10-Mar-13 21:24
Sachin k Rajput 10-Mar-13 21:24 
GeneralRe: Presentation Controller(PPT viewer) Pin
DaveyM6911-Mar-13 14:01
professionalDaveyM6911-Mar-13 14:01 
QuestionC# foreach for loop Pin
dcof10-Mar-13 11:54
dcof10-Mar-13 11:54 
AnswerRe: C# foreach for loop Pin
SledgeHammer0110-Mar-13 13:24
SledgeHammer0110-Mar-13 13:24 
AnswerRe: C# foreach for loop Pin
Jegan Thiyagesan10-Mar-13 14:03
Jegan Thiyagesan10-Mar-13 14:03 
Hi,
I just refactored your code, it goes through your root dir and look for the sub dirs that you wanted and check for the files you wanted. If you want to change the sub dirs or add new sub dirs, you just need to edit the "SubDirNames" array.
C#
private string[] SubDirNames = new string[]
                                            {
                                                "Cancel",
                                                "ED",
                                                "UI",
                                                "RCS"
                                            };

private List<string> GetSubDirectoryPaths(DirectoryInfo rootDir)
{
    List<string> subDirs = new List<string>();

    foreach (string subDirName in SubDirNames)
    {
        DirectoryInfo[] dirs = rootDir.GetDirectories(subDirName);
        if (dirs.Length > 0)
        {
            subDirs.Add(dirs[0].FullName);
        }
        else
        {
            string path = Path.Combine(rootDir.FullName, subDirName);
            Directory.CreateDirectory(path);
            logging.Error("The location " + path + " does not exist for documents. ");
        }
    }
    return subDirs;
}

private string[] GetFiles(string subDir)
{
    return (from path in Directory.GetFiles(subDir)
            let name = Path.GetFileName(path)
            where name.EndsWith(".pdf") ||
            name.EndsWith(".xlsx") ||
            name.EndsWith(".xls")
            select path).ToArray();
}

public string addNewPKG()
{
    try
    {
        string packageId = "";

        DateTime myDateTime = DateTime.Now.AddMonths(-1);
        string fromDate = "_" + myDateTime.ToString("MMM").ToUpper() + " _" + myDateTime.ToString("yyyy");
        string rootDirPath = Path.Combine(ConfigurationSettings.AppSettings["tLocation"], fromDate);

        DirectoryInfo rootDir = new DirectoryInfo(rootDirPath);
        if (!rootDir.Exists)
        {
            rootDir.Create();
            logging.Error("The location " + rootDirPath + " does not exist for documents. ");
            return packageId;
        }
        else
        {
            List<string> subDirPaths = GetSubDirectoryPaths(rootDir);

            foreach (string subDir in subDirPaths)
            {
                string[] RVWFiles = GetFiles(subDir);

                if (RVWFiles.Length > 0)
                {
                    foreach (string RFile in RVWFiles)
                    {
                        string orgnizationName = "";
                        string contactName = "";
                        string fileNameWithExtension = Path.GetFileName(RFile);
                        string fileName = Path.GetFileNameWithoutExtension(RFile);
                        string Original_location = subDir;

                        string[] names = fileName.Split('_');
                        orgnizationName = names[0].TrimEnd();
                        contactName = names[1].TrimStart();


                        string strOrgnizationName = orgnizationName;

                        string[] items = contactName.TrimEnd().Split(' ');
                        string surname = items[items.Length - 1];
                        //--here does the processing that is required--

                        // you cannot return here, you are still in the loop!
                        //return null;
                    } //closing bracket for  foreach (String RFile in RFiles)
                }
                else
                {
                    logging.Info("packageId: " + packageId + " the location " + subDir + " does not contain any documents. ");
                    return packageId;
                }
            }
        }
    }
    catch (Exception e)
    {
        logging.Error("Error Processing --> " + e.Message);
        logging.Error("************* Stack Trace *******************");
        logging.Error(e.StackTrace);
        // throwing an exception on exception is pointless; either catch the exception and handle it safely or let the compiler to throw the exception.
        //throw new Exception(e.Message);
        return packageId;
    }
    return null;
}


There are few other things, I an not sure about your "PackageId" variable, this is empty at the moment; and why are you returning the packageId when error occurs and returning "null" when success?

Second, don't throw exception on exception, it is pointless, either handle the exception or let the compiler does what it does best.

Third, if i were you, I would move the processing part of the file into a separate method too for better readability.

I hope this helps.

Regards
Jegan
Think! Don't write a line of code unless you absolutely need to.


modified 10-Mar-13 20:22pm.

GeneralRe: C# foreach for loop Pin
dcof10-Mar-13 15:46
dcof10-Mar-13 15:46 
QuestionTYPE OF THE FIELD PASSWORD IN ACCESS Pin
User349010-Mar-13 9:17
User349010-Mar-13 9:17 
AnswerRe: TYPE OF THE FIELD PASSWORD IN ACCESS Pin
PIEBALDconsult10-Mar-13 10:32
mvePIEBALDconsult10-Mar-13 10:32 
GeneralMessage Closed Pin
10-Mar-13 10:50
User349010-Mar-13 10:50 
GeneralRe: TYPE OF THE FIELD PASSWORD IN ACCESS Pin
PIEBALDconsult10-Mar-13 11:02
mvePIEBALDconsult10-Mar-13 11:02 
GeneralRe: TYPE OF THE FIELD PASSWORD IN ACCESS Pin
Pete O'Hanlon10-Mar-13 11:03
mvePete O'Hanlon10-Mar-13 11:03 
QuestionCombobox changing value in datagridview Pin
ManimozhiMuralitharan10-Mar-13 7:13
ManimozhiMuralitharan10-Mar-13 7:13 
QuestionI have string "1999220" how to parse? Pin
Unque10-Mar-13 1:48
Unque10-Mar-13 1:48 
AnswerRe: I have string "1999220" how to parse? Pin
Richard MacCutchan10-Mar-13 2:58
mveRichard MacCutchan10-Mar-13 2:58 
GeneralRe: I have string "1999220" how to parse? Pin
Unque10-Mar-13 4:17
Unque10-Mar-13 4:17 
AnswerRe: I have string "1999220" how to parse? Pin
M-Badger10-Mar-13 3:21
M-Badger10-Mar-13 3:21 
GeneralRe: I have string "1999220" how to parse? Pin
Unque10-Mar-13 4:17
Unque10-Mar-13 4:17 
AnswerRe: I have string "1999220" how to parse? Pin
Abhinav S10-Mar-13 5:26
Abhinav S10-Mar-13 5:26 
AnswerRe: I have string "1999220" how to parse? Pin
dusty_dex10-Mar-13 6:10
dusty_dex10-Mar-13 6:10 
GeneralRe: I have string "1999220" how to parse? Pin
Manfred Rudolf Bihy10-Mar-13 6:23
professionalManfred Rudolf Bihy10-Mar-13 6:23 
Questioncode wave Pin
samir razzak10-Mar-13 0:27
samir razzak10-Mar-13 0:27 
AnswerMy Vote of 1 Pin
Keith Barrow10-Mar-13 0:55
professionalKeith Barrow10-Mar-13 0:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.