Folder Name and not full path

Hello,

I have a folder with subfolders and I am trying to obtain just the subfolders’ name. For example, the full path of the folder is “C:\User\Desktop\Test\subfolder” for one subfolder and I want to iterate through all of the subfolders in the Test directory. I just want “subfolder” in which I will use that folder name as the subject in outlook. So the subject will then be “subfolder”. If I have subfolder2 then the subject will be “subfolder2”.

@mbaur3

Check as below

You will get only foldername, not path

Mark as solution if this helps

Thanks

1 Like

Check this link below, @mbaur3

Hope this may help you :slight_smile:

It splits every directory. I’m trying to figure out how to keep only the name of the last directory. If I have “C:\test\folder\example” I just want the name of the last directory which is “example”. Thank you for your response!

@mbaur3 - you should use the built-in methods like the one @Manish540 has linked - no need to recreate the wheel by using string manipulation or anything like that :slight_smile:

Directory.GetDirectories(YourPathAsString,"*",SearchOption.AllDirectories) - this code will give you all of the directories (and their subdirectories) within the directory you defined in the variable YourPathAsString

If you want to get the parent directory of YourPathAsString before finding all of the subdirectories, then just use another built-in method to do that.
Directory.GetDirectories(Directory.GetParent(YourPathAsString),"*",SearchOption.AllDirectories)

And of course if you don’t want to get all the subdirectories, then you can just remove the extra stuff in the method as those are optional parameters

@Dave thanks for your response. I don’t understand the built-in methods. I need to do a little more research and maybe I am taking the wrong approach.

Which part are you having trouble understanding? I will do my best to try and explain it if you’d like.

Using the example you put in your first post I will assume you receive the path “C:\User\Desktop\Test\subfolder” as an input. You want to get all of the subfolders within the “Test” folder. I’ll also assume you don’t want the subfolders within the subfolders (so if there is a directory called “C:\User\Desktop\Test\subfolder\secondsubfolder” we would only return the “subfolder” portion and not the “secondsubfolder” one)

1. Assign path = “C:\User\Desktop\Test\subfolder”   //   This is a string variable
2. Assign ParentDir = Directory.GetParent(path)   //    This is a string variable
3. For each item in Directory.GetDirectories(ParentDir)   //   IMPORTANT  -  set the TypeArgument to string within the For Each activity properties
4. Message Box = item.ToString
5. Send Outlook Mail Message
5a. To = toUserName
5b. Subject = item.ToString
5c. Body = (this is kept blank in your example)

@Dave maybe my question wasn’t clear. I am trying to use the name of the folder as the subject to send an outlook message. If I have “C:\User\Desktop\Test\subfolder” then the bot will send a message with “subfolder” as the subject.

1. Assign path = “C:\User\Desktop\Test\subfolder”   //   This is a string variable
2. Get directories from "Test" folder by doing Directory.GetDirectories(path)
 2a. The "Test" folder has other folders for example 2 which are "subfolder" and 
   "subfolder2"  which are the names that I am trying to use as the subject line in outlook.
4. Message Box = item.ToString
5. Send Outlook Mail Messages //send one messages for each of the two subfolders
 5a. First message with "subfolder" as subject line 
   5a1. To = toUserName
   5a2. Subject = item.ToString // subfolder 
   5a3. Body = (blank)
 5b. Second message with "subfolder2" as subject line 
   5b1. To = toUserName
   5b2. Subject = item.ToString // subfolder2 
   5b3. Body = (blank)

I appreciate that you’re taking the time to explain it to me. I am still fairly new to programming and UIPath. Thanks!

When you assign path in step 1, you are already getting the subfolder. Will you be getting a subfolder like that, or will your step one actually be “C:\User\Desktop\Test” ?

If you are receiving the subfolder, then please see step 2 in my example above. Getting the parent directory seems to be the key step you’re missing. Right now your step 2 is getting the directories from the “subfolder” not from the “test” folder. You can fix this by adding another variable + assign activity like I outlined in my example, or else you could combine it with what you have already where you would change from Directory.GetDirectories(path) → Directory.GetDirectories(Directory.GetParent(path))

Either way works, but right now you are not starting from the “Test” folder and that is what seems to be causing the issues

My step one is “C:\User\Desktop\Test”. I am using “path” as a dynamic variable by using “Environment.GetEnvironmentVariable(“userprofile”) +”\Desktop\Test.ToString". I will follow your example and see what I am missing. Thank you for your help.

If path = “C:\User\Desktop\Test” instead of “C:\User\Desktop\Test\subfolder” then you should be good to go and won’t need to use the Directory.GetParent() method.

However, right now your email will be sending the full file path, so it iwll say “C:\User\Desktop\Test\subfolder” instead of “subfolder” in the subject line. Luckily that’s a simple fix, just change the subject from item.ToStringnew DirectoryInfo(item.ToString).Name

4 Likes

Hi, welcome to the community!
Use DirectoryInfo class instead: DirectoryInfo Class (System.IO) | Microsoft Learn
It will give you the details you need and not only the fullpath.

1 Like

You are awesome!! Thank you so much for your time and help!

@bcorrea Thank you for your response! I will take a look but I followed what Dave above stated and used:

new DirectoryInfo(subFolder.ToString).Name

It will be your choice, but will also be a waste of time and resources for your robot… :wink:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.