How to delete multiple files

uiautomation
studio

#1

I have some files with different extensions that I need to delete. I tried to delete multiple files adding a * in the path for the Delete activity like:

“C:\Users\FINROB1\file.*”

But I get an error message.

sortIssues has thrown an exception
Message: Illegal characters in path.

What is the right way to do it.

Thanks


How to Delete all files & all folders in a specific directory
#2

Hi @selrac,

Please follow the example that I have attached:
Main.xaml (7.2 KB)


#3

I see. You have to look through the files and delete. Would it be possible to use Visual Basic for this type of actions?


#4

Hi @selrac,

To be honestly I don’t know, but why wouldn’t use the approach that I have suggested? I don’t get your point.


#5

I would go with Mr. @acaciomelo 's solution , but if you want in a single line you can try somethinking like below

Array.ForEach(Directory.GetFiles(@"c:\MyDir\"), File.Delete);


Delete files from a folder
Checking all open excel file in current path and delete them from folder
How to delete all the files from a folder and make it empty
How to delete the all the files in a folder in one line
#6

Wanna add some points here: @selrac

if you will use this:

When directory will be empty then it will return an error so keep that in mind and as a workaround you can handle it by using files count with if condition.

like

Directory.GetFiles("<your path>").Length > 0

Regards…!!
Aksh


#7

I would be tempted to use the Path.GetExtension method to determine whether or not you want to delete the file as it sounds like you only want to delete certain ones…

https://msdn.microsoft.com/en-us/library/system.io.path.getextension(v=vs.110).aspx

E.g. get all files in the directory then loop through using the above to check the file extensions and delete them if appropriate.


#8

Long Path Tool allows delete, copy or rename long path files.


#9

Hey @Edwards_Crofer
why you wanna depend on third party tool if you can make things with code. of some lines :slight_smile:

Regards…!!
AKsh


#10

Can you pls provide .xaml file for this…


#11

Hi All,

refer below xaml file to delete all files from the folder.

Main.xaml (10.1 KB)

Regards,
Arivu


#13

Hi, arivu96!

Thank you so much for sharing the xaml file!

However, i have problem, when I add the delete activity to if window and add item as the path just as you did, it gives me an error that “object cannot be converted to string”.
I’m a newbie since a few weeks back and i still haven’t gotten the hang of all the variable type and how they work.
Would you please tell me what the solution might be to this error.

Thank you


#15

You’ve probably already figured this out, but in case others have the same result I’ll help…

In UiPath, when you create a For Each statement, the default type is Object. You have to either change the TypeArgument in properties to String, or change the item to item.toString


#16