-1

I have this simple batch file (test.bat):

echo %CD%
pause

No matter where I run this .bat file from (i.e. C:\some\dir\test.bat), my system tells me that I'm in C:\ . This is obviously neither the intended nor the desired behavior, and if I try the exact same script on a different machine, it behaves as expected, giving me current directory of the .bat file. I've been googling for hours now and haven't been able to find anything relevant to my issue. Perhaps I missed up a windows setting somewhere? I have no idea, anyone have any ideas?

dxiang
  • 1
  • 2
  • 1
    Is this happening in a brackets block? Are you changing the directory with a one on the same drive? – npocmaka Jul 17 '18 at 04:04
  • 3
    The current directory of the cmd.exe process is not the same thing as the script directory. The script is `%0`. Single-percent variables support a special "~" syntax to get additional information, such as the [d]rive and [p]ath components. Thus the path of the script directory is `%~dp0`. This includes a trailing backslash. – Eryk Sun Jul 17 '18 at 04:05
  • Further to Eryksuns comment, if you throw in `%~dpnx0` then it will show you the full path `C:\some\dir\test.bat`. Have a check out of [THIS ANSWER](https://stackoverflow.com/a/3679781/5552766) – Drew Jul 17 '18 at 04:08
  • 2
    I guess you set CD to `C:\ ` somewhere. Try to unset CD using `set "CD="`. Or try the other shadow variable `echo %__CD__%`. The `__CD__` variable can't be hidden by a self assigned variable, see also [Why can't I access a variable named CD](https://stackoverflow.com/questions/20156490/why-cant-i-access-a-variable-named-cd-on-windows-7/20169219#20169219) – jeb Jul 17 '18 at 06:25
  • @npocmaka - no, it's not in a brackets block, the code above are the only two lines in the script. – dxiang Jul 17 '18 at 08:58
  • @eryksun - I am aware of that, but I would like to extract the current directory from where the shell is launched. Futhermore, based on documented behavior, the %CD% var should also provide the same information because it's tied to the shell right? – dxiang Jul 17 '18 at 09:03
  • @jeb - The script doesn't contain anything else besides the two lines above; are you implying that %CD% is a global var that can be changed by different shells? I thought it was unique to a single shell. Anyways, I'll try your ideas tomorrow! – dxiang Jul 17 '18 at 09:04
  • Normally `%CD%` is a dynamic variable, but when it is manually set, you only see the value of the environment variable, the dynamic value will be invisible/hidden then. You could also test it on the command line with `echo %CD%` and `set CD`. CD should be undefined – jeb Jul 17 '18 at 09:08
  • 3
    The question said the desired result is the "current directory of the .bat file", which is not necessarily the working directory. As to "from where the shell is launched", typically it's launched by Explorer, using a working directory that could be set by (1) a .LNK shortcut, (2) the user's profile directory if the shell was run from the Win+R dialog, or (3) the script directory if the shell was run by clicking on a .bat or .cmd file. If you run the script from the command line, it should execute in the current shell and thus have the same working directory. – Eryk Sun Jul 17 '18 at 09:19
  • @eryksun - Apologies for any confusion; the desired result IS indeed the "current directory of the .bat file", but I am running the .bat file using option (3): by clicking on the .bat file. If such is the case, shouldn't the working of the shell directory be the same as the directory of the .bat file? This is my issue! Thanks guys. – dxiang Jul 17 '18 at 17:36

1 Answers1

0

Found the issue: there was an AutoRun registry entry that would change directory to C:\ whenever a shell was opened. facepalm

dxiang
  • 1
  • 2