7 Replies - 441 Views - Last Post: 08 June 2012 - 11:00 AM Rate Topic: -----

#1 JimmyJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-June 12

Deletion program problem

Posted 08 June 2012 - 09:21 AM

I'm new to programming and I'm trying to use this to delete all .htm files in a couple of directories I have by using recursion. So far it works fine with just one folder, but I haven't been able to find a way to add more than one folder to the code. Is there any way i can add more directories to the directory path so I don't have to keep changing the code every time I want it to delete files in another directory? Apologies if this is a dumb question..
namespace ConsoleApplication7
{
    class Deleter
    {
        static void Main(string[] args)
        {
            string directorypath = @"c:\Public\";
            string[] directories = System.IO.Directory.GetDirectories(directorypath);
            DeleteDirectories(directories);
        }
        private static void DeleteDirectories(string[] directories)
        {
              foreach (var directory in directories)
                {
                    string[] files = System.IO.Directory.GetFiles(directory, "*.htm");
                    DeleteFiles(files);
                    var subDirectories = System.IO.Directory.GetDirectories(directory);
                    DeleteDirectories(directories);
                }
        }
        private static void DeleteFiles(string[] files)
        {
            foreach (string file in files)
            {
                FileInfo f = new FileInfo(file);
                if (f.CreationTime < DateTime.Now)
                    f.Delete();
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Deletion program problem

#2 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Deletion program problem

Posted 08 June 2012 - 09:35 AM

Add a feature to your Console application that it first requests the directory that it will sweep. So whenever you start it, it will request a new directory which you will provide.

What you could also do is: if the user inputted something with a ',' (if the user inputted multiple directories) then split the input string on ',' characters and use the strings gotten by doing that as the directories you need to sweep.

Why not switch it to a WPF or WinForms app? :)

This post has been edited by RexGrammer: 08 June 2012 - 09:36 AM

Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: Deletion program problem

Posted 08 June 2012 - 09:36 AM

18 DeleteDirectories(directories);

Don't you want to send the subdirectories to the method? You just keep sending the same root level to this method over and over.

I also cleaned up the code a bit so everything wasn't static and made proper use of instantiating your deleter class

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
    internal class Program
    {
            private static void Main(string[] args)
            {
                string directorypath = @"c:\testdelete\";
                string[] directories = System.IO.Directory.GetDirectories(directorypath);
                Deleter myDeleter = new Deleter();
                myDeleter.DeleteDirectories(directories);
            }

        private class Deleter
        {

            public  void DeleteDirectories(string[] directories)
            {
                foreach (var directory in directories)
                {
                    string[] files = System.IO.Directory.GetFiles(directory, "*.htm");
                    DeleteFiles(files);
                    var subDirectories = System.IO.Directory.GetDirectories(directory);
                    DeleteDirectories(subDirectories);
                }
            }

            public  void DeleteFiles(string[] files)
            {
                foreach (string file in files)
                {
                    FileInfo f = new FileInfo(file);
                    if (f.CreationTime < DateTime.Now)
                        f.Delete();
                }
            }
        }
    }
}


Was This Post Helpful? 2
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: Deletion program problem

Posted 08 June 2012 - 09:41 AM

Rex and Ryan: I hid your responses because neither of them actually addressed the OP's question of how to fix his recursive logic.

Shoving a load of directories into the method doesn't fix the method and doesn't make it smart and dynamic: It requires knowing all the paths up front.

Switching to WPF or Winforms doesn't resolve the bug in the logic. Console apps are just fine for this sort of thing, especially when one is trying to build a .dll of commonly used functions like delete a tree of files and so on.

I just wanted you to know why your answers suddenly disappeared.
Was This Post Helpful? 0
  • +
  • -

#5 JimmyJ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-June 12

Re: Deletion program problem

Posted 08 June 2012 - 09:46 AM

That is a brilliant idea, thank you rex! As for as WPF or WinForms, i'm not very familiar with the different style of coding that goes into those, I just wanted to get a solid console program first.

tlhIn, I would have never thought of that, it makes way more sense than having a user input like I was just adding. Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: Deletion program problem

Posted 08 June 2012 - 10:02 AM

I stand correctly. I assumed the OP recognized the obviousness of having the user enter the starting path. I thought the path was just there for confirming the delete class operation. It never occurred to me that the hard-coded path was ever intended for actual release/operation. - Message un-hide.
Was This Post Helpful? 0
  • +
  • -

#7 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Deletion program problem

Posted 08 June 2012 - 10:47 AM

I didn't mean to switch to WinForms or to WPF because of the logic, but because it can be really easier to select directories with a GUI, and therefore the UX would be considerably better :)

Oh and be sure to make a helper class that does this (it's more OO), just like tlhIn`toq already stated.

This post has been edited by RexGrammer: 08 June 2012 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: Deletion program problem

Posted 08 June 2012 - 11:00 AM

Not to belittle the suggestion, but yeah, duh. A good GUI is a lot nicer than a console window. That's why we don't code in DOS anymore.

I just thought the OP was doing this as a tiny little test rig to see if their delete class was working. Doing what the OP did takes about 3 minutes: Its a fast test platform. And in no way ties the class to the GUI which is a good thing.

I've done test rigs like this but in GUI form and find myself spending hours on all the GUI behaviors that don't really test the class. It can be easy to get side tracked with a GUI for things like handling the .TextChanged event, the font for the path, making WPF behave, blah blah. I won't admit how many times my 5 minute test rigs took until lunch time.

So I mistakenly thought this was just for a test rig for the recursive logic.

I'm going to bow out now before I offend anyone else.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1