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

The Old New Thing (Raymond Chen) News

Summary

Raymond Chen 解释为何程序在资源管理器中双击可正常运行,却在批处理文件中运行时报错:根本原因在于当前工作目录不同,批处理脚本需用 pushd 切换到程序所在目录。

<p>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.</p> <p>I immediately suspected that they were not setting the current directory.</p> <p>Some time ago, I noted that when you double-click a file in Explorer, <a title="Your debugging code can be a security vulnerability: Loading optional debugging DLLs without a full path" href="https://devblogs.microsoft.com/oldnewthing/20101110-00/?p=12313"> Explorer sets the current directory of the program to the directory containing the thing that you double-clicked</a>. 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 <tt>ABC.LIT</tt> file, looks for related files like <tt>ABC.LTC</tt> (the table of contents) and <tt>ABC.LDC</tt> (the custom dictionary) in the current directory.</p> <p>This also happens if you double-click the <code>.exe</code> file: The current directory is set to the directory that contains the executable.</p> <p>My guess is that the customer&#8217;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&#8217;s path.</p> <p>The customer confirmed that explicitly changing the directory fixed the program. (More specifically, they used <code>pushd</code> to change the directory and then <code>popd</code> to restore the original directory.)</p> <p><b>Bonus chatter</b>: It didn&#8217;t come to it, but my second idea would have been to check whether the program had <a title="How is it possible to run Wordpad by just typing its name even though it isn't on the PATH?" href="https://devblogs.microsoft.com/oldnewthing/20110725-00/?p=10073"> set a custom entry under <code>App Paths</code></a> to ask for additional directories to be added to the <code>PATH</code> environment variable when the program is run.</p> <p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261026-00/?p=112797/">Puzzling out why a program doesn&#8217;t work when run from a batch file, but works when double-clicked from Explorer</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
Original Article
View Cached Full Text

Cached at: 10/27/26, 01:57 AM

# Puzzling out why a program doesn't work when run from a batch file, but works when double-clicked from Explorer - The Old New Thing Source: [https://devblogs.microsoft.com/oldnewthing/20261026-00/?p=112797/](https://devblogs.microsoft.com/oldnewthing/20261026-00/?p=112797/) October 26th, 2026 ![like](https://devblogs.microsoft.com/oldnewthing/wp-content/themes/devblogs-evo/images/emojis/like.svg)1 reaction ![](https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2019/02/RaymondChen_5in-150x150.jpg) 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)\. 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 anABC\.LITfile, looks for related files likeABC\.LTC\(the table of contents\) andABC\.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)to ask for additional directories to be added to the`PATH`environment variable when the program is run\. ### Category ### Topics ## Author ![Raymond Chen](https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2019/02/RaymondChen_5in-150x150.jpg) Raymond has been involved in the evolution of Windows for more than 30 years\. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie\-jeebies\. The Web site spawned a book, coincidentally also titled The Old New Thing \(Addison Wesley 2007\)\. He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information\. ## Read next ## Stay informed Get notified when new posts are published\. Follow this blog - [https://twitter.com/ChenCravat](https://twitter.com/ChenCravat) - [![youtube](https://devblogs.microsoft.com/oldnewthing/wp-content/themes/devblogs-evo/images/social-icons/youtube.svg)](https://www.youtube.com/playlist?list=PLlrxD0HtieHge3_8Dm48C0Ns61I6bHThc) - [https://github.com/oldnewthing](https://github.com/oldnewthing) - [https://devblogs.microsoft.com/oldnewthing/feed/](https://devblogs.microsoft.com/oldnewthing/feed/)

Similar Articles

Don't Put Tilde In Your Path

Hacker News Top

一篇技术博文指出在 shell 配置中用引号包裹的 `PATH="$PATH:~/.local/bin"` 不会展开波浪号,导致当前目录下的字面 `./~/` 路径被加入 PATH,带来潜在劫持风险;建议改用 `$HOME`,并介绍了 nono 沙箱工具的相关警告与排查方法。

A compatibility note on the abuse of Windows window class extra bytes

The Old New Thing (Raymond Chen)

Raymond Chen discusses a historical Windows compatibility issue where some 16-bit programs abused window class extra bytes to store private data, and how Microsoft blocked the loophole for 32-bit and 64-bit programs while maintaining backward compatibility.

@VincentLogic: Found a pretty cool CLI tool! OfficeCLI lets you work with Word, Excel, and PPT files right in the terminal — no Office installation needed. Create, read, and modify files with ease, making it super handy for automation scripts. The best part? Once installed, Claude Code, Cursor, and other AI coding assistants can …

X AI KOLs Timeline

OfficeCLI is an open-source command-line tool that lets you create, read, and modify Word, Excel, and PPT files in the terminal without installing Office. It integrates with AI coding assistants like Claude Code and Cursor, making it ideal for automation scripts and batch file processing.