Link to home
Start Free TrialLog in
Avatar of jbobst
jbobst

asked on

Windows 7 Batch file problem - directory changes when envoking admin rights

I have a simple batch file that copies three files to my \%windir%\syswow64 directory and then does a regsvr32 on these three files.  The problem is, when I just run the batch file normally, it gives me an access denied error as Windows doesn't allow non-elevated copy commands to the syswow64 directory.  So then, you would think that you could just right click on the batch file and say "run as administrator".  The problem then is that when you run a batch file with elevated admin rights, it changes the current directory path for some reason and it can no longer find the files I am trying to copy!  How do I fix this?  Is there a way to reset the current directory in my batch file before it gets to the copy lines?

So just to clarify, I have a folder on the root (it could be anywhere) of my c:\ drive.  In this folder is the batch file and the three files I want to copy.  When I run the batch file as administrator, it changes the directory in the command prompt to c:\windows\system32 and it obviously cannot find the three files it needs to copy since they are not in the system32 folder to begin with.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbobst
jbobst

ASKER

Perfect!  That did it!  Thanks so much.  Not sure how you knew that, but that did the trick.
You can reference the path to the BAT file in the BAT file itself as %~dp0, so just CD or PUSHD to that folder at the top of the BAT, like:

@echo off
cd /d %~dp0
copy f1.txt c:\temp
copy f2.txt c:\temp
copy f3.txt c:\temp

Open in new window

~bp
Well, there were no posts when I STARTED typing, but I got interrupted for a bit right near the end...

Glad you got a working solution.

~bp