---
title: "Puzzling out why a program doesn't work when run from a batch file, but works when double-clicked from Explorer"
subtitle: "Mimicking the operation more accurately."
slug: puzzling-out-why-a-program-doesnt-work-when-run-from-a-batch-file-but-works-when-double-clicked-from-explorer
url: https://listedarticles.com/articles/puzzling-out-why-a-program-doesnt-work-when-run-from-a-batch-file-but-works-when-double-clicked-from-explorer
canonical_url: https://devblogs.microsoft.com/oldnewthing/20261026-00/?p=112797
content_type: blog_post
language: en
published_at: 2026-10-26T14:00:00.000Z
updated_at: 2026-10-30T11:11:59.798Z
author: "Raymond Chen"
authored_by: human
publisher: "The Old New Thing (Microsoft Dev Blogs)"
publisher_url: https://devblogs.microsoft.com/oldnewthing/
topics: ["programming", "software-engineering", "developer-tools"]
license: all-rights-reserved
word_count: 261
reading_minutes: 1
citation: "Raymond Chen, The Old New Thing (Microsoft Dev Blogs). \"Puzzling out why a program doesn't work when run from a batch file, but works when double-clicked from Explorer.\" 26 Oct 2026. https://devblogs.microsoft.com/oldnewthing/20261026-00/?p=112797 (all-rights-reserved)"
# The full text follows. The web page shows an extract and sends readers
# to the source above; quote the citation and link the canonical URL.
---

# Puzzling out why a program doesn't work when run from a batch file, but works when double-clicked from Explorer

*Mimicking the operation more accurately.*

> Raymond Chen explains why a customer’s program failed when launched from a batch file but worked from Explorer: Explorer sets the current directory to the program’s folder, so programs that look for ancillary files there break unless the batch file changes directory first (e.g. with pushd/popd).

A customer asked for help writing a batch file to launch a program. For some reason, the program displayed an error message when the batch file ran it, whereas it worked fine when they double-clicked the program from Explorer.

I immediately suspected that they were not setting the current directory.

Some time ago, I noted that when you double-click a file in Explorer,  [Explorer sets the current directory of the program to the directory containing the thing that you double-clicked](https://devblogs.microsoft.com/oldnewthing/20101110-00/?p=12313 "Your debugging code can be a security vulnerability: Loading optional debugging DLLs without a full path"). This is done to accomodate programs that look for ancillary files in the current directory. The example I had was a hypothetical LitWare Writer program that, when given an ABC.LIT file, looks for related files like ABC.LTC (the table of contents) and ABC.LDC (the custom dictionary) in the current directory.

This also happens if you double-click the `.exe` file: The current directory is set to the directory that contains the executable.

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.

The customer confirmed that explicitly changing the directory fixed the program. (More specifically, they used `pushd` to change the directory and then `popd` to restore the original directory.)

**Bonus chatter**: It didn’t come to it, but my second idea would have been to check whether the program had  [set a custom entry under `App Paths`](https://devblogs.microsoft.com/oldnewthing/20110725-00/?p=10073 "How is it possible to run Wordpad by just typing its name even though it isn't on the PATH?") to ask for additional directories to be added to the `PATH` environment variable when the program is run.
