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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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

Michael MachieIT SupervisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.