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.

Run.Bat
  • 105
  • 1
  • 10
  • 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. – Run.Bat Mar 27 '19 at 16:26
  • 1
    Why wouldn't you use a wildcard with the `DEL` command? – Squashman Mar 27 '19 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. – Compo Mar 27 '19 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. – Run.Bat Mar 27 '19 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. – Compo Mar 27 '19 at 20:39
  • Thanks, I'll take your advice. My main goal is to cleanup cache files generated from our EHR application which `Disk Cleanup` won't pickup on machines that are shared with 20+ users. – Run.Bat Mar 27 '19 at 20:48
  • Have you tried to add the location to be included in Disk Cleanup? and have you asked its developers if they have, or could, provide a handler for it? Also the cached files from your application is irrelevant to the question asked, only each users `temp` directory is! – Compo Mar 27 '19 at 23:33

1 Answers1

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.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 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? – Run.Bat Mar 27 '19 at 17:43
  • 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? – Gerhard Mar 27 '19 at 17:46
  • @GerhardBarnard in the users code they are iterating all the directories in `C:\users` then going to the temp folder for each user. – Squashman Mar 27 '19 at 18:29
  • @Squashman, yes, that is what I got from the code, but I just need to clarify what he meant by other folders. – Gerhard Mar 27 '19 at 18:33
  • The other folders are cache files saved onto the local machine from our EHR application example path would be: C:\ProgramData\appname\xa\xb\xc\cachefiles – Run.Bat Mar 27 '19 at 18:47
  • 1
    ok, I suggest you edit your question and show exactly what you want to achieve as it is difficult to guess the exact requirement. – Gerhard Mar 27 '19 at 18:52
  • 1
    @Squashman.. you get my point :) – Gerhard Mar 27 '19 at 18:52
  • @GerhardBarnard - Sorry, worded for testing purposes. I am testing this on a development VM, so no worries stuff craps out. – Run.Bat Mar 27 '19 at 20:52