@echo off
REM Resets default rights for files in GAC_MSIL folder for unreadable files.
REM Useful when Outlook Sync is unable to load and after setting sys. var. VSTO_SUPPRESSDISPLAYALERTS = 0
REM the error similar to:
REM System.IO.FileLoadException: Could not load file or assembly office, Version=, Culture=, PublicKeyToken= or one of its dependencies. Access is denied.
REM Log file GAC_reset.log is created.

SET GAC_Path=%windir%\assembly\GAC_MSIL\

echo Starting to check readability for files from "%GAC_Path%"...

echo Affected Files: > GAC_reset.log

For /f "tokens=*" %%c in ('dir %GAC_Path% /a:-d /s /b') do (
set pathToDirName=%%c
set dirName=%%~dpc
set fileName=%%~nxc
call :next
)

(SET GAC_Path=)
echo DONE!
pause
REM exit

:next
REM echo  %pathToDirName%
type "%pathToDirName%" > nul
IF %ERRORLEVEL% NEQ 0 (
echo Error, Changing owner...
takeown /F "%pathToDirName%" /A > nul
if %ERRORLEVEL% EQU 1 (echo Success) ELSE (
echo Failed, trying whole folder...
takeown /f "%dirName%" /r /d y > nul
if %ERRORLEVEL% EQU 1 (echo Success) ELSE (echo Failed)
)
echo Changing permissions...
icacls "%pathToDirName%" /grant administrators:F /grant SYSTEM:F /grant Users:R
echo %pathToDirName% >> GAC_reset.log
)