Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for delprof.bat

(view source code of delprof.bat as plain text)

  1. @ECHO OFF
  2. :: Check command line arguments and Windows version
  3. IF     "%1"=="" GOTO Syntax
  4. IF NOT "%2"=="" GOTO Syntax
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6. ECHO.%* | FIND "?" >NUL
  7. IF NOT ERRORLEVEL 1 GOTO Syntax
  8. ECHO.%* | FIND "/" >NUL
  9. IF NOT ERRORLEVEL 1 GOTO Syntax
  10.  
  11. :: Use local environment copy
  12. SETLOCAL
  13.  
  14. :: Check if TAKEOWN is available
  15. SET TOXist=
  16. FOR %%A IN (.;%Path%) DO IF EXIST "%%~A.\TAKEOWN.EXE" SET TOXist=1
  17. IF NOT DEFINED TOXist GOTO Syntax
  18.  
  19. :: Workgroup or domain?
  20. SET Domain=/DOMAIN
  21. SET Profile=
  22. FOR /F "tokens=2*" %%A IN ('NET CONFIG WORKSTATION 2^>NUL ^| FIND /I "Logon domain "') DO IF /I "%%B"=="%ComputerName%" SET Domain=
  23.  
  24. :: Check if command line argument is a valid user ID
  25. NET USER "%~1" %Domain% >NUL 2>&1
  26. IF ERRORLEVEL 1 GOTO Syntax
  27.  
  28. :: Recursively take ownership of profile directories and files
  29. FOR /F "tokens=2*" %%A IN ('NET USER "%~1" %Domain% ^| FIND /I "User profile "') DO IF NOT "%%~B"=="" (
  30. 	CALL :Own "%%~B"
  31. )
  32.  
  33. :: Remove the profile directory
  34. echo RD /S /Q "%Profile%"
  35.  
  36. :: Done
  37. ENDLOCAL
  38. GOTO:EOF
  39.  
  40.  
  41. :Own
  42. :: Display directory name
  43. ECHO.%~1
  44. :: Take ownership of specified directory
  45. TAKEOWN "%~1"
  46. :: Take ownership of files in specified directory
  47. FOR /F "tokens=* delims=" %%A IN ('DIR /A-D /B "%~f1\*.*" 2^>NUL') DO TAKEOWN   "%~1.\%%~A"
  48. :: Recursively take ownership of subdirectories
  49. FOR /F "tokens=* delims=" %%A IN ('DIR /AD  /B "%~f1\*.*" 2^>NUL') DO CALL :Own "%~1.\%%~A"
  50. GOTO:EOF
  51.  
  52.  
  53. :Syntax
  54. ECHO.
  55. ECHO DelProf.bat,  Version 0.50 Beta for Windows NT 4 / 2000 / XP
  56. ECHO Delete the specified user profile
  57. ECHO.
  58. ECHO Usage:  DELPROF  userid
  59. ECHO.
  60. ECHO Where:  userid   is a valid user name
  61. ECHO.
  62. ECHO Note:   This script requires TAKEOWN from the NT 4 Resource Kit
  63. ECHO.
  64. ECHO Written by Rob van der Woude
  65. ECHO http://www.robvanderwoude.com
  66.  
  67. :: Discard local environment copy if applicable
  68. IF NOT "%1"=="" IF "%OS%"=="Windows_NT" ENDLOCAL
  69.  

page last modified: 2024-04-16; loaded in 0.0082 seconds