0

I have a batch script that goes and deletes all files and folders within the "Temp" folder of each users profile, without deleting the main directory. The script will delete the files just fine but when it gets to the subfolders I get "The system cannot find the path specified." error.

@echo off cd /D c:\Users for /D %%a in (*.*) do del /f/s/q "%%a\appdata\local\Temp\" for /D %%a in (*) do RMDIR /s/q "%%a\appdata\local\Temp\*" 

I've tried as explained in this post: Batch file to perform start, run, %TEMP% and delete all, but the directory "Temp" gets deleted, just need files and subfolders within the parent Direcotry (Temp) to be removed.

7
  • I've tried that method and the directory "Temp" gets deleted, not what I am trying to accomplish. Would like to delete just files and subfolders within the "Temp" directory. Commented Mar 27, 2019 at 16:26
  • 1
    Why wouldn't you use a wildcard with the DEL command? Commented Mar 27, 2019 at 18:31
  • 1
    I hope you're aware that local users do not have to have their profiles in C:\Users, and they're also free to modify the locations of their %TEMP% and/or %TMP% directories. I would also add, that you should not be deleting everything from the temporary directory and that Windows has a Disk Cleanup utility which would be a safer method of dealing with these things. Commented Mar 27, 2019 at 20:03
  • @Compo Yes thank you, I am very aware. The users I'll be running this script on have no idea to make such changes so it'll be okay. Commented Mar 27, 2019 at 20:34
  • 1
    That may be, but you also need to account for other users, like Administrator, All Users, Default, Default User and Public, all of which are likely to exist as directories under your C:\Users location. And please heed my advice about the Disk Cleanup utility, it is without a doubt a better way to manage unnecessary temporary files than this. Commented Mar 27, 2019 at 20:39

1 Answer 1

1

If you wanted to delete all files and folders in all users' appdata\local\temp\ dir, you could simply do:

@echo off for /f "delims=" %%i in ('dir /b "C:\users"') do ( del /Q "%%i\appdata\local\Temp\*">nul rmdir /Q/S "%%i\appdata\local\Temp\*">nul ) 

We don't really care whether it is a dir or a file, we just attempt del and rmdir also we pipe the output to nul.

Sign up to request clarification or add additional context in comments.

8 Comments

This is great, for the Temp folder (which Im testing), but there are other folders I want to clean up that are not defined as an environment variable. How would I accomplish that?
So you want to pretty much clean everything, including folders inside of %LOCALAPPDATA%? or can you specify if you have a list of folders to clean perhaps?
@GerhardBarnard in the users code they are iterating all the directories in C:\users then going to the temp folder for each user.
ok, I suggest you edit your question and show exactly what you want to achieve as it is difficult to guess the exact requirement.
@Squashman.. you get my point :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.