windows 7 - Rename files in a batch file

01
2014-07
  • Ben Bever

    I want to rename file1 to file2 using a batch file in Windows 7 but with no luck.

    Below is the batch file:

    @echo off
    c:
    cd\test
    ren file1 file2
    

    Error message I get is

    syntax error.
    

    Update to my question: However, to be more specific, the following is the content of my script named update.cmd which I will run as administrator in Windows 7.

    Here it is:

    :START
    cls
    cd C:\Program Files\Autodesk\Revit Structure 2012\Program
    ren C:RevitMFC.dll RevitMFC_dll.bak
    

    When I cross check the program lines above using cmd.exe run as administrator, I found out the error msg is "access is denied".

    Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.
    
    C:\Users\Ben>cd c:\program files\autodesk\revit structure 2012\program
    
    c:\Program Files\Autodesk\Revit Structure 2012\Program>ren c:revitmfc.dll revitm fc_dll.bak Access is denied.
    
    c:\Program Files\Autodesk\Revit Structure 2012\Program>
    

    My question: How do I overcome this "Access is denied" ?

  • Answers
  • Dennis

    If

    ren file1 file2
    

    gives a syntax error, there are most certainly spaces in the files' names.

    Try this instead:

    ren "file1" "file2"
    
  • CharlieRB

    There should not be a \ after the CD command.

    Try this:

    @echo off
    c:
    cd test
    ren file1 file2
    
  • CharlieRB

    You are likely getting the "Access Denied" error because the file or folder is read only. The other possibility is the file is in use.

    You can check the file attributes as follows. To check the folder's attributes leave out the file name.

    attrib  [[Drive:][Path] FileName]
    

    Here is more info on how to use the attrib command.

    In order to rename the file with your batch file, you will need to modify the attributes of the file, and possibly the folder. After renaming the file change the attributes back.

    Word of warning; changing the name of a dynamic link library (dll) file can cause the program(s) it is associated with to stop working.


  • Related Question

    rename - Renaming a file using a batch program in Vista/7. Problem with administrator access
  • Questioner

    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.


  • Related Answers
  • nhinkle

    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.

  • nhinkle

    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

  • Richie086

    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.