0

I'm using a .bat file for installation purposes and I'm using ".\" very often when I need to access files (for example for an unzip) in order so get which directory I'm in.

It worked quite well until yesterday when suddenly I had 2 different behaviours:

  • Lauched from command console (with the console inside the directory): The file works as normal and knows where .\ is at.
  • Launched from windows explorer it also functions if it is not started with administrator rights
  • If launcehd from windows explorer with administrator rights the executing directory (thus the directory which is printed with cd if its the first command in the .bat file) suddenly is c:\windows\system32

This behaviour like I said started just a few days ago (2 days ago we saw it for the first time when we tried that .bat file), before that it worked from windows explorer exactly the same as from the command console.

So my question here is twofold:

  1. Is there anything known as to the cuase of this changed behaviour?
  2. How can I get a handle to the current directory that the .bat file is located in (because the .zip file will always be in the same directory as the .bat file itself), thus how can I solve this problem as I need to start the file with administrator privileges

I found that there is a similar issue in windows vista: Windows batch file starting directory when 'run as admin' although what intrigues me there is why the problem did not appear for weeks and only appeared recently with windows 8

Community
  • 1
  • 1
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • When executed from Explorer, the working directory will be the directory the batch file is located in. There's no way for it to "not know" that directory, assuming you have permissions to view it (and if you didn't, I'm not sure how it would execute). Have the batch file do `cd` (without arguments) and it'll print its working directory. – Sneftel Oct 03 '14 at 09:50
  • Maybe [this](http://stackoverflow.com/a/25440709/2861476) can help – MC ND Oct 03 '14 at 09:50
  • @Sneftel was the same thought here but thanks to trying out the cd I found out something there when I'm executing as administrator ONLY then the problem occurs (as C:\windows\system32 is then the base printed out by cd)......althogh I need to start the .bat as administrator because of permission problems else. I'm rephrasing the question there – Thomas Oct 03 '14 at 09:53
  • Question is updated now. That turn I have to say took me by surprise – Thomas Oct 03 '14 at 09:56

1 Answers1

0

Use this as the line after @echo off:

cd /d "%~dp0"

It will change the working directory to be where the batch file is located.

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Do you also know why it (.\) doesnt work anymore in windows 8? (orif it shouldnt have worked from the beginning) – Thomas Oct 03 '14 at 10:16
  • It has always done that when you run with elevated admin permissions (not just as admin user) - system32 becomes the working directory. .\ is never needed in Windows and doesn't do anything, as the current directory is valid without it. Unix/Linux need it. – foxidrive Oct 03 '14 at 10:56