command file changing file/folder permissions
<p>I am attempting to audit windows command files by writing to an audit folder set under C:\ProgramData\CommandFileName.
If the audit folder does not exist, the command file will create it as in the following code (echo/pause inserted for debugging):</p>

<pre><code>@SET CmdFileName=%~n0
@SET LogFileFolder=%ProgramData%\%CmdFileName%
@IF NOT EXIST "%LogFileFolder%\" (
 @ECHO About to create the audit folder
 @ECHO ON
 MKDIR "%LogFileFolder%\"

 @ECHO Check before proceeding
 @PAUSE
 @ECHO OFF
)
</code></pre>

<p>If the first run is as an administrator, the folder is created, the echo/pause statements execute and the audit log file is created also.</p>

<p>For any subsequent run as a standard user, the attempt to append to the audit log results in an "access denied" error.</p>

<p>If the first run is by a standard user however, this problem does not occur.</p>

<p>It is clearly an issue of permissions but how can I ensure that if the first run is by Administrator, the newly created folder (and any files subsequently created within it) can be appended to by a standard user?</p>

<p>I have tried inserting the following after the MKDIR above:</p>

<pre><code>icacls "%LogFileFolder%" /grant Everyone:(OI)(CI)F
</code></pre>

<p>But this simply aborts the command file with "(CI)F was unexpected at this time." without even executing the ECHO, MKDIR or PAUSE lines.
This happens whether run as Administrator or Standard user.</p>

<p>Can somebody suggest what it is I am doing wrong?</p>