Click here to Skip to main content
11,922,381 members (30,657 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked

Running a .bat file as administrator - Correcting current directory

, 20 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to set the current directory correctly when running batch scripts as administrator
When you run a batch file as administrator under windows vista and 7 the current directory gets set to C:\windows\system32. This can prevent your scripts from working correctly if you use relative paths.
To fix this problem, include these two lines at the top of your .bat script:
@setlocal enableextensions
@cd /d "%~dp0"
This will change the current directory to the location of the .bat file.
How it works:
@setlocal enableextensions - controls the visibility of environment variables[^] and enables cmd extensions[^].
@cd /d "%~dp0" - Changes the current directory to %~dp0 which is a special batch parameter[^] that expands to the drive and directory that batch file is located in.
%0 expands to the full path and file name of the batch file, adding the ~dp modifier in the middle to make %~dp0 reduces the %0 value to just the drive and path.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Simon P Stevens
Software Developer
United Kingdom United Kingdom
I discovered programming aged 11 with my school's BBC micro, and a book titled "Write your own space action games". (Their ideas of "space action" games were very different to mine. My ideas didn't include a bunch of * signs controlled via a text based menu)

I got hooked on VB for a while (mainly because I discovered I could replace the * signs with .bmp files) until someone pointed out the wonderful world of objects and Java. I also went thought a c++ phase.

I've now been a professional developer for 5 years.

My current language of choice is C#. I spend my free time playing with XNA and microcontrollers.

You may also be interested in...

Comments and Discussions

QuestionGreat My 5 Pin
Kaushal_Ecstasy24-Dec-14 23:57
memberKaushal_Ecstasy24-Dec-14 23:57 
GeneralMy vote of 5 Pin
Member 838737319-Jun-13 13:50
memberMember 838737319-Jun-13 13:50 
GeneralMy vote of 5 Pin
Brijesh_Guptra30-May-13 1:40
memberBrijesh_Guptra30-May-13 1:40 
GeneralMy vote of 5 Pin
wout de zeeuw31-Oct-12 4:15
memberwout de zeeuw31-Oct-12 4:15 
GeneralMy vote of 5 Pin
TheMasterCylinder16-Oct-12 8:16
memberTheMasterCylinder16-Oct-12 8:16 
GeneralMy vote of 5 Pin
polonggo7-Jun-12 19:37
memberpolonggo7-Jun-12 19:37 
It just simply works. Thanks.
GeneralReason for my vote of 5 Great Help, Looking for It. Pin
sumantamca22-Feb-12 21:32
membersumantamca22-Feb-12 21:32 
GeneralReason for my vote of 5 Thank you so much! Pin
_WhiteRussian12-Nov-11 5:04
member_WhiteRussian12-Nov-11 5:04 
GeneralGood spot - thanks for sharing Pin
Reiss2-Sep-11 0:55
memberReiss2-Sep-11 0:55 
GeneralSensible advice. Love, you can easily find this documented b... Pin
Rasqual Twilight21-Feb-11 9:24
memberRasqual Twilight21-Feb-11 9:24 
GeneralWherever did you find that ~dp ? Good job ! ;) Pin
Love Nystrom28-Oct-10 6:39
memberLove Nystrom28-Oct-10 6:39 
GeneralRe: Wherever did you find that ~dp ?Good job ! ;) Pin
PIEBALDconsult22-Mar-12 13:10
memberPIEBALDconsult22-Mar-12 13:10 
GeneralReason for my vote of 5 Tricks like these are stumbled upon ... Pin
E.F. Nijboer22-Oct-10 13:09
memberE.F. Nijboer22-Oct-10 13:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151120.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Simon P Stevens
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid