Advertisement
J2897

Cygwin Remover v1.5 - BETA

Mar 9th, 2016
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.23 KB | None | 0 0
  1. :: Released under the GNU General Public License version 3 by J2897.
  2.  
  3. @echo OFF
  4. setlocal
  5. title Cygwin Remover
  6. cls
  7.  
  8. :: Cygwin's POSIX permissions often confuse people whom then try to adjust
  9. :: permissions from Windows - which can be brutal for delicate, little Cygwin.
  10.  
  11. :: Cygwin's permissions are only meant to be configured from within Cygwin. So
  12. :: if you've accidentally messed up Cygwin's permissions, this may help you to
  13. :: remove Cygwin ready for a fresh, warm and cosy, reinstall.
  14.  
  15. :: Inspired by...
  16. :: http://superuser.com/questions/167219/how-to-completely-uninstall-remove-cygwin-from-windows-7
  17.  
  18. set "TAB=   "
  19. set "CYGUSER=cyg_server"
  20. set "ERRORS=0"
  21.  
  22. ver | find "Version 6." >nul
  23. if %ERRORLEVEL% EQU 0 (
  24.     set "OS_VERSION=Windows 7"
  25.     goto :Checks
  26. )
  27. ver | find "Version 5." >nul
  28. if %ERRORLEVEL% EQU 0 (
  29.     set "OS_VERSION=Windows XP"
  30. )
  31.  
  32. :Checks
  33. if not defined OS_VERSION (
  34.     echo Unsupported operating system.
  35.     echo.
  36.     goto :End
  37. )
  38.  
  39. if "%OS_VERSION%"=="Windows 7" (
  40.     REM Do OPENFILES to check for administrative privileges
  41.     openfiles >nul
  42.     if errorlevel 1 (
  43.         color cf
  44.         echo Right-click on this file and select 'Run as administrator'.
  45.         pause
  46.         color
  47.         exit /b 1
  48.     )
  49. )
  50. if exist "%SYSTEMDRIVE%\cygwin" (set "MCF=%SYSTEMDRIVE%\cygwin")
  51. if exist "%SYSTEMDRIVE%\cygwin64" (set "MCF=%SYSTEMDRIVE%\cygwin64")
  52. if not defined MCF (
  53.     echo Cygwin folder doesn't exist.
  54.     goto :end
  55. )
  56. net user | find "%CYGUSER%" >nul
  57. if %ERRORLEVEL% NEQ 0 (
  58.     echo Unable to proceed. The user %CYGUSER% doesn't exist.
  59.     goto :end
  60. )
  61. if exist "%MCF%\home\%USERNAME%\.bashrc" (
  62.     echo WTF^? Dude, I ain't destroying your ".bashrc" file:
  63.     echo %MCF%\home\%USERNAME%\.bashrc
  64.     explorer /select,"%MCF%\home\%USERNAME%\.bashrc"
  65.     goto :end
  66. )
  67.  
  68. set "DELSERV=0"
  69. echo Stopping and deleting services created by the user "%CYGUSER%"...
  70. for /F %%A in ('wmic service get name^, StartName ^|find ".\%CYGUSER%"') do (
  71.     net stop "%%A" && sc delete "%%A"
  72.     if %ERRORLEVEL% NEQ 0 (set "DELSERV=1")
  73. )
  74. if %DELSERV% EQU 1 (set /a "ERRORS+=1" & echo There was a problem.)
  75.  
  76. echo Taking ownership of the Main Cygwin Folder...
  77. takeown /F "%MCF%" /R /D Y >nul
  78. if %ERRORLEVEL% NEQ 0 (
  79.     set /a "ERRORS+=1"
  80.     echo There was a problem.
  81. ) else (
  82.     echo Done.
  83. )
  84.  
  85. echo Resetting permissions...
  86. if "%OS_VERSION%"=="Windows XP" (
  87.     icacls "%MCF%" /reset /T /C /Q >nul 2>&1
  88. ) else (
  89.     icacls "%MCF%" /reset /T /C /L /Q
  90. )
  91. if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
  92.  
  93. echo Removing the Main Cygwin Folder...
  94. RD /S /Q "%MCF%"
  95. if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
  96.  
  97. echo Deleting users...
  98. net user | find "%CYGUSER%" >nul
  99. if %ERRORLEVEL% EQU 0 (
  100.     net user %CYGUSER% /delete
  101. )
  102. net user | find "sshd" >nul
  103. if %ERRORLEVEL% EQU 0 (
  104.     net user sshd /delete
  105. )
  106.  
  107. if "%OS_VERSION%"=="Windows XP" (
  108.     set "CUF=%SYSTEMDRIVE%\Documents and Settings\%CYGUSER%"
  109. ) else (
  110.     set "CUF=%SYSTEMDRIVE%\Users\%CYGUSER%"
  111. )
  112.  
  113. echo Taking ownership of the Cygwin User Folder...
  114. takeown /F "%CUF%" /R /D Y >nul
  115. if %ERRORLEVEL% NEQ 0 (
  116.     set /a "ERRORS+=1"
  117.     echo There was a problem.
  118. ) else (
  119.     echo Done.
  120. )
  121.  
  122. :: I think the errorlevel of "if" will be returned instead of "icacls". 'Needs testing.
  123. echo Resetting permissions...
  124. if "%OS_VERSION%"=="Windows XP" (
  125.     icacls "%CUF%" /reset /T /C /Q >nul 2>&1
  126. ) else (
  127.     icacls "%CUF%" /reset /T /C /L /Q
  128. )
  129. if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
  130.  
  131. echo Removing the Cygwin User Folder...
  132. RD /S /Q "%CUF%"
  133. if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
  134.  
  135. echo Deleting firewall entry...
  136. if exist "%SYSTEMDRIVE%\Program Files (x86)" (
  137.     netsh advfirewall firewall delete rule name="OpenSSH"
  138. ) else (
  139.     netsh firewall delete allowedprogram "%SYSTEMDRIVE%\cygwin\usr\sbin\sshd.exe"
  140. )
  141.  
  142. echo Cleaning registry...
  143. reg delete HKCU\Software\Cygwin /f >nul 2>&1
  144. reg delete HKLM\Software\Cygwin /f >nul 2>&1
  145.  
  146. echo.
  147. if %ERRORS% EQU 0 (
  148.     echo Cygwin was removed absolutely perfectly^! ^:^)
  149. ) else (
  150.     echo The removal of Cygwin didn't go so well. There were at least %ERRORS% problem^(s^).
  151. )
  152.  
  153. if exist "%USERPROFILE%\.bashrc" (
  154.     echo.
  155.     echo Don't forget about your ".bashrc" file:
  156.     echo %USERPROFILE%\.bashrc
  157. )
  158.  
  159. :end
  160. endlocal
  161. echo.
  162. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement