Skip navigation

JSI Tip 1254. How can I grant a user Full Control of an existing file?


In tip 0557, we granted a user Full Control of all new files and folders that they create in a directory.

To grant a user Full Control on existing files in a folder, use:

JSIFCU UserName FullFolderPath,     where JSIFCU.bat contains:

@echo off
if NOT \{%2\}

\{\} goto begin :syntax @echo Syntax: JSIFCU UserName Folder \[/N\] goto end :begin setlocal set user=%1 set folder=%2 set folder=%folder:"=% if not exist "%folder%\*.*" goto syntax if /i \{%3\}

\{/N\} goto nok set found=N for /f "Tokens=*" %%i in ('net user %user%') do call :parse "%%i" if "%found%"

"N" goto syntax :nok for /f "Tokens=*" %%i in ('dir /b /a-d /s "%folder%"') do call :doit "%%i" endlocal goto end :doit CACLS %1 /E /C /P %user%:F goto end :parse set str=%1 if %str%

"The command completed successfully." set found=Y :end
If you have a directory structure like:

<Drive:>\Folder\UserName1
<Drive:>\Folder\UserName2
<Drive:>\Folder\UserNameN

you can use JSIFCUAll.bat, which calls JSIFCU with the /N switch, to do them all.

JSIFCUAll.bat contains :

@echo off
if NOT "%1"

"" goto begin :syntax @echo Syntax: JSIFCUAll TopFolder goto end :begin setlocal set Folder=%1 set folder=%folder:"=% if not exist "%folder%\*.*" goto syntax for /f "Skip=4 Tokens=*" %%i in ('net users') do call :parse "%%i" endlocal goto end :parse set str=#%1# set str=%str:#"=% set str=%str:"#=% if "%str%"

"The command completed successfully." goto end set substr=%str:~0,25%# set substr=%substr: =% set substr=%substr: #=% set substr=%substr:#=% if "%substr%"

"" goto end if exist "%folder%\%substr%\*.*" call JSIFCU "%substr%" "%folder%\%substr%" /N set substr=%str:~25,25%# set substr=%substr: =% set substr=%substr: #=% set substr=%substr:#=% if "%substr%"

"" goto end if exist "%folder%\%substr%\*.*" call JSIFCU "%substr%" "%folder%\%substr%" /N set substr=%str:~50,25%# set substr=%substr: =% set substr=%substr: #=% set substr=%substr:#=% if "%substr%"=="" goto end if exist "%folder%\%substr%\*.*" call JSIFCU "%substr%" "%folder%\%substr%" /N :end
To grant a user Full Control of files that they own in a common folder, see tip 1255.
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish