Deleting from user profile subfolders using batch or line command

I am trying to delete a text file from all of the user profiles within the c:\Users and F:\Users on a Windows 2008 r2 server, using Del /q /f /s c:\users\%user profile%\folder1\folder2\*.txt. which does not work, and the error says path not found.   I need to delete the text file from all the users folders on the server.

how can I make it work.
Bob BrownIT Asked:
Who is Participating?
Michael MachieFull-time technical multi-taskerCommented:
Try it wothout the c:\USERs and f:\USERS, just %userprofile% notice no spaces

The parameter %userprofile% is how you point to it and that may be why your path is failing.
Remove the USERS folder from the path and remove the space between %user profile%

Del /q /f /s c:\%userprofile%\folder1\folder2\*.txt
Del /q /f /s f:\%userprofile%\folder1\folder2\*.txt
Bill PrewCommented:
Give this a try, should do what you want.

Notice that it is in TEST mode currently, run it in a new command window, and it will display the DEL commands it would execute but not actually execute them (no files will be deleted yet).  This allows you to run it and inspect the console output to verify it will delete the desired files.  Then if all looks well removes the "ECHO" before the DEL command and rerun.

@echo off

for %%A in (C:\Users,F:\Users) do (
    for /d %%B in ("%%~A\*") do (
        ECHO del /q /f /s "%%~B\folder1\folder2\filename.txt"

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.