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’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.</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’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’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>
# 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
1 reaction

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 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)
- [](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/)
A technical debugging story from Microsoft's Raymond Chen about a Windows Explorer crash caused by a thread executing from an unloaded third-party DLL, highlighting how developers investigate such issues.
Raymond Chen explains how Windows 95 guessed whether a program was a setup program by checking the executable name for magic words like 'setup' or 'install'.
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.
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.