none
how to insert separator in array list RRS feed

  • Question

  • Hi All,

    here's my code, I want to have result such as:

    @"C:\test\1.xlsx",@"C:\test\2.xlsx",@"C:\test\3.xlsx",...

    but all I know is, my code was return only @"C:\test\1.xlsx"

    does any one can help me with it?does my code was wrong?i'll appreciated you help.

    foreach

    (stringd inDirectory.GetDirectories(extractDirPath))

                            {

                               

    //MessageBox.Show(d.ToString());


                               

    DirectoryInfosf = newDirectoryInfo(d);

                                            

                               

    string[] entries = Directory.GetFileSystemEntries(d, "*.xlsx", SearchOption.AllDirectories);

                               

    for(inti = 0; i < entries.Length;i++)

                                {

    stringcat = string.Join(",", entries[i]);

                                    System.IO.

    File.WriteAllText(extractDirPath + "\\list1.txt", String.Join(",", string.Format("@"+ "\""+ entries[i] + "\"", i)));

                                }

    Best Regards,

    Bambang

    Monday, April 4, 2016 4:05 AM

Answers

  • If you want to append the list of existing files, then replace the for loop with this fragment:

    string cat = string.Join( ", ", entries );

    File.AppendAllText( Path.Combine( extractDirPath, "list1.txt" ), cat + Environment.NewLine );

    But if you want to generate new names based on i variable, or to include ‘@’ and ‘”’, then give more details.


    Monday, April 4, 2016 5:43 AM
  • Hi Bambang Priantoro,

    Remove the for loop would be fine.

    foreach (string d in Directory.GetDirectories(extractDirPath))
    {
        DirectoryInfo sf = new DirectoryInfo(d);
    
        string[] entries = Directory.GetFileSystemEntries(d, "*.xlsx", SearchOption.AllDirectories);
    
        //string cat = string.Join(",", entries[i]);
        string result = string.Join(",", entries.Select(s => "@" + "\"" + s + "\""));
        //System.IO.File.WriteAllText(extractDirPath + "\\list1.txt", String.Join(",", string.Format("@" + "\"" + entries[i] + "\"", i)));
        System.IO.File.AppendAllText(extractDirPath + "\\list1.txt", result);
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 4, 2016 6:28 AM
    Moderator

All replies

  • If you want to append the list of existing files, then replace the for loop with this fragment:

    string cat = string.Join( ", ", entries );

    File.AppendAllText( Path.Combine( extractDirPath, "list1.txt" ), cat + Environment.NewLine );

    But if you want to generate new names based on i variable, or to include ‘@’ and ‘”’, then give more details.


    Monday, April 4, 2016 5:43 AM
  • Hi Bambang Priantoro,

    If you want to set the format of file path, you could use Select method and Lambda expression.

    string result = string.Join(",", entries.Select(s => "@" + "\"" + s + "\""));
    System.IO.File.AppendAllText(extractDirPath + "\\list1.txt", result);
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 4, 2016 6:03 AM
    Moderator
  • Hi Li wang,

    Thank you for your quick and answer.

    but I have small problem, after I implement your code. the result was occurred twice.

    so when I have implement like this:

    foreach (string d in Directory.GetDirectories(extractDirPath))
                            {
                                                            DirectoryInfo sf = new DirectoryInfo(d);
    
                                string[] entries = Directory.GetFileSystemEntries(d, "*.xlsx", SearchOption.AllDirectories);
                                for (int i = 0; i < entries.Length; i++)
                                {
                                    //string cat = string.Join(",", entries[i]);
                                    string result = string.Join(",", entries.Select(s => "@" + "\"" + s + "\""));
                                    //System.IO.File.WriteAllText(extractDirPath + "\\list1.txt", String.Join(",", string.Format("@" + "\"" + entries[i] + "\"", i)));
                                    System.IO.File.AppendAllText(extractDirPath + "\\list1.txt", result);
                                }

    the result on List1.txt was like this:

    @"D:\testing\un\1\1\1.xlsx",@"D:\testing\un\1\1\2.xlsx"@"D:\testing\un\1\1\1.xlsx",@"D:\testing\un\1\1\2.xlsx"

    the same path was occurred twice, and without separator comma in between.

    Best Regards,

    Bambang

    Monday, April 4, 2016 6:21 AM
  • Hi Viorel,

    Thank you for your help.

    the result as I have expected.

    thank you so much.

    Best Regards,

    Bambang

    Monday, April 4, 2016 6:26 AM
  • Hi Bambang Priantoro,

    Remove the for loop would be fine.

    foreach (string d in Directory.GetDirectories(extractDirPath))
    {
        DirectoryInfo sf = new DirectoryInfo(d);
    
        string[] entries = Directory.GetFileSystemEntries(d, "*.xlsx", SearchOption.AllDirectories);
    
        //string cat = string.Join(",", entries[i]);
        string result = string.Join(",", entries.Select(s => "@" + "\"" + s + "\""));
        //System.IO.File.WriteAllText(extractDirPath + "\\list1.txt", String.Join(",", string.Format("@" + "\"" + entries[i] + "\"", i)));
        System.IO.File.AppendAllText(extractDirPath + "\\list1.txt", result);
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 4, 2016 6:28 AM
    Moderator
  • Hi LI Wang,

    Thank you. for your response, and now I have my result as I expected.

    cheers,

    Bambang

    Monday, April 4, 2016 6:33 AM