Copying a file from a local folder to the "C: \ Windows \ System32 \" folder using a batch file

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

I also asked for administrator rights using the code below:

Code block for correct ADMIN entry :

@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 for copying the file to the system32 folder :

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

There is no error in the copy operation, but the file is automatically copied to the folder " C: \ Windows \ SysWOW64 ". Help is needed.

+4
source share
1 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 in:

  • net file && echo Admin || echo No-admin
  • ​​64- VBScript 32-bit ( C:\Windows\System32 C:\Windows\SysWOW64).

64- : call start %WinDir%\SysNative\cmd /c %0 ( )

+1

Source: https://habr.com/ru/post/1655831/


All Articles