How to specify wildcard in folder path?

Hello,

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?

C:\Users\user1\AppData\Local\Temp
C:\Users\user2\AppData\Local\Temp
C:\Users\user3\AppData\Local\Temp
eddo9696Asked:
Who is Participating?
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..

"C:\Users\user1\AppData\Local\Temp"


Hope this helps
0
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.
0
Bill PrewCommented:
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:

C:\Users\user1
C:\Users\user2
C:\Users\user3

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

~bp
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

eddo9696Author Commented:
I actually need this path:

C:\DOCUME~1\(USERNAME_VARIABLE)\LOCALS~1\Temp

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.

0
Bill PrewCommented:
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\*.*"

~bp
0

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
0
Steve KnightIT ConsultancyCommented:
(assuming you want to have this as a standard script that is that you run on different machines etc.)
0
eddo9696Author Commented:
Worked great, thanks.
0
Bill PrewCommented:
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.

~bp
0
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.

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.