Search code examples
batch-filerunas

Batch Script to copy contents of one folder to the other under C:\Program Files (x86) location. Not working - Acess Denied Error


I have written simple batch script, which copies contents of one folder to the other. I have already logged into the machine as Admin. I am working on windows-7 machine.

Folder 1 and Folder 2

Location of folders :

C:\Program Files (x86)\Folder1  
C:\Program Files (x86)\Folder2

If i am running my script by double clicking it, i am getting Access Denied. If I right click on file "Run as Administrators" it correctly copies.

Expected Behavior: On directly double clicking the batch script it should copy the file.As i am already logged in as Admin.

Waiting for valuable feedback, from fellow coder's


Solution

  • Following script, helps to run your batch file as "Run As Admin".

    @echo off
    if _%1_==_payload_  goto :payload
    
    :getadmin
        echo %~nx0: elevating self
        set vbs=%temp%\getadmin.vbs
        echo Set UAC = CreateObject^("Shell.Application"^)                >> "%vbs%"
        echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
    goto :eof
    
    :payload
        echo %~nx0: running payload with parameters:
        echo %*
        echo ---------------------------------------------------
        echo ADM is launching. DO NOT CLOSE THIS WINDOW.
        cd /d %2
        shift
        shift
        echo Name of the batch file which you want to run as admin
        CopyFiles.bat 
    goto :eof
    
    exit