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 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
add comment

3 Answers

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
add comment

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
add comment

A simple two-line solution here at my blog.

http://chienyiing.blogspot.com/2014/04/batch-file-run-as-administrator-changes.html

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

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.