Windows – Copying a file from local folder to “C:\Windows\System32\” using batch file

batch-filecopywindows

I am writing an application which uses batch file to copy some files to another location. I am using 64-bit windows 7.

I have asked for admin privileges too using below code:

Code block to get ADMIN right:

@echo off
:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) 
else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line

Code to copy a file to system32 folder:

copy /d /Y "D:\opt\optPath.txt" "C:\Windows\System32\"

There is no error in copy operation, but the file is copied to "C:\Windows\SysWOW64" location automatically. Need help.

Best Answer

Try using:

@echo off
:: Batch-Admin API
    net file>nul 2>&1&&if "%~1"=="64" (goto:GotAdmin) else (if exist "%windir%\Sysnative\" (call start %windir%\Sysnative\cmd /c "%~0" 64&exit) else (goto:GotAdmin))
    echo Requesting administrative privileges...
    (echo Set UAC = CreateObject^("Shell.Application"^)
     echo UAC.ShellExecute "%~s0", "ELEV","", "runas", 0 ) > "%temp%\admin.vbs"
    cscript /Nologo "%temp%\admin.vbs"&exit

:GotAdmin
:: Place ADMIN tasks below
    copy /d /Y "D:\opt\optPath.txt" "C:\Windows\System32\"
    pause
exit

I modified rewrote your script to:

  • Use an alternative way to check for admin permissions net file && echo Admin || echo No-admin
  • Added 64-bit launcher VBScript launches everything as 32-bit (redirecting C:\Windows\System32 to C:\Windows\SysWOW64).

64-Bit launcher: call start %WinDir%\SysNative\cmd /c %0 (This window is hidden)