"Access is denied" when a .CMD file tries to copy a file

45,231

Solution 1

I'm afraid there's no simple solution to what you're trying to do.

  • cmd is not very interactive, and scripts are intended to be run without human attention.
  • another limitation lies in the way UAC works: a process can be elevated only when it starts; after a process is started, it either has administrator privileges or not.
    So in your case, for every command that could potentially lead to UAC prompt, you have to code both ways: where the script has permission to copy without elevation, and with the elevation.

You can't program elevation with cmd unless you have an additional tool which handle elevation for you and then proceed with the command that failed with regular permissions.


If you were OK with the way it worked on Windows XP, the most simple solution is to run your script with administrator privileges.

If the purpose of your script is to copy one single file and that's all the script does, creating utility that plays nice with UAC seems like overkill.


You can create shortcut, .lnk file, for your script. Then in shortcut properties, on Shortcut tab, click Advanced button and check Run as administrator. Click OK twice to save changes.

When you click such shortcut, you'll see UAC prompt for Windows Command Processor (because it executes .cmd files).

Solution 2

You need to open the command prompt as an administrator.

  1. Open the task manager, either by opening the run dialog (press Win + R together) and typing taskmgr, or by pressing Ctrl + Shift + Esc.

  2. Click File --> New Task.

  3. Check the box that says Create this task with administrative privileges. If you don't see this, it means that your user account is a standard account, not an administrator account. Since you can click through the UAC prompt, I'm assuming you're an administrator.

  4. Type cmd and click OK. You should be able to run your copy command from this command prompt without receiving any permission errors.

Alternatively, you can press Win + X to bring up the Power User Menu, and select

power user menu

and select Command Prompt (Admin) from there (or press A)

Share:
45,231

Related videos on Youtube

user184411
Author by

user184411

Updated on September 18, 2022

Comments

  • user184411
    user184411 over 1 year

    I have a .CMD file that I run by right-clicking on a file in Windows Explorer; the file that I click on gets passed as the parameter to the .CMD file. The .CMD file does some processing and then copies a file from one folder to another. It worked fine on Windows XP; on Windows 8, when the "copy" command runs, it returns the error message "Access is denied".

    If I copy the file using Windows Explorer, a dialog box pops up with the message "You'll need to provide administrator permission to copy to this folder". I click on "Continue", and the file gets copied.

    I don't know the details of UAC, but I know that I don't want to change permissions to allow the file to be copied without intervention. That is, I don't want the whole .CMD file to run as administrator. I want to right-click the input file, run the .CMD file, and have the dialog appear when the "copy" command tries to run; I'll then click on "Continue" to allow only the copy command to run with administrator privilege. (If there were another place in the .CMD file where a file were copied, or any other operation that required administrator privilege, I would want the dialog to appear again, to ask if it's ok to run that operation as administrator).

    Can this be done?

  • user184411
    user184411 over 10 years
    I've edited the question, and added some more detail, to show why this isn't the answer I'm looking for.
  • John Bensin
    John Bensin over 10 years
    @user184411 It sounds like you need a batch file. Place your copy command in a batch file, then run that as administrator.
  • user184411
    user184411 over 10 years
    I've edited the question yet again, trying to be clearer, in case anyone can help; but @Alexey Ivanov: I think you've made it clear that what I want to do can't be done. I'll try the shortcut (.lnk file). Thanks.
  • user184411
    user184411 over 10 years
    The shortcut didn't work; I'll try sudowin.
  • Alexey Ivanov
    Alexey Ivanov over 10 years
    @user184411 How didn't shortcut work?
  • user184411
    user184411 over 10 years
    @ Alexey Ivanov: I'll tell you when I get back to my computer, early in October.