I'm looking to make a batch file to delete profiles that haven't been used in over 6 months while retaining specific ones such as "Administrator, Default User, All Users, etc."
I've searched all around and have found a way to remove older files/folders:
forfiles.exe /p D:\\Files /s /m . /d -7 /c "cmd /c del @file"
But there's no way of making an exception with "forfiles"
I've also found a way to delete files/folders with exceptions:
for %%i in (*.exe) do if not "%%i"=="file name" del /q "%%i"
But that'll remove every file/folder and not just older ones.
My scenario: I take care of hundreds of workstations which run WinXP. There are several users that log in to these computers and I wouldn't want to delete their accounts. Only those who haven't logged in in a while and the exception of 3 or 4 permanent accounts in each workstation which are always the same. So, if no one has logged in to an Administrator account in over 6 months, I would like for it to stay intact. I can't drop forfiles into all of these workstations or any other piece of software so I'm limited on my capabilities.
Can anyone help?
At first, make a text file with the folder exceptions, put one folder per line in double quotes.
Put the exception file in your %userprofile%
folder.
example exception.txt
:
"Default"
"Default User"
"All Users"
Batch
code:
for /f "delims=" %%a in ('forfiles /p "%userprofile%\.." /d -180 /c "cmd /c if @isdir==TRUE echo @file"^|findstr /vig:"%userprofile%\exception.txt"') do echo rd /s /q "%%~a"
Look at the output and remove the echo
command, if it looks good.
Forfiles
is available in XP Prof. or better, not XP Home .
I put together this script from a couple of scripts I found online which pretty much does what I need all in one shot:
get-childitem "c:\\documents and settings\\" -exclude "Admin*", "All Users", "Default Users" |? {$ .psiscontainer -and $ .lastwritetime -le (get-date).adddays(- 90 )} |% {remove-item $_ -recurse}
The only things that need to be changed are in bold:
I don't know anything about powershell, but the research I did just to put this all together makes me feel like it's worth learning. Thank you for your time and I hope this helps someone out there!
Here is a vbs file that will do what you want.
NumberOfDays = 180 'anything older than this many days will be removed
TempFolderPath = "C:\Users\" 'location of main folder, (deletes folders inside here)
XPTempFolderPath = "C:\Documents and Settings\"
On Error Resume Next
Dim fso, objFolder, objFolderB
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(TempFolderPath)
Set objFolderB = fso.GetFolder(XPTempFolderPath)
'Win 7
'DateCreated or DateLastModified can be used
FOR EACH fldr in objFolder.SubFolders
IF DateDiff("d", fldr.DateLastModified,Now) > NumberOfDays AND NOT isexception(fldr.name) Then
fso.DeleteFolder fldr.path, TRUE
END IF
NEXT
'XP
'DateCreated or DateLastModified can be used
FOR EACH fldr in objFolderB.SubFolders
IF DateDiff("d", fldr.DateLastModified,Now) > NumberOfDays AND NOT isexception(fldr.name) Then
fso.DeleteFolder fldr.path, TRUE
END IF
NEXT
'Put name of folder to NOT delete in "true" section
'The Else blank false, deletes everything else
FUNCTION isException(BYVAL foldername)
SELECT CASE foldername
CASE "Administrator"
isException = TRUE
CASE "All Users"
isException = TRUE
CASE "Default User"
isException = TRUE
CASE "Default"
isException = TRUE
CASE "HPADMIN"
isException = TRUE
CASE "ittech"
isException = TRUE
CASE "Public"
isException = TRUE
CASE "Guest"
isException = TRUE
CASE "LocalService"
isException = TRUE
CASE "NetworkService"
isException = TRUE
CASE ELSE
isException = FALSE
END SELECT
END FUNCTION
Set fso = nothing
Set objFolder = nothing
Set objFile = nothing
Set objSubfolder = nothing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.