Looping through a list of directories with a batch file?

I’ve started on a batch file that Window’s Task Scheduler can run every Friday to clean up by deleting file bloat. I think my next step is to abstract it into a for loop that can be fed a list of directories, bonus points if I can read that list from an external file as well. (Mainly for security, the manager wants to make sure only they can change the list, but each employee should be able to run it locally.)

::      C:\Users%username%\AppData\Local\Temp
::      C:\Users%username%\AppData\Local\Autodesk\Revit\PacCache
::      C:\Users%username%\AppData\Local\Autodesk\Revit\Autodesk Revit 2023\CollaborationCache
::      C:\Users%username%\AppData\Local\Autodesk\Revit\Autodesk Revit 2023\CefCache
::      C:\Users%username%\AppData\Local\Autodesk\Revit\Autodesk Revit 2023\Journals

:: Delete all files
 del /s /q "C:\Users\%username%\Desktop\DeletedWeeklyOnFriday\*.*"

:: Delete all folders
 for /d %%p in ("C:\Users\%username%\Desktop\DeletedWeeklyOnFriday\*.*") do rmdir "%%p" /s /q


When I try to loop through the list of files it’s breaking on the spaces

@echo OFF
FOR %%x IN (
Autodesk\Revit\Autodesk Revit 2020\CollaborationCache
Autodesk\Revit\Autodesk Revit 2020\CefCache
Autodesk\Revit\Autodesk Revit 2020\Journals
Autodesk\Revit\Autodesk Revit 2022\CollaborationCache
Autodesk\Revit\Autodesk Revit 2022\CefCache
Autodesk\Revit\Autodesk Revit 2022\Journals
Autodesk\Revit\Autodesk Revit 2023\CollaborationCache
Autodesk\Revit\Autodesk Revit 2023\CefCache
Autodesk\Revit\Autodesk Revit 2023\Journals
    ) DO ECHO %%x

I’ve tried back ticks, double quotes and percent signs to escape them, but no luck yet

Override your delimiters.


for /d "delims=" %%p in ("C:\Users\%username%\Desktop\DeletedWeeklyOnFriday\*.*") do rmdir "%%p" /s /q

By default the for loop takes a tab or a space as a delimiter.

1 Like

ah, that makes sense.
For now, I’ve got this version to work.
The only thing left is to see if there is a way to move the list of directories to an external file.

:: @echo off
FOR %%x IN (
"Autodesk\Revit\Autodesk Revit 2020\CollaborationCache"
"Autodesk\Revit\Autodesk Revit 2020\CefCache"
"Autodesk\Revit\Autodesk Revit 2020\Journals"
"Autodesk\Revit\Autodesk Revit 2022\CollaborationCache"
"Autodesk\Revit\Autodesk Revit 2022\CefCache"
"Autodesk\Revit\Autodesk Revit 2022\Journals"
"Autodesk\Revit\Autodesk Revit 2023\CollaborationCache"
"Autodesk\Revit\Autodesk Revit 2023\CefCache"
"Autodesk\Revit\Autodesk Revit 2023\Journals"
    ) DO del /s /q "C:\Users\%username%\AppData\Local\%%~x\*.*"

Tho to point it out, rmdir /s is going to remove the files anyway, so it’s kind of redundant to del first.

I was under the impression batch wouldn’t remove directories until they were empty

Pretty sure /s overrides that behavior and dumps the files too…

1 Like

good to know, I’m more use to shells on linux than batch on windows.

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