Question Deleting directories and subdirectories

Gomo

Member
Joined
Mar 9, 2017
Messages
7
Programming Experience
Beginner
Hello there guys,

I've been trying to figure out a way to delete all folders & sub-folders which are older than "XX" amount of days .. but without luck. I've googled my ass off and still cannot seem to get it working. I'm still new at C# so hence why I'm here, asking for your help!

Here are my attempts:
C#:
                if (checkBox2.Checked == true)
                {
                    DirectoryInfo backups = new DirectoryInfo(Backup);
                    foreach (FileInfo file in backups.GetFiles())
                        if (file.LastWriteTime < DateTime.Now.AddDays(-30))
                            file.Delete();
                }
.. I guess this one ^ wouldn't even work the way I wanted it, because it deletes only files?
C#:
                if (checkBox2.Checked == true)
                {
                    string[] backups = Directory.GetFiles(Backup);

                    foreach (string folders in backups)
                    {
                        DirectoryInfo fi = new DirectoryInfo(folders);
                        if (fi.LastAccessTime < DateTime.Now.AddDays(-30))
                            fi.Delete();
                    }
                }
And for this one, I have no clue why it doesn't work ..

You help would be appreciated! Thanks in advance!

-kind regards-
Gomo
 
As JohnH has shown, you can find useful and relevant information in the documentation. Maybe you should read it. VS has a Help menu for a reason. We can certainly help with the difficult stuff but you can answer this for yourself in about 30 seconds using the Help.
 
Okay .. gotcha, and what should I use instead? .. for deleting non-empty directories?
I did post that help topic link for a reason, did you open it?
 
I did post that help topic link for a reason, did you open it?

Of course I did .. I'm replying right now because I had to go yesterday. And guys, you have to understand that not everyone is really into programming and willing to spend large amounts of time on it. I get that you don't want to give out solutions to "lazy people", but some of us are doing this because are simply forced and have no other option left. I'm not saying that learning a programming language is a bad thing, because it's not .. it just takes time. And yes, I could've paid someone to do this for me but why should I? I have few hours here and there that I can afford to invest in programming and also found forums where help was advertised, so why not do it this way? Anyways .. thanks for the help, I figured it out!
 
Noone's asking you to spend large amounts of time on it. It would have taken you less time to use the Help menu and the documentation than it would to type your question here and then wait for an answer. If you actually did follow the link JohnH provided then you already have your answer, so why did you post the question, never mind imply that we should realise that you've already read the information that provides the answer. I can't speak for everyone but I don't come here so that I can do people's work for them. I come here to help them become better developers. If you're not interested in that then that's fine but I'm not going to spend time doing your work when I could be doing mine or helping other people. If you do want to get better then you need to read the information at your disposal and use it. If you still have an issue after that then we can still help but, speaking for myself at least, I would expect a demonstration of your effort, which generally means posting the code you used, a description of what happened when you used that code and an explanation of how that differs from your expectation.
 
If you still have an issue after that then we can still help but, speaking for myself at least, I would expect a demonstration of your effort, which generally means posting the code you used, a description of what happened when you used that code and an explanation of how that differs from your expectation.

The code I wrote now works the way I wanted it to.
C#:
if (checkBox2.Checked == true && Selection2.Value > 0)
                {
                    double backupAge = Convert.ToDouble(Selection2.Value) * -1;
                    DirectoryInfo di = new DirectoryInfo(Backup);
                    if (di.LastWriteTime < DateTime.Now.AddDays(backupAge))
                    {
                        foreach (DirectoryInfo dir in di.GetDirectories())
                        {
                            dir.Delete(true);
                        }
                        foreach (FileInfo file in di.GetFiles())
                        {
                            file.Delete();
                        }
                    }
                }
It deletes all files and folders in a selected directory which are older than "Selection2.Value" -> user input. But now, I've encountered another problem, .. I'd like to open minimized (running in background) CMD.exe and make that window active / selected. This is the code I'm using:
C#:
using System.Runtime.InteropServices;
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        public void MakeWindowActive(string processName)
        {
            Process[] p = Process.GetProcessesByName(processName);
            if (p.Count() > 0)
                SetForegroundWindow(p[0].MainWindowHandle);
        }
MakeWindowActive("cmd");

But it doesn't seem to work .. any suggestions? (I did google for solutions, noone of them worked)

-kind regards-
Gomo
 
This is the topic of this thread as stated by you:
Deleting directories and subdirectories
This is the topic of your new issue:
I'd like to open minimized (running in background) CMD.exe and make that window active / selected.
Those two things are completely unrelated. We can certainly help with your new issue but it belongs in its own thread with a title that describes the new topic.
 

Latest posts

Back
Top Bottom