0
@echo off

SET dir=%temp%
SET /p choice= Are you sure you want to delete your Temp Folder's contents? [y/n] 
IF /I "%choice%"=="y" (
    rmdir /s /Q %dir%
    mkdir %dir%
    Title Deleted Temp's contents
    echo Deleted Temp's contents
    timeout 2 > nul
    exit
)
IF /I "%choice%"=="n" (
    exit
) ELSE (exit)

I want to automate deleting the Temp folder. I'm running this batch script on Windows 10 version 2004

phd
  • 82,685
  • 13
  • 120
  • 165
  • 3
    if not a typo, you need a space between `==“y”` and closing parens in your if statement. `==“y” (`. also you don’t need second if at all. – elzooilogico Aug 17 '20 at 08:05
  • Some folders should be never deleted: The folder referenced by `%TEMP%` is such a folder. It should be never deleted completely, just the files in this folder and all the subfolders. If any application wants to create a temporary file in the folder of which path is assigned to environment variable `PATH` while it is currently deleted, the application can fail with undefined behavior. See [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) It is also not advisable to delete `%TEMP%` due to the NTFS permissions. – Mofi Aug 17 '20 at 12:44
  • 1
    A folder which should always exist should be never deleted and recreated as that results also in deletion of owner and permissions set for the important folder. The recreation of an important folder can next result on different owner or different permissions set for the important folder and access to the folder might be different after deletion and recreation for one or more accounts. So it is better to delete just the files and folders inside the important folder for temporary files, but not the folder itself. – Mofi Aug 17 '20 at 12:48
  • Mofi is correct. Do not delete the folder and recreate it. You should empty the folder instead. (Why? As he said, owner and permissions are lost if you delete and recreate.) – Bill_Stewart Aug 17 '20 at 13:47
  • What happens instead? What have you tried to debug the problem? – Nico Haase Aug 17 '20 at 13:58

2 Answers2

0

Use choice instead, the default option is Y/N. I would also not delete the entire temp folder and recreate, just clean it out:

@echo off
choice /m "Are you sure you want to delete your Temp Folder's contents?"
if errorlevel 2 goto :eof
for /D %%i in ("%temp%\*") do rd "%%i" /S /Q
del "%temp%\*" /S /Q
Title Deleted Temp's contents
echo Deleted Temp's contents
timeout 2 > nul
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

Windows cannot remove a directory which is your current working directory, so the best way to empty a directory is to make it your current directory before trying to remove it. This will essentially remove all permissible content without touching the directory itself. (Note: All content may not be removed, just those which aren't locked/in use, and which you have sufficient permissions to remove).

@Echo Off
SetLocal EnableExtensions

Rem If the directory to empty, has no file content, end the script.
"%__APPDIR__%where.exe" /Q /R "%TEMP%" * || (
    Echo %TEMP% has no file content.
    Echo Exiting . . .
    "%__APPDIR__%timeout.exe" /T 3 /NoBreak 1> NUL
    GoTo :EOF
)

Rem If the running script is in within, the directory to empty, end it.
SetLocal EnableDelayedExpansion
If /I Not "!__CD__:%TEMP%=!" == "%__CD__%" (
    Echo Cannot empty %TEMP% because this script is located within it.
    Echo Please move %~f0 to another location and try again.
    Echo Press any key to exit . . .
    "%__APPDIR__%timeout.exe" /T -1 1> NUL
    GoTo :EOF
)
EndLocal

Rem Make the directory to empty, the current working directory.
Rem If the directory to empty, cannot be found, end the script.
PushD "%TEMP%" 2> NUL || (
    Echo The directory assigned to %%TEMP%% cannot be found.
    Echo Exiting . . .
    "%__APPDIR__%timeout.exe" /T 3 /NoBreak 1> NUL
    GoTo :EOF
)

Rem If the end user answers N, or n, end the script.
"%__APPDIR__%choice.exe" /M "Are you sure you want to empty your Temp directory"
If ErrorLevel 2 (
    Echo Exiting . . .
    "%__APPDIR__%timeout.exe" /T 3 /NoBreak 1> NUL
    GoTo :EOF
)

Rem Empty permissible content from the current directory.
RD /S /Q . 2> NUL
Title Cleaned %CD%.
Echo Emptied permissible content from %CD%

Rem Make the previously current directory, current again.
PopD

Echo Press any key to exit . . .
"%__APPDIR__%timeout.exe" /T -1 1> NUL
GoTo :EOF

The code above may have more content than the other answers, but it shows a logical progression you should try to follow when creating your scripts, especially those which are removing file and directory content.

If you do not want a well thought out structure to your code then you could just do it like this:

@CD /D "%TEMP%" 2>NUL||Exit /B
@"%__APPDIR__%choice.exe" /M "Are you sure you want to empty your Temp directory"
@If ErrorLevel 2 Exit /B
@RD /S /Q . 2>NUL
@Title Cleaned %CD%.
@Echo Emptied permissible content from %CD%
@"%__APPDIR__%timeout.exe" /T 2 1>NUL
Compo
  • 36,585
  • 5
  • 27
  • 39