views:

104

answers:

2

I'm writing a .bat script where I need to copy a file to System32. I change to the folder then attempt to copy the file from a storage folder to the System32 folder.

cd C:\Windows\System32
copy %~dp0file.txt file.txt

I get an error Access Denied, 0 files copied.

I see why this is a problem, because if I try to copy a file to System32 using the non-programmatic GUI interface, I get a prompt asking me to confirm. So with the script, how do I bypass this Windows permissions or set it correctly, or some other solution.

Edit: The hint I got from the answer below is that it's possible to trigger Windows to show its GUI prompt for the user to give permissions. This idea will do. Hopefully someone knows exactly how to do this.

+1  A: 

Belongs on superuser (voting to move).

Answer is: use an elevated command prompt (or if you launch your batch file from a shortcut, select "Run as Admin" in the shortcut properties)

EDIT: Now that you explain you're looking for a programmatic way to trigger elevation, you should have a look at this other question (not necessarily the accepted answer, but all the other answers). I'd still vote to close, but as a dupe instead of move to superuser. From batch you might want to look at the "runas" command, but it will still need user confirmation.

Ben Voigt
This answer does not work for me. I'm looking for a programmatic way to manage the permissions with code.
Berming
You can't programmatically unprotect something that requires admin rights unless you're running with full (elevated) admin rights, and doing so is an extremely bad idea that makes the computer vulnerable to all sorts of mischief. Your best option is to trigger the elevation prompt instead of simply failing.
Ben Voigt
Ok, +1 for the hint. I guess the essence of the thread you linked to is to trigger Windows to show its GUI prompt. This idea will do. Hopefully someone knows exactly how to do this.
Berming
+1  A: 

You can use RunAs command to copy the file as a local administrator. http://ss64.com/nt/runas.html

Ruel