Tuesday 3 September 2019

How to delete Custom locale registry keys from T-SQL


USE master

GO
exec sp_configure 'xp_cmdshell', 1
GO
reconfigure
GO
IF(object_id('tempdb..#DirectoryTree_F') IS NOT NULL ) DROP TABLE #DirectoryTree_F
CREATE TABLE #DirectoryTree_F  (id int IDENTITY(1,1) ,subdirectory nvarchar(512),depth int,isfile bit);
DECLARE @FolderPath SYSNAME = 'C:\Windows\Globalization\'INSERT #DirectoryTree_F (subdirectory,depth,isfile)

EXEC master.sys.xp_dirtree @FolderPath,1,1;

DELETE #DirectoryTree_F WHERE subdirectory NOT LIKE '%.nlp' and isfile=0 and depth  =1
/*SELECT * FROM #DirectoryTree_F
--Keep today's records 
--DELETE #DirectoryTree_F  WHERE CAST( PARSENAME(REPLACE(SUBSTRING(subdirectory,1,CHARINDEX('-',subdirectory,13)-1),'-','.'),1) AS DATE) >= CAST(GETDATE() AS DATE)
--select * from #DirectoryTree_F*/

WHILE EXISTS (SELECT * FROM #DirectoryTree_F)
BEGIN
DECLARE @FileName SYSNAME,@FilePath NVARCHAR(3000),@SQL NVARCHAR(4000)
SELECT TOP 1  @FileName = subdirectory FROM #DirectoryTree_F
SELECT @FilePath = @FolderPath + @FileName
SET @SQL = 'DEL ' + @FilePath
EXEC xp_cmdshell @SQL
--PRINT @FileName
DELETE #DirectoryTree_F WHERE subdirectory = @FileName
END

GO
/*--Delete all Custom Locale registry entry also and it also deletes Custom Locale folder too
*/
EXEC master.dbo.xp_regdeletekey
 @rootkey='HKEY_LOCAL_MACHINE',
  @key='SYSTEM\CurrentControlSet\Control\Nls\CustomLocale\'

GO
/*
--below helps to grant the access to the folder, replace the "domain\group" value to the output of whoami statement*
/

exec xp_cmdshell whoami
go
exec xp_cmdshell 'icacls "C:\Windows\Globalization" /grant "domain\group":(f)'
GO

No comments:

Post a Comment