Explorer quietly sets the working directory — your batch file doesn't

Why a program doesn't work when run from a batch file, but works when clicked

A customer's program failed when launched from a batch file but worked when double-clicked in Explorer. Raymond Chen immediately suspected the current directory: Explorer sets it to the folder containing the clicked file so programs can find ancillary files, while the batch file ran the executable by full path without changing directories. Switching to pushd and popd fixed it.

My guess is that the customer's program looks for ancillary files relative to its current directory, but the batch file ran the program by its full path without changing the current directory to match the program's path.
  1. netsharc

    With drive letters, a neat functionality in DOS/Windows command prompts (don't ask me about Powershell) is that each letter can have its own "current directory".

    So if you have ffmpeg.exe in C:\Program Files\FFMpeg\bin, and you have some source files in D:\SourceVideos, and you want to process them in ffmpeg and output them to E:\Output you can do:

    C: (to navigate to the C: drive)

    cd "C:\Program Files\FFMpeg\bin"

    E: (to navigate to the E:\ drive)

    cd D:\SourceVideos (you remain in E:\, but the "current directory" for D: has been changed).

    cd Output (to enter E:\Output)

    You can now do C:ffmpeg.exe -i D:scene1.mp4 output-scene1.mp4

    and Windows accesses the files in the "current" directory on each drive letter.

  2. xg15

    > Explorer sets the current directory of the program to the directory containing the thing that you double-clicked

    I found the linked article to informative and confirming my suspicion: "The thing that you clicked" doesn't have to be the executable - it could also be a document with the executable registered as launch handler, in which case the current directory will be the directory of the document.

    Would be interesting to know how links and items in shell namespaces are handled.

  3. tveita

    How much are these customers paying for support to have Raymond Chen on first level support to debug their batch files?

  4. PaulHoule

    Well, there is no concept of "the current directory" in the UI of explorer.exe the way there is in CMD.EXE or Bash. You can have multiple directories open and none of them are privileged over the others.

    I think of cultures of command line programs: the node ecosystem very much has the idea that a program "lives" in a a certain directory and you're always going to work with that program in that directory. This is convenient in a lot of ways but... say you are writing a data analysis program and you want to put it on your $PATH and then cd to the place where your data lives and do your work there?

  5. lordnacho

    A related issue is when you set up automation, and it turns out the automation script runs as a different user with different authorizations from the user who tested the script. It's just one of those little annoyances that appears when you try to go from "works on my machine" to "works".

More from this day

2026-10-30