none
Two Loops RRS feed

  • Question

  • My desired output is to have 4 folders created (Eastern, Western, Southern, Northern) and 5 files created (Mary.txt, James.txt, Jack.txt, Bill.txt, Jill.txt) with each text

    file saved in it's respective zone.  Meaning the directory structure would be:

    Eastern\Mary.txt

    Eastern\James.txt

    Western\Jack.txt

    Southern\Bill.txt

    Northern\Jill.txt

    How would I code this to happen?

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("Zone", typeof(string));
    table.Rows.Add("Mary", "Eastern";
    table.Rows.Add("James", "Eastern");
    table.Rows.Add("Jack", "Western");
    table.Rows.Add("Bill", "Southern");
    table.Rows.Add("Jill", "Northern");
    
    var names = table
    			  .AsEnumerable()
    			  .Select(x => x.Field<string>("Name"))
    			  .Distinct()
    			  .ToList();
    
    var zones = table
    			.AsEnumerable()
    			.Select(x => x.Field<string>("Zone"))
    			.Distinct()
    			.ToList();
    			
    foreach (string n in names)
    {
    	foreach (string z in zones)
    	{
    		System.IO.Directory.CreateDirectory(z);
    		System.IO.File.Create(n + ".txt");
    	}
    }

    Monday, November 25, 2019 2:48 AM

Answers

  • Hi IndigoMontoya, 

    Thank you for posting here.

    As Ante Meridian suggested, one loop can solve the problem.

    I make a test on my side, and here’s my code.

            DataTable table = new DataTable();
            private void button1_Click(object sender, EventArgs e)
            {
                foreach (DataRow row in table.Rows)
                {
                    createByString(row.ItemArray[1] as string, row.ItemArray[0] as string);
                }
            }
            static void createByString(string col1,string col2)
            {
                string pathCol1 = "D:\\TestFile2\\" + col1;
                if (!Directory.Exists(pathCol1))
                {
                    Directory.CreateDirectory(pathCol1);
                }
                string pathCol2 = pathCol1 + "\\" + col2 + ".txt";
                if (!File.Exists(pathCol2))
                {
                    File.Create(pathCol2);
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Zone", typeof(string));
                table.Rows.Add("Mary", "Eastern");
                table.Rows.Add("James", "Eastern");
                table.Rows.Add("Jack", "Western");
                table.Rows.Add("Bill", "Southern");
                table.Rows.Add("Jill", "Northern");
            }
    

    Result of my test.

          

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by IndigoMontoya Monday, November 25, 2019 1:53 PM
    Monday, November 25, 2019 6:32 AM
    Moderator

All replies

  • Greetings Indigo.

    I think you only need one loop.

          public static void Main(string[] args)
          {
             DataTable table = new DataTable();
             table.Columns.Add("Name", typeof(string));
             table.Columns.Add("Zone", typeof(string));
             table.Rows.Add("Mary", "Eastern");
             table.Rows.Add("James", "Eastern");
             table.Rows.Add("Jack", "Western");
             table.Rows.Add("Bill", "Southern");
             table.Rows.Add("Jill", "Northern");
    
             foreach (DataRow row in table.Rows)
             {
                MakeFile(row.ItemArray[0] as string, row.ItemArray[1] as string);
             }
          }
    
          // Create the given filename in the given folder.
          static void MakeFile(string fileName, string folderName)
          {
             MakeFolder(folderName);
    
             // Create the file here.
          }
    
          // If the given folder doesn't exist, create it.
          static void MakeFolder(string folder)
          {
             // Create the folder here (if it doesn't already exist).
          }
    

    Monday, November 25, 2019 3:13 AM
  • Hi IndigoMontoya, 

    Thank you for posting here.

    As Ante Meridian suggested, one loop can solve the problem.

    I make a test on my side, and here’s my code.

            DataTable table = new DataTable();
            private void button1_Click(object sender, EventArgs e)
            {
                foreach (DataRow row in table.Rows)
                {
                    createByString(row.ItemArray[1] as string, row.ItemArray[0] as string);
                }
            }
            static void createByString(string col1,string col2)
            {
                string pathCol1 = "D:\\TestFile2\\" + col1;
                if (!Directory.Exists(pathCol1))
                {
                    Directory.CreateDirectory(pathCol1);
                }
                string pathCol2 = pathCol1 + "\\" + col2 + ".txt";
                if (!File.Exists(pathCol2))
                {
                    File.Create(pathCol2);
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Zone", typeof(string));
                table.Rows.Add("Mary", "Eastern");
                table.Rows.Add("James", "Eastern");
                table.Rows.Add("Jack", "Western");
                table.Rows.Add("Bill", "Southern");
                table.Rows.Add("Jill", "Northern");
            }
    

    Result of my test.

          

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by IndigoMontoya Monday, November 25, 2019 1:53 PM
    Monday, November 25, 2019 6:32 AM
    Moderator