0

I want to remove all directory in BAT FILE in this path but i don't understand why don't work.

for /d %f in ("C:\Users\Users\AppData\Roaming\Mozilla\Firefox\dir_temp_profile\*") do (
    rmdir %f
)

Thank you for you help

Tazounet
  • 41
  • 6
  • 4
    When using the FOR command in batch files, be sure to specify the variables with double percent: `%%f` – josh poley Aug 24 '23 at 15:45
  • 2
    It would also be better if you were to use the `/S` and `/Q` options with `RD`/`RmDir` command too. – Compo Aug 24 '23 at 17:04
  • I suggest reading: [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) There can be used a batch file with the single command line `@for /F "eol=| delims=" %%I in ('dir "%APPDATA%\Mozilla\Firefox\dir_temp_profile\*" /AD /B 2^>nul') do @rd /Q /S "%APPDATA%\Mozilla\Firefox\dir_temp_profile\%%I" 2>nul` to delete all the directories in that folder but not the files. Directories with files being currently opened by an application like Mozilla Firefox cannot be deleted as the Windows file IO prevents that. – Mofi Aug 24 '23 at 20:00

1 Answers1

1

I think first you need to remove all the files and then the folders. This might help: Just make sure you set your target_path variable accordingly:

@echo off
set "target_path=C:\path\to\your\folder"

REM Remove files recursively
for /r "%target_path%" %%F in (*) do (
    del "%%F" /q
)

REM Remove folders recursively (starting from deepest)
for /d /r "%target_path%" %%D in (*) do (
    rmdir "%%D" /s /q
)

echo Removal complete.

Let me know if that helped.

Mike
  • 229
  • 1
  • 4
  • 1
    If you take a look at [my comment](https://stackoverflow.com/questions/76970879/why-cant-i-delete-my-directories-in-my-bat-file#comment135689889_76970879), you should see a method of doing it without having to first deal with the existing files. The `/S` and `/Q` command options you are already using will deal with the contained files. – Compo Aug 24 '23 at 17:25
  • The first `for` loop is unnecessary as Compo wrote already except all the files in `target_path` should be deleted too which is not reached by this command line at all. Read my answer on: [How to delete files/subfolders in a specific directory at the command prompt in Windows?] Files with hidden or read-only attribute are not deleted by the first `for` loop. The second `for` loop has completely unnecessary the options `/r "%target_path%"` and does also not work as expected by you as directories with hidden attribute in `target_path` are not deleted by this loop. – Mofi Aug 24 '23 at 20:05
  • There are by default no directories and files with hidden or read-only attribute in a Mozilla Firefox profiles directory. The code works for that reason for this folder __by chance__ and not by good code design considering all possibilities. The comment `(starting from deepest)` is complete nonsense as you can see yourself by modifying `rmdir "%%D" /s /q` to `echo rmdir "%%D" /s /q& rmdir "%%D" /s /q` or changing `@echo off` to `@echo ON` and run the batch file from within a command prompt window instead of double clicking on it in *Windows File Explorer*. – Mofi Aug 24 '23 at 20:11
  • Thank you guys ! I use this for /d /r "%target_path%" %%D in (*) do ( rmdir "%%D" /s /q ) – Tazounet Aug 25 '23 at 07:46