How grand folder permission to delete it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drjackool
Posts: 1
Joined: 01 Jan 2021 03:52

How grand folder permission to delete it

#1 Post by drjackool » 01 Jan 2021 04:01

Hi
I need to redirect Windows logs folder by mklink command but before creating a link the source folder must be deleted and when I using rmdir command it reports access denied. for solving this problem i have to change folders permission manually. But I need a command to change it from command prompt before calling rmdir command. I found icacls to changing folder security options but I do not know how set parameters it is complex command.

Please help

Following is my batch file to redirect some Windows directories:

Code: Select all

@ECHO OFF
CLS

ECHO *******************************************************************************************
ECHO * This file applies some settings that improves the system performance    *  
ECHO * and increases SSD life span                                                                   *
ECHO *******************************************************************************************
ECHO.
PAUSE

ECHO {************ Creating directories redirection ************}

:: Set redirection path to this variable
SET REDIR_PATH="D:\SysRedirection"

IF EXIST "%REDIR_PATH%" (RMDIR /S /Q "%REDIR_PATH%")

:: Logs
SC stop "EventLog"
CALL :Redirect "C:\Windows\Logs", "%REDIR_PATH%\WinLogs"
CALL :Redirect "C:\Windows\System32\LogFiles", "%REDIR_PATH%\Sys32Logs"
CALL :Redirect "C:\Windows\System32\winevt\Logs", "%REDIR_PATH%\EventLogs"
SC start "EventLog"

ECHO {************************** Done **************************}
PAUSE

EXIT /B %ERRORLEVEL%


:: This function deletes the source folder and creates a junction link at target
:: Param1: Source folder, Param2: Target
:Redirect
ECHO ============================================================
ECHO Redirecting %~1
ECHO 
IF EXIST %~1 (
	RMDIR /S /Q %~1
	:: Check if folder exist yet, ask the user to delete it manually
	IF EXIST %~1 (
		ECHO.
		ECHO [!!!NOTE!!!] Please delete %~1 manually before continue.
		PAUSE
		)
	)

MKDIR %~2
MKLINK /J %~1 %~2
ECHO.
EXIT /B 0

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How grand folder permission to delete it

#2 Post by aGerman » 01 Jan 2021 08:53

To answer your question - take a look at the TAKEOWN and ICACLS commands.
Something like ...

Code: Select all

takeown /R /F "the path here"
icacls "the path here" /grant %username%:F
... might help. (untested though)


Side notes:
- Your script has to be run elevated. Bail out if not.

- Thake care of parenthesizing. Your ...

Code: Select all

SET REDIR_PATH="D:\SysRedirection"
IF EXIST "%REDIR_PATH%" (RMDIR /S /Q "%REDIR_PATH%")
... doubles it in the second line. Wrong!

- Fail fast if anything was going wrong. Make sure subsequent commands are not executed anymore after displaying an appropriate error message.

- You might NOT want to delete the original folder. Rather move it using ROBOCOPY.

Code: Select all

robocopy "the path here" "new path here" /s /e /move /r:1 /w:1
Steffen

Post Reply