Directory.Exists with a wildcard? Can this be done?

Leopardfist

Member
Joined
Nov 25, 2014
Messages
14
Programming Experience
Beginner
Hi, and thank you for assiting me. First off what I need to do, since I am very new to C# and there is almost certainly a better way to accomplish the task I am trying to accomplish.

First, the user will supply a path to a directory. For example: \\server\users\name

Now, I want to delete several folders from thsi directory, IF THEY EXIST. The tricky part is, one of those always has a different name, but that name always starts with ICAClient. Its typically something like ICAClientx8f7s6e0s538fj50 etc etc.

I need to to know if I can do a Directory.Exists(path + "*"); to return the exact filename, so that I can then do a Directory.Delete(newpath, true);

Now, if there is a better way, I am very open to suggestions. Below are the exact folders I need to delete. path variable will be the base path.

path + "CitrixTemp\"
path + "ProfileUnity\"
path + "Cookies\" (I need to delete everything inside this folder EXCEPT the .dat files, as they will be locked due to user being logged in.) not sure of a foreach with this exception.
path + "Application Data\Citrix\"
path + "Application Data\ICAClient\"
path + Application Data\ICAClient*\"
path + Application Data\Sun\"

Those are everything I need to delete in my program when my button is clicked.

THANKS for any assistance, and to every, have a SAFE and HAPPY THANKSGIVING!

Oh Sorry, I am using VS2013 express and writing this little program for Windows XP and Windows 7 boxes.
 
I need to to know if I can do a Directory.Exists(path + "*"); to return the exact filename, so that I can then do a Directory.Delete(newpath, true);
You can easily try this yourself ;) I've tried it with 'c:\program files*' and it always returns false.

I suggest you store the above list of directories in a List<string> or array and loop through them
C#:
for all directories in list
    directorytodelete = path + directory
    if directorytodelete exists, delete directorytodelete
next directory
 
OK, sorry about that. I could not figure out your for all command usage, but I did figure out how to accomplish the task of finding the ever changing filename and deleting it.

C#:
string path = textbox1.text + "\\";
string[] icaclients = Directory.GetDirectories(path, "ICAClient*");
foreach (string item in icaclients)
  {
    Directory.Delete(item, true);
  }

This seems to work, however I am sure there is a much better way to do it lol.

Like I said, I'm very new to the C# Programming thing so sorry if my explanations or questions were dumb.

Thanks for the help!
 
Ah, learned something about GetDirectories(). Thanks

My solution was basically

            string path = tbPath.Text + Path.DirectorySeparatorChar;

            List<string> directories = new List<string>();
            // just samples; add whatever you need
            directories.Add(@"Application Data\Citrix\");
            directories.Add(@"Application Data\ICAClient\");

            for (int cnt = 0; cnt < directories.Count; cnt++)
            {
                string directory = path + directories[cnt];
                if (Directory.Exists(directory))
                    Directory.Delete(directory);
            }

 
Got another question.

How can I most easily accomplish this task.

I want to delete every folder inside a directory, EXCEPT these fodlers:
i. Access (contains macros)
ii. Excel (contains macros)
iii. Internet Explorer (contains background files & Quick Launch shortcuts)
iv. Outlook (contains config files & autocomplete files)
v. Signatures (contains email signatures)
vi. UProof (contains personal dictionary words)

I'm trying to figure out the easiest way to accomplish this. My best idea is to do the following example.

C#:
string[] dirs = Directory.GetDirectories(path);
foreach (string item in dirs)
If (item == path + "Access")
  Break;
else if (item == path + "Excel")
  Break;
else if (item == path + "Internet Explorer")
  Break;
etc. etc. etc. for the next 3 additional folders, and finally
else
directory.delete(item, true);

I am positive there is a way to do this in 3 lines or less lol, I'm just to new to programming to figure it out.
 
You use a break; that will not work as you will jump out of the foreach instead of processing the next directory. You can use continue to skip the rest of the loop and start the next iteration.

Further look at lists to store what you do not want to delete; the list has a method that allows you to easily check if it contains a value. Further same basics as earlier.


List<string> donotdelete = new List<string>();
donotdelete.Add(path + "Access");
donotdelete.Add(path + "Excel");
...
...

for (int cnt = 0; cnt < directories.Count; cnt++)
{
    string directory = path + directories[cnt];
    if (donotdelete.Contains(directory))
        continue;

    if (Directory.Exists(directory))
        Directory.Delete(directory);
}

 
Thanks!

I am working on the easy part of this project now hehe, deleting the folders I know of... that part is the next step, where I go into another folder system and delete everything EXCEPT certain items hehe. That part will be much tougher.

Heck I'm having a hard enough time with what would seem to be the eays part. I got a bug I cant track down.
 
OK, finally tracked down my mistake that was messing up my program, and got it working. I would like to post it here and let you guys see it and tell me how I could of done this better, so I can learn.

Let me know how I did for a newb :).

C#:
private bool checkpath(string path)
        {
            bool result = Directory.Exists(path);
            return (result);
        }


        private void buttonRoaming_Click(object sender, RoutedEventArgs e)
        {
            //Simple variables to define directory paths.
            string pathRoam = textPath.Text + "\\";
            bool check = checkpath(pathRoam);
            if (check == false)
            {
                MessageBox.Show("Invalid Path Entered");
                return;
            }


            string pathApp = pathRoam + "Application Data\\";
            string pathCookies = pathRoam + "Cookies\\";
            string icaclient = pathApp + "ICAClient";
            
            //Now I want to delete all the text files in my Cookies Folder.
            string[] files = Directory.GetFiles(pathCookies, "*.txt");
            foreach (string file in files)
            {
                try
                {
                    updater.AppendText("Deleting " + file + "\n");
                    File.Delete(file);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("There was an error when trying to delete: " + file + "\nError type: " + ex);
                }
            }


            //Now I want to do my folder deletions in my main directory pathRoam. CitrixTemp and ProfileUnity Folders.
            string[] folders = Directory.GetDirectories(pathRoam);
            foreach (string item in folders)
            {
                if (item.Contains("CitrixTemp") || item.Contains("ProfileUnity"))
                {
                    try
                    {
                        updater.AppendText("Deleting " + item + "\n");
                        Directory.Delete(item, true);
                    }
                    catch (Exception ex)
                    {


                        MessageBox.Show("There was an error while trying to delete the " + item + " Folder!\nError tpye: " + ex);
                    }
                }
            }
            //Now I am going to delete the folders out of the Application Data Folder. Should be
            //Citrix, ICAClient, ICAClient* and Sun
            string[] foldersApp = Directory.GetDirectories(pathApp);
            foreach (string item in foldersApp)
            {
                if (item.Contains("ICAClient") || item == pathApp + "Sun" || item == pathApp + "Citrix")
                {
                    try
                    {
                        updater.AppendText("Deleting " + item + "\n");
                        Directory.Delete(item, true);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error deleting the folder: " + item + "\n\nError Type: " + ex);
                    }
                }               
            }
            updater.AppendText("Roaming Profile Deleted!");
            updater.ScrollToEnd();
         }
    }

Took out the using statements and window code, just pasting the code I made.
 
Back
Top Bottom