How to specify wildcard in folder path?


I want to write a batch file to clear temp files in each users temp profile. How can I do this since each profile has a different username? Ex. I would like to delete the temp files from all these paths and any others that exist. Is there a folder wildcard?

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.

Shinesh PremrajanEngineering ManagerCommented:

Try calling the path in double quotes

something ike..


Hope this helps
eddo9696Author Commented:
But that would only work for a single profie, and that's assuming I know the user name. I need to clear that temp folder for all random user TEMP profiles.
Bill PrewIT / Software Engineering ConsultantCommented:
You can loop over all the subfolders in C:\USERS with a FOR /D statement, and then delete in each user folder found, like this:

for /d %%A in ("c:\users\*.*") do del /q "%%A\AppData\Local\Temp"

In each itteration of the FOR loop %%A will have the value of the user subfolder path, like:


Then we just do a delete command adding on the rest of the path to the TEMP folder.

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

eddo9696Author Commented:
I actually need this path:


I edited your script to this:

FOR /d %%A in ("C:\DOCUME~1\*.*") DO DEL /q "%%A\LOCALS~1\Temp\*.*"

I got an error in DOS of:

C:\>FOR /d %%A in ("C:\DOCUME~1\*.*") DO DEL /q "%%A\LOCALS~1\Temp\*.*"
%%A was unexpected at this time.

Bill PrewIT / Software Engineering ConsultantCommented:
Are you running it at the command line, or in a BAT file?  The syntax for the two are just slightly different.  The form I provided assumed you'd be using this in a BAT file.  If you want to run it right from a command line then change the %%A to %A, like:

FOR /d %A in ("C:\DOCUME~1\*.*") DO DEL /q "%A\LOCALS~1\Temp\*.*"


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
Steve KnightIT ConsultancyCommented:
I would recommend using "%userprofile%\..\*.*" rather than the old 8.3 form or other specific path, as it is not impossible that there could be a different folder with the same 8.3 docume~1 name or this could be a Vista or Win 7 machine using c:\users etc...

This takes the current users profile directory then goes up one level, just replace the bit in the for command of Bill's with that path instead.

Steve KnightIT ConsultancyCommented:
(assuming you want to have this as a standard script that is that you run on different machines etc.)
eddo9696Author Commented:
Worked great, thanks.
Bill PrewIT / Software Engineering ConsultantCommented:
Great, glad to hear that was useful.  Thanks for the input from Steve too, I was focusing on the specifics of how to iterate over the sub-folders and resisted thinking about the "bigger picture" but his comments are definitively inline with my thoughts as well.

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.