Please Read: Significant Update Planned, Migrating Forum Software This Month

See more
See less

Deleting user profiles - once more for King George

  • Filter
  • Time
  • Show
Clear All
new posts

  • Deleting user profiles - once more for King George

    Hello all.

    I know this topic has been posted before (I have used the search), but none of the topics have done exactly what I would like to do. All computers are Windows XP SP2 in a domain which I have read-only access to.

    What I want to do is delete user profiles which are older that 14 days. Profile names consist of UDC and then 4 or 5 digits, e.g. UDC1234 or UDC12345. Delprof /q /i /d:14 would do the trick, but a test run showed that this command also deletes the Administrator, default user and all users folder, which I want to keep. How can I be sure that certain folders will not be deleted when I run delprof? Is it as simple as delprof /q /i /d:14 UDC*?

    I have read about the touch command at, but another test showed that this command could not be recognized. This would possibly help, but do I need to download some software before this command is known?

    There's also the question of when to delete the profiles. To avoid excessively long logon times, I would like to delete the profiles during startup of the machine. I suppose that means either autoexec.bat or autoexec.nt, unless a better solution is available?

    Alternatively, a bat file could be constructed, so that I could carry it with me on a floppydisk or usbdrive and then run the bat file during preparation for imaging the machine.
    Last edited by slmadsen; 27th May 2008, 10:56. Reason: Typos and linkfix

  • #2
    Re: Deleting user profiles - once more for King George

    You can use Forfiles to find folders older than 14 days.
    then use Findstr /r to match a foldername pattern.
    Finally use RD with the switches /S /Q to delete the folders from the computer.

    Make the batch a computer startup script.

    'Forfiles' does not ship with Windows XP.
    There exist different versions of ForFiles! The one you can download as a Windows 2000 Resource Kit tool from the internet, uses a different command line syntax than the version that is installed with Windows Server 2003.

    The batch below works only for the 'Forfiles' version that ships with Windows Server 2003. You can copy that file to a networkshare, and use "unc-Path\forfiles.exe".... in the batch.

    The batch is written to run on Windows XP only
    :: this sample -
    :: find folders where of the name starts with UDC followed by at least 4 digits
    @echo off
    :: This batch removes only certain userprofile folders from the local computer.
    :: (where "Target" is the 'Documents and Settings' folder)
    :: The batch find the folders if the foldername matches a defined pattern
    :: and will only delete that folder if it is n "daysold" or older
    (Set userprofiles=%AllUsersProfile:~0,-10%)
    Set "Target=%userprofiles%"  you can also harcode a local-path here
    Set "_RunCMD=ECHO @Path /S /Q"  replace ECHO with RD for acually removing of the userfolders
    Set "daysold=15"
    Set "namepattern=\^<.UDC[0-9][0-9][0-9][0-9]"
    ::Remark about the reguler expression ("namepattern") above
    :: the ^ is just needed to escape the sign that comes next which is normally a redirect sign to dos.
    :: the dot in front of the 'first characters of the name' is needed for the 'forfiles' loop later on. Because 'forfiles' add quotes around the found foldername.
    If Exist "%Target%" (
    forfiles /p "%Target%" /d -%daysold% /c "%comspec% /c IF @isdir==TRUE ([email protected] |Findstr /i /r \"%namepattern%\")>nul &&(%_RunCMD%)"
    ::Remark about the \ signs that are used in the string for Findstr.
    :: because the entire command after the /c switch must be between quotes,
    :: the \ sign is needed to escape the two quotes that are within quotes.
    rem Pause
    test test test the batch before making it a startup script!

    Last edited by Rems; 27th May 2008, 21:12. Reason: added the colors

    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts