Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a piece of software that is being distributed to several departments for installation onto Windows 7 laptops. They install software from the thumb drive and then they have to run a script to properly configure the software.

Because the script is changing registry files and program files, it requires Admin rights. When running as Admin, it drops into the System32 folder and I no longer have an easy scriptable way to access files that need to be copied from the thumb drive, simply because I don't know for sure what drive letter its going to use on the various machines. Previous installations were on Windows XP and the command window file path stayed within the script folder.

I've found similar questions here and I have already tried Relative Paths, but it can't seem to find the proper folder on the thumb drive or I can't seem to find the proper way to format it.

share|improve this question
up vote 3 down vote accepted

%~dp0 is the path containing the batch file.

For Example: if your script is g:\scripts\batch.cmd then %~dp0 is equivalent to g:\scripts\

So if all of your files are in a folder g:\scripts\files then you could refer to them with %~dp0files\filename.fileextension

also %~d0 would be just the drive g: in the example above

share|improve this answer
Another way is to just run the batch file from the USB drive. You can use %WinDir%\System32 to get the absolute path to the System32 folder. – surfasb Jul 1 '11 at 1:33
Perfect. Exactly what I needed. Thanks. – Jeremy DeStefano Jul 1 '11 at 2:19

How about you write the full path to the location you were working on to a temporary text file - thus just read that file as admin?

As an alternative, perhaps you can set the thumb drive's label to something unique and then look for that drive label when you elevate to admin?

share|improve this answer

A simple two-line solution here at my blog.

@setlocal enableextensions
@cd /d "%~dp0"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .