Results 1 to 9 of 9

Thread: Running batch files as administrator change working directory

  1. #1
    Join Date
    Jan 2004
    Posts
    38

    Running batch files as administrator change working directory

    I need a bit help with Batch files on the new Vista OS. I know if I start a batch file from Explorer the working directory is the directory which holds the file. And if I start a batch file from a shortcut then I can specify a different one, right? But here is the problem. When I start a batch file as Administrator the working directory is always C:\Windows\System32, even if the file is in a dissimilar directory or starts from a shortcut whit an explicit working directory. Due to this all my batch files get fails with all another executable (batch, exe, ecc.) in case not using absolute paths.

    As far as I know the best method to edit the batch file is by changing the current directory. For example like this
    CD \my_directory
    CALL another_batch_file

    Am I right up to here? If yes, then I would like to know if there is reason because the working directory is not respected or is simply a missing functionality. Any idea?

  2. #2
    Join Date
    Sep 2005
    Posts
    61
    Well i am not sure whether this will work for you but i think you should try out putting "cmd /c" before the batch file location. Just try out doing this and see if it fixes the issue.

  3. #3
    Join Date
    Dec 2008
    Posts
    1

    Re: Running batch files as administrator change working directory

    Code:
    @echo off
    for %%F in (folder1 folder2 folder3) DO xcopy /h /r /k /x /y /S /E "\\servername\folder" "\\servername\%%F"
    exit
    This will look at the all the folder names 'folder1, folder2, and folder3' and will copy all files from the origin to them

  4. #4
    Join Date
    Dec 2009
    Posts
    2

    Re: Running batch files as administrator change working directory

    In XP, %~dp0 should give you the path of the batch file. I don't have a box nearby to test Vista.

    http://www.microsoft.com/resources/d....mspx?mfr=true

  5. #5
    Join Date
    Dec 2009
    Posts
    2

    Re: Running batch files as administrator change working directory

    The OP are not the only people looking for the answers to this issue on the internets. Moron.

  6. #6
    Join Date
    Jan 2004
    Posts
    38

    Re: Running batch files as administrator change working directory

    Thank you very much for all your helps guys. But still i have a bit problem, actually a small confusion. I request you to please tell me how exactly i can create the batch file that will copy one file to multiple drives. Also please let me know how to do this with a file that is password protected. Many thanks.

  7. #7
    Join Date
    Apr 2011
    Posts
    1

    Re: Running batch files as administrator change working directory

    just put this as the first line:
    chdir %~dp0

    works in xp, vista, and win7

  8. #8
    Join Date
    Nov 2011
    Posts
    2

    Re: Running batch files as administrator change working directory

    Quote Originally Posted by CapinWinky View Post
    just put this as the first line:
    chdir %~dp0

    works in xp, vista, and win7
    This unfortunately doesn't wok if the batch file was located on a different drive.
    (e.g. on drive D: - chdir changes directory but not the drive letter)

  9. #9
    Join Date
    Nov 2011
    Posts
    2

    Re: Running batch files as administrator change working directory

    you need to use
    chdir /D %~dp0
    to be sure that also the drive letter is correct

Similar Threads

  1. Replies: 5
    Last Post: 29-11-2010, 10:59 PM
  2. Delete the contents of a directory using Batch
    By GUSSIE in forum Operating Systems
    Replies: 3
    Last Post: 11-09-2009, 12:43 PM
  3. Retrieving the current directory in batch
    By Jacques25 in forum Software Development
    Replies: 3
    Last Post: 15-07-2009, 11:36 PM
  4. Can't change file association for DOS batch files
    By pushpendra in forum Windows XP Support
    Replies: 6
    Last Post: 11-03-2009, 12:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •