You can use the COPY
command to determine the "yes" response used by the machine.
@echo off
setlocal
for /f "delims=(/ tokens=2" %%Y in (
'"copy /-y nul "%~f0" <nul"'
) do if not defined yes set "yes=%%Y"
takeown /f foldername /r /d %yes% >nul 2>&1
The %yes%
value will be the entire word, not just the first letter. I think that will work just fine. But if I am wrong, you can simply use %yes:~0,1%
instead.
Update
As fgrieu points out in his comment, the above will fail if the script path includes (
. Failure due to other special characters could be fixed by ditching the outer double quotes and escaping the redirection. But the (
is problematic.
Here is an alternate strategy that works as long as %temp%
points to a valid folder that the user has write privileges. It creates a file with a name known not to cause problems. If the file already exists in %temp%
then it is undisturbed.
@echo off
setlocal
pushd "%temp%"
if not exist getYes.tmp (call ) >>getYes.tmp
set "yes="
for /f "delims=(/ tokens=2" %%Y in (
'"copy /-y nul getYes.dummy <nul"'
) do if not defined yes set "yes=%%Y"
takeown /f foldername /r /d %yes% >nul 2>&1
I suppose there is no guarantee that COPY and TAKEOWN use the same yes response, but I would be shocked if they don't.