Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Only look in subfolders..

Only look in subfolders..

Scheduled Pinned Locked Moved C#
question
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andredani
    wrote on last edited by
    #1

    hi how do i change to only look in the path´s subfolders, and move them to (dest)?? And when my program are finished how do i convert my project to a program, witch program do u use?? heres my code: string[] lines = System.IO.File.ReadAllLines("dataapplikationer.txt"); string appl = lines[1]; string path = lines[2]; string sort = lines[5]; string dest = lines[3]; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path); { int filesCouunt = dir.GetFiles().Length; string name = dir.Name; DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, name, time.ToString(), "Summa tid", filesCouunt.ToString(), "av:", appl }); this.listView2.Items.Add(item); this.listView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); //thanks;)

    L 1 Reply Last reply
    0
    • A andredani

      hi how do i change to only look in the path´s subfolders, and move them to (dest)?? And when my program are finished how do i convert my project to a program, witch program do u use?? heres my code: string[] lines = System.IO.File.ReadAllLines("dataapplikationer.txt"); string appl = lines[1]; string path = lines[2]; string sort = lines[5]; string dest = lines[3]; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path); { int filesCouunt = dir.GetFiles().Length; string name = dir.Name; DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, name, time.ToString(), "Summa tid", filesCouunt.ToString(), "av:", appl }); this.listView2.Items.Add(item); this.listView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); //thanks;)

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      As far as I understand your post, you want to use dir.GetDirectories method. To move folder u've to recursively move it's content to a new location and then delete old, empty directory using Delete or DeleteDirectory method. Please correct me if I'm wrong.

      Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

      A 1 Reply Last reply
      0
      • L Lutoslaw

        As far as I understand your post, you want to use dir.GetDirectories method. To move folder u've to recursively move it's content to a new location and then delete old, empty directory using Delete or DeleteDirectory method. Please correct me if I'm wrong.

        Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

        A Offline
        A Offline
        andredani
        wrote on last edited by
        #3

        I´m trying to tell the path that there are only subfolder that it gonna countfiles and pass subfolder name insted for path name and path filescount... my code works but i want to change that it only will look in subfolders.. And i think my Debug are broke beacause i get error when i use Directory.MoveTo(sub, dest) strange.. says that this file allready exists in dest.. Thanks..!;P

        L 1 Reply Last reply
        0
        • A andredani

          I´m trying to tell the path that there are only subfolder that it gonna countfiles and pass subfolder name insted for path name and path filescount... my code works but i want to change that it only will look in subfolders.. And i think my Debug are broke beacause i get error when i use Directory.MoveTo(sub, dest) strange.. says that this file allready exists in dest.. Thanks..!;P

          L Offline
          L Offline
          Lutoslaw
          wrote on last edited by
          #4

          The number of files contained in all subdirectories of a given folder?

          string path = @"C:\";
          string[] subDirs = System.IO.Directory.GetDirectories(path);
          int count = 0;
          for (int i = 0; i < subDirs.Length; i++)
          count += System.IO.Directory.GetFiles(subDirs[i]).Length;
          // Do sth with 'count'.

          Please improve your English -- it was very hard for me to understand you.

          Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

          A 1 Reply Last reply
          0
          • L Lutoslaw

            The number of files contained in all subdirectories of a given folder?

            string path = @"C:\";
            string[] subDirs = System.IO.Directory.GetDirectories(path);
            int count = 0;
            for (int i = 0; i < subDirs.Length; i++)
            count += System.IO.Directory.GetFiles(subDirs[i]).Length;
            // Do sth with 'count'.

            Please improve your English -- it was very hard for me to understand you.

            Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

            A Offline
            A Offline
            andredani
            wrote on last edited by
            #5

            ok =) gonna try... Way won´t this work?? System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path); foreach (String sub in Directory.GetDirectories(path)) { foreach (String file in Directory.GetFiles(sub, "*.dat")) { int count = 0; for (int i = 0; i < sub.Length; i++) count += System.IO.Directory.GetFiles(sub[i]).Length;:((:(( //// Here!!!! String subName = sub.Substring(sub.LastIndexOf(@"\") + 1); DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, subName, time.ToString(), "", count.ToString(), "", pris, appl }); this.listView2.Items.Add(item); this.listView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); this.toolStripProgressBar1.Value += 50; sparaform(); } this.toolStripProgressBar1.Value = 0; } Want to getfiles count from subfolder to the path. //thanks man!

            L 1 Reply Last reply
            0
            • A andredani

              ok =) gonna try... Way won´t this work?? System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path); foreach (String sub in Directory.GetDirectories(path)) { foreach (String file in Directory.GetFiles(sub, "*.dat")) { int count = 0; for (int i = 0; i < sub.Length; i++) count += System.IO.Directory.GetFiles(sub[i]).Length;:((:(( //// Here!!!! String subName = sub.Substring(sub.LastIndexOf(@"\") + 1); DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, subName, time.ToString(), "", count.ToString(), "", pris, appl }); this.listView2.Items.Add(item); this.listView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); this.toolStripProgressBar1.Value += 50; sparaform(); } this.toolStripProgressBar1.Value = 0; } Want to getfiles count from subfolder to the path. //thanks man!

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #6

              Please read a documentation of foreach statement in msdn.

              // subDI var. is not used
              // System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path);
              // Testing list:
              List<string> result = new List<string>();
              foreach (String sub in Directory.GetDirectories(path)) {
              // 1. Do not call GetFiles function twice if don't have to!
              // This loop was useless while you never use file variable!
              // foreach (String file in Directory.GetFiles(sub, "*.dat")) {
              // If you declare count inside a loop, it will be initialized down to zero
              // each time. Was it intended?
              int count = 0;
              foreach (string subSub in Directory.GetDirectories(sub))
              count += Directory.GetFiles(subSub).Length;
              // Do not use Substring here!
              // String subName = sub.Substring(sub.LastIndexOf(@"\") + 1);
              string subName = Path.GetFileName(sub);
              // sub is a string, not a string array, so sub[i] will give a i'th CHAR of a string.
              // for (int i = 0; i < sub.Length; i++)
              // this.toolStripProgressBar1.Value += 50;
              // Create ListView item etc.

              // Use some simple code for testing!
              result.Add(subName + ": " + count.ToString() + " files in subdirectories");
              

              }

              -- modified at 8:17 Monday 17th September, 2007

              Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

              A 1 Reply Last reply
              0
              • L Lutoslaw

                Please read a documentation of foreach statement in msdn.

                // subDI var. is not used
                // System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path);
                // Testing list:
                List<string> result = new List<string>();
                foreach (String sub in Directory.GetDirectories(path)) {
                // 1. Do not call GetFiles function twice if don't have to!
                // This loop was useless while you never use file variable!
                // foreach (String file in Directory.GetFiles(sub, "*.dat")) {
                // If you declare count inside a loop, it will be initialized down to zero
                // each time. Was it intended?
                int count = 0;
                foreach (string subSub in Directory.GetDirectories(sub))
                count += Directory.GetFiles(subSub).Length;
                // Do not use Substring here!
                // String subName = sub.Substring(sub.LastIndexOf(@"\") + 1);
                string subName = Path.GetFileName(sub);
                // sub is a string, not a string array, so sub[i] will give a i'th CHAR of a string.
                // for (int i = 0; i < sub.Length; i++)
                // this.toolStripProgressBar1.Value += 50;
                // Create ListView item etc.

                // Use some simple code for testing!
                result.Add(subName + ": " + count.ToString() + " files in subdirectories");
                

                }

                -- modified at 8:17 Monday 17th September, 2007

                Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                A Offline
                A Offline
                andredani
                wrote on last edited by
                #7

                tnx man.. but i did like this: System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path); foreach (String sub in Directory.GetDirectories(path)) { foreach (String file in Directory.GetFiles(sub, "*.dat")) { listView2.Items.Clear(); this.toolStripProgressBar1.Value += 10; System.Threading.Thread.Sleep(dimension); int fileCount = System.IO.Directory.GetFiles(sub, "*.*", SearchOption.AllDirectories).Length; String subName = sub.Substring(sub.LastIndexOf(@"\") + 1); DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, subName, time.ToString(), "", fileCount.ToString(), "", pris, appl }); this.listView2.Items.Add(item); ListViewItem item2 = new ListViewItem(new string[] { sort, subName, time.ToString(), "", fileCount.ToString(), "", pris, appl }); this.listView3.Items.Add(item2); works just fine.. yet..=) But i got another problem::(( i start a timer with button1 and want to stop it with button2 i thought it could be like this: private void button1_Click(object sender, EventArgs e) { string[] lines1 = System.IO.File.ReadAllLines("inställningar.txt"); string path1 = lines1[8]; string tid = lines1[0]; int dimension = Convert.ToInt32(tid); Timer oT = new Timer(); oT.Tick += new System.EventHandler(TimerFunction); oT.Interval = dimension; oT.Start(); } private void button2_Click(object sender, EventArgs e) { Timer oT = new Timer(); oT.Tick += new System.EventHandler(Timerfunction); oT.Stop(); } But i where wrong!! How would u do it?? tnx!;)

                L 2 Replies Last reply
                0
                • A andredani

                  tnx man.. but i did like this: System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path); foreach (String sub in Directory.GetDirectories(path)) { foreach (String file in Directory.GetFiles(sub, "*.dat")) { listView2.Items.Clear(); this.toolStripProgressBar1.Value += 10; System.Threading.Thread.Sleep(dimension); int fileCount = System.IO.Directory.GetFiles(sub, "*.*", SearchOption.AllDirectories).Length; String subName = sub.Substring(sub.LastIndexOf(@"\") + 1); DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, subName, time.ToString(), "", fileCount.ToString(), "", pris, appl }); this.listView2.Items.Add(item); ListViewItem item2 = new ListViewItem(new string[] { sort, subName, time.ToString(), "", fileCount.ToString(), "", pris, appl }); this.listView3.Items.Add(item2); works just fine.. yet..=) But i got another problem::(( i start a timer with button1 and want to stop it with button2 i thought it could be like this: private void button1_Click(object sender, EventArgs e) { string[] lines1 = System.IO.File.ReadAllLines("inställningar.txt"); string path1 = lines1[8]; string tid = lines1[0]; int dimension = Convert.ToInt32(tid); Timer oT = new Timer(); oT.Tick += new System.EventHandler(TimerFunction); oT.Interval = dimension; oT.Start(); } private void button2_Click(object sender, EventArgs e) { Timer oT = new Timer(); oT.Tick += new System.EventHandler(Timerfunction); oT.Stop(); } But i where wrong!! How would u do it?? tnx!;)

                  L Offline
                  L Offline
                  Lutoslaw
                  wrote on last edited by
                  #8

                  Sorry for complaining... could you modify your post and enclose code blocks with "pre" tags? e.g.

                  some code

                  ? Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                  1 Reply Last reply
                  0
                  • A andredani

                    tnx man.. but i did like this: System.IO.DirectoryInfo subDI = new System.IO.DirectoryInfo(path); foreach (String sub in Directory.GetDirectories(path)) { foreach (String file in Directory.GetFiles(sub, "*.dat")) { listView2.Items.Clear(); this.toolStripProgressBar1.Value += 10; System.Threading.Thread.Sleep(dimension); int fileCount = System.IO.Directory.GetFiles(sub, "*.*", SearchOption.AllDirectories).Length; String subName = sub.Substring(sub.LastIndexOf(@"\") + 1); DateTime time = DateTime.Now; ListViewItem item = new ListViewItem(new string[] { sort, subName, time.ToString(), "", fileCount.ToString(), "", pris, appl }); this.listView2.Items.Add(item); ListViewItem item2 = new ListViewItem(new string[] { sort, subName, time.ToString(), "", fileCount.ToString(), "", pris, appl }); this.listView3.Items.Add(item2); works just fine.. yet..=) But i got another problem::(( i start a timer with button1 and want to stop it with button2 i thought it could be like this: private void button1_Click(object sender, EventArgs e) { string[] lines1 = System.IO.File.ReadAllLines("inställningar.txt"); string path1 = lines1[8]; string tid = lines1[0]; int dimension = Convert.ToInt32(tid); Timer oT = new Timer(); oT.Tick += new System.EventHandler(TimerFunction); oT.Interval = dimension; oT.Start(); } private void button2_Click(object sender, EventArgs e) { Timer oT = new Timer(); oT.Tick += new System.EventHandler(Timerfunction); oT.Stop(); } But i where wrong!! How would u do it?? tnx!;)

                    L Offline
                    L Offline
                    Lutoslaw
                    wrote on last edited by
                    #9

                    You should revise your knowledge about variable visibility and their life time.

                    void A()
                    {
                    string str = new string();
                    str = "text";
                    // At the end of the method str dies.
                    }

                    void B()
                    {
                    string str = new string(); // allocates a new variable regardles the name is the same.
                    Console.WriteLine(str); // str is equal to "" here.
                    }

                    To stop a timer you must not declare it as a local variable.

                    Timer oT = new Timer();
                    private void button1_Click(object sender, EventArgs e)
                    {
                    string[] lines1 = System.IO.File.ReadAllLines("inställningar.txt");
                    string path1 = lines1[8];
                    string tid = lines1[0];
                    int dimension = Convert.ToInt32(tid);
                    //Timer oT = new Timer();
                    oT.Interval = dimension;
                    oT.Start();
                    }

                    private void button2_Click(object sender, EventArgs e)
                    {
                    //Timer oT = new Timer();
                    // Did you mean "-="?
                    //oT.Tick += new System.EventHandler(Timerfunction);
                    oT.Tick -= TimerFunction;
                    oT.Stop();
                    }

                    Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups