The main mistake is the idea. For each user account exists data in Windows registry like the GUID of the user account respectively user profile used in security groups for accessing directories and files on NTFS partitions, Windows registry, network, ... Therefore just deleting the profile directory of a user account is possible, but is really not a good idea.
However, this batch file could be used for this task:
@echo off
if not "%~1" == "" goto DeleteFolder
%SystemRoot%\System32\forfiles.exe /P "C:\Users" /D -30 /C "%SystemRoot%\System32\cmd.exe /C if @isdir == TRUE "%~f0" @path"
goto :EOF
:DeleteFolder
for %%I in ("marc" "administrator") do if /I "%~nx1" == "%%~I" goto :EOF
echo rd /Q /S %1
The batch file first checks if it is started with a parameter. The initial start of this batch file is without any parameter which results in executing the command FORFILES and then exit the batch file.
The command FORFILES searches in directory C:\Users
for files and directories older than 30 days. For each file or folder matching this criterion the Windows command interpreter is executed with option /C
to close after finishing execution of specified command.
The started Windows command interpreter first runs an IF condition to check if the current item of FORFILES is a directory at all. Only in this case the batch file is started once again by started Windows command interpreter with the path of the current directory older than 30 days enclosed in double quotes.
So the second instance of the batch file detects that it is started with a parameter and therefore jumps to label DeleteFolder
.
There is a simple FOR loop with two strings in double quotes which can be extended with more strings. An IF condition is executed by FOR for each string inside the parentheses.
The IF condition compares case-insensitive the current string of the loop with the string after last backslash of parameter passed to the batch file. So compared is the folder path without C:\Users\
with the current string from list.
The FOR loop and the batch file execution are exited if the compared strings are equal.
Otherwise if the directory in C:\Users
older than 30 days does not match any of the strings, the command RD to recursively and quietly delete the directory would be executed if there would not be command ECHO left to command RD to just output this command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cmd /?
echo /?
for /?
forfiles /?
goto /?
if /?
See also Where does GOTO :EOF return to?