Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a batch file that renames a file in a folder using:

rename filename.exe filename.bac

This works good in Win XP. But in Vista/7, if the batch file is run under Program Files/ it does not rename the file unless I run it as administrator. Is there any way to bypass the UAC and use this without the need to run it as administrator?

thanks.

share|improve this question

migrated from stackoverflow.com Jul 3 '10 at 6:31

This question came from our site for professional and enthusiast programmers.

3 Answers 3

If you ever worked with linux you'll note that even when you are an administrator you don't get to do everything you want without elevation. The same concept applies to UAC. Even if you are logged in as an administrator, you run with standard user privileges by default. So no, you can't just make changes to system files (like the program files directory) - that's exactly what UAC is there to prevent.

You can disable uac if you really want to, but this is a potential security risk.

share|improve this answer
    
So, you're telling me that there's no way to run rename the file without having to run it under Administrator? –  Anonymous Jul 1 '10 at 17:53
2  
It should be noted that disabling UAC is discouraged for kinda obvious reasons. –  Joey Jul 1 '10 at 18:22
1  
Yes, you have to run it as an administrator if you want to change things in program files. That's the point of UAC - it prevents non-administrators from making changes to program files. –  nhinkle Dec 18 '10 at 0:36

Modifying folders and files in the program files directory requires elevation, even if you are logged in as an administrator. There are a few potential solutions to your problem:

  • Always elevate the batch file when you run it. If it is being used as a scheduled task, you can check the box for Run this task with highest privileges.

  • Use the Windows Vista/7 Elevation powertoy script to elevate just that command

  • Change the permissions in that specific subdirectory of program files to allow normal users to rename files

    To do this, go to the directory with the file you will be renaming. Right-click on the folder and click on Properties. Go to the security tab. Click advanced and then Change Permissions. Click Add and choose the account which will be running the batch file. Grant that account the necessary permissions - probably create files and delete files at a minimum. Then click OK however many times you need to dismiss all the dialogs. The specified account will now be able to modify files in that folder without needing to elevate.

  • Disable UAC. This is not recommended, as it leaves your system vulnerable

share|improve this answer

Take a look at the elevate tool

http://technet.microsoft.com/en-us/magazine/2007.06.utilityspotlight.aspx

I have used this tool to run various batch files that normally require running in an administrator prompt. It turns out that if you pass the verb "runas" to either the ShellExecute API or to its COM equivalent, the ShellExecute method of Shell.Application, the application launched will prompt for elevation This tool (which consists of the files ElevateCommand.inf, elevate.cmd, and elevate.vbs) adds an elevate command to your system. This lets you launch applications that prompt for elevation from the command line, a script, or the Run dialog. For example, the following command opens Win.ini with Notepad (after prompting for elevation):

elevate notepad c:\Windows\Win.ini

When using the elevate command with Windows Script Host (WSH), Windows PowerShell™ or other scripts, you need to specify the script host executable (such as wscript, cscript, or powershell) as the application. To run a vbs, for example, you’d use this:

elevate wscript “C:\windows\system32\slmgr.vbs” –dli

The help text explains this. Use elevate with -? or with no parameters to see the help text.

The next irritant was that there was no "Run as Administrator" context menu option (when you right-click on the file in Explorer) for most Windows script types. The one exception was for command-shell scripts (those with .bat and .cmd file extensions). So I set out to investigate this. Many of the context menu options for file types are controlled through command keys for the "object" type in the HKEY_CLASSES_ROOT section of the Registry (see Verbs and File Associations in the sidebar for details). It turns out that if that command key is named runas, the command invoked will prompt for elevation.

Elevate HTML Application PowerToy (ElevateHTA.inf)

Elevate Windows PowerShell Script PowerToy (ElevatePowerShellScript.inf)

Elevate WSH Script PowerToy (ElevateWSHScript.inf, elevate.cmd, elevate.vbs)

These PowerToys add a Run as Administrator Explorer context menu entry for HTAs, Windows PowerShell, and Windows Script Host file types respectively. ElevateWSHScript.inf also adds a Run as Administrator with Command Prompt menu entry. (Please read the note in ElevatePowerShellScript.inf before installing it.)

Be sure to extract the power toy into c:\Windows\System32, or if you put the tool elsewhere add the directory to you $PATH variable so you can call it inside of batch scripts easily.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.