12

looking for a way to perform the following:

Start > Run > "%TEMP% > Delete everything (skipping any conflicts).

so Far I have

@echo off start %TEMP% DEL *.* 

I suppose I could use CD to get to the folder, the thing I'm wondering is if there are any instances where it cannot delete an a dialog box comes up, I want to skip these.

Thanks for the help! Liam

8 Answers 8

34

del won't trigger any dialogs or message boxes. You have a few problems, though:

  1. start will just open Explorer which would be useless. You need cd to change the working directory of your batch file (the /D is there so it also works when run from a different drive):

    cd /D %temp% 
  2. You may want to delete directories as well:

    for /d %%D in (*) do rd /s /q "%%D" 
  3. You need to skip the question for del and remove read-only files too:

    del /f /q * 

so you arrive at:

@echo off cd /D %temp% for /d %%D in (*) do rd /s /q "%%D" del /f /q * 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks mate! Works a charm, can now schedule it weekly with task scheduler!
One issue I am facing. "The process cannot access the file because it is being used by another process." when trying for second time. Do we need to kill something?
Why would you need to loop thru all the directories in the batch when RD can do it by itself? @echo off del %temp% /f /s /q >nul rd %TEMP% /S /Q>nul
@arana: Because RD will also remove the directory you specified along with anything beneath it. That may be fine in some cases, but typically I avoid this, as re-creating the directory could lead to issues (different ACLs, etc.).
This is great. I used to do nested loops, one /R to get all the files, and then a loop that POP to delete the directories once they were empty. However later I created this nested loop to write to each file "echo junk > file.* And then double back to delete the files and dirs.
12

The following batch commands are used to delete all your temp, recent and prefetch files on your System.

Save the following code as "Clear.bat" on your local system

*********START CODE************ @ECHO OFF del /s /f /q %userprofile%\Recent\*.* del /s /f /q C:\Windows\Prefetch\*.* del /s /f /q C:\Windows\Temp\*.* del /s /f /q %USERPROFILE%\appdata\local\temp\*.* /Below command to Show the folder after deleted files Explorer %userprofile%\Recent Explorer C:\Windows\Prefetch Explorer C:\Windows\Temp Explorer %USERPROFILE%\appdata\local\temp *********END CODE************ 

1 Comment

The prefetch seems very small. What's the reasoning? Is it a log (spyware) that carries personal data?
4

If you want to remove all the files in the %TEMP% folder you could just do this:

del %TEMP%\*.* /f /s /q 

That will remove everything, any file with any extension (*.*) and do the same for all sub-folders (/s), without prompting you for anything (/q), it will just do it, including read only files (/f).

Hope this helps.

2 Comments

It won't remove directories, though.
@Joey True, your answer is better :)
3
@echo off RD %TEMP%\. /S /Q ::pause explorer %temp% 

This batch can run from anywhere. RD stands for Remove Directory but this can remove both folders and files which available to delete.

Comments

2

cd C:\Users\%username%\AppData\Local rmdir /S /Q Temp

del C:\Windows\Prefetch*.* /Q

del C:\Windows\Temp*.* /Q

del C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent Items*.* /Q pause

Comments

1
@echo off del /s /f /q %windir%\temp\*.* rd /s /q %windir%\temp md %windir%\temp del /s /f /q %windir%\Prefetch\*.* rd /s /q %windir%\Prefetch md %windir%\Prefetch del /s /f /q %windir%\system32\dllcache\*.* rd /s /q %windir%\system32\dllcache md %windir%\system32\dllcache del /s /f /q "%SysteDrive%\Temp"\*.* rd /s /q "%SysteDrive%\Temp" md "%SysteDrive%\Temp" del /s /f /q %temp%\*.* rd /s /q %temp% md %temp% del /s /f /q "%USERPROFILE%\Local Settings\History"\*.* rd /s /q "%USERPROFILE%\Local Settings\History" md "%USERPROFILE%\Local Settings\History" del /s /f /q "%USERPROFILE%\Local Settings\Temporary Internet Files"\*.* rd /s /q "%USERPROFILE%\Local Settings\Temporary Internet Files" md "%USERPROFILE%\Local Settings\Temporary Internet Files" del /s /f /q "%USERPROFILE%\Local Settings\Temp"\*.* rd /s /q "%USERPROFILE%\Local Settings\Temp" md "%USERPROFILE%\Local Settings\Temp" del /s /f /q "%USERPROFILE%\Recent"\*.* rd /s /q "%USERPROFILE%\Recent" md "%USERPROFILE%\Recent" del /s /f /q "%USERPROFILE%\Cookies"\*.* rd /s /q "%USERPROFILE%\Cookies" md "%USERPROFILE%\Cookies" 

2 Comments

Thank me after... Enjoy!! :)
Welcome to Stack Overflow, please review: stackoverflow.com/help/how-to-answer
0
@echo off del /s /f /q c:\windows\temp\*.* rd /s /q c:\windows\temp md c:\windows\temp del /s /f /q C:\WINDOWS\Prefetch del /s /f /q %temp%\*.* rd /s /q %temp% md %temp% deltree /y c:\windows\tempor~1 deltree /y c:\windows\temp deltree /y c:\windows\tmp deltree /y c:\windows\ff*.tmp deltree /y c:\windows\history deltree /y c:\windows\cookies deltree /y c:\windows\recent deltree /y c:\windows\spool\printers del c:\WIN386.SWP cls 

Comments

-1

Just use

del /f /q C:\Users\%username%\AppData\Local\temp 

And it will work.

Note: It will delete the whole folder however, Windows will remake it as it needs.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.