How can I delete all files from a directory?

I know there is the Delete File activity, but it only works for one file at a time. I want to be able to select the directory and delete all files in it. I haven’t found an actvity that does that. So far the only solution I ahve found is to use List diretory files and then creating a counter that deletes one file at a time as long as the counter is less than the number of files in the directory.

Is there a different approach I can use?

Hi @cris-dsc

I hope you are doing well!

Yes! I am sure you can delete all the files with one activity. Have you tried to use command prompt. If not, give it a try. It will solve your issue.

Share your feedback on this. If it not solves your problem, we can find another way for it.

Have a great day!

Best Regards,
Muhammad Hayyan Khan

I’ve tried using the del * command to delete all files from the folder, but I’m getting an error because the system asks me if I’m sure I want the files to be deleted and I don’t know how to use command prompt activity to answer ‘yes’.

Can you please share the screenshot of the process you mentoined above. So that I could tell you another method to solve the issue.

Hi @cris-dsc!

I see you’ve used the del * command successfully to delete all the files inside a folder, but it’s prompting you for confirmation.

You can add a /q parameter to that command to execute it in “quiet mode” - which means that you will not be prompted for confirmation.

So your full command would be del <directory>\* /q (where <directory> is of course the directory that contains all the files you want to delete).

Please let me know if this works for you! You can also mark my answer as a Solution.

1 Like

Could you inform us, @cris-dsc, if the aforementioned solutions provided by @Ivan_Ramos were effective for you?

The solution I found for this case is the next one:

  1. Create a “.bat” file with the following code:
    @echo off
    setlocal enabledelayedexpansion

set targetFolder=C:\route\of the\file “this route belongs to the folder which you want to delete”

for %%f in (“%targetFolder%*”) do (
if not “%%~nxf” == “eliminar_archivos.bat” (
del /Q “%%f”
)
)

for /d %%d in (“%targetFolder%*”) do (
rd /s /q “%%d”
)

  1. Use the “Command prompt” activity on electroneek and set the route value as the same as the “.bat” file route

For those who want to keep it strictly within the Studio activities, I’ve found that using ‘List Files’ followed by a ‘For Each’ loop with the ‘Delete File’ activity works consistently. Just make sure to handle any ‘File in use’ errors with a Try-Catch block so the whole bot doesn’t crash if a file is still locked.