Getting an error when using LastIndexOf() Method

I am trying to get a list of folder names in an array format.In order to do this, I used the Directory.GetDirectories("") method to fetch the path for all the folders in a certain directory and saved it to a string variable.

Then I loop through each object within the string variable called item. I am trying to get the LastIndexOf() of the path ‘\’ for each item object so I can then get the pure foldername without the slashes (\) in the path.

However,I keep getting the following error in the exclamation mark:
‘object.ToString()’ is a method, which is not valid in the given context

Can’t figure out why - can someone please help?

I am following steps similar to the video here

Hi @I_J

For eg :-
image
lets say From the above img you want the folder names like Dummy Folder 1, Dummy Folder 2, & Dummy Folder 3 to get store in some string variable one by one

Right ??

Hi,

Can you try the following expressions?

getFolderName = System.io.Path.GetFileName(item.toString)

or

getFolderName = System.Text.RegularExpressions.Regex.Match(item.toString,"[^\\]+$").Value

Regards,

Hi @I_J

This is my Main Folder Named as “List of Retail Folders”
image

With in that I have 3 Sub Folders Named as “Dummy Folder 1, Dummy Folder 2, & Dummy Folder 3”
image

And below is the workflow which gives the respective output that you want
project.json (1011 Bytes)
Main.xaml (7.4 KB)
image

Hope this workflow may help you to solve your issue
Mark as solution if this helps you and like it :innocent:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

Hi @I_J

Please use this

path.GetDirectoryName(item.tostring)

Hi Pratik,

Thanks for your effort. However, I seem to get an error when running it in debug mode

Hi @I_J

Is the below Img is your Path ??
image

Or can you show me your Path if above one is not your path in which you are searching the folders ??

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

Yes Pratik - I have other folders within EDI Trading Partners like Dummy1 , Dummy2 etc. I am trying to extract a string array of just the folder names

Hey @I_J

You can simply use for each to get file names.
PFB Screenshot

Hi Guys - thank you all for your valuable input.

Looks like I had missed out the parenthesis
Also for the foreach block I had to use String instead of string array type argument

1 Like

Hi @I_J

“(?<=List of Retail Folders\).+”
In the above regex instead of “List of Retail Folders” you have to edit the folder name as
“(?<=EDI Trading Partners\).+”

And below is the workflow which returns the array of all the subfolder names
Main1.xaml (10.1 KB)

Hope this workflow may help you to solve your issue

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

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