0
cd "C:\path\to\directory\" >nul & echo %CD%

Say I run that in C:\dir. I'd expect it to output C:\path\to\directory since that's what it does if I run each command individually and in succession. But it doesn't do that. When the two commands are sorta concatenated together it outputs the current path - eg. C:\dir.

Any ideas why? Alternatively, any ideas as to how I can get the full path from a relative path via the CLI?

4

1 回答 1

1

如果您将两个命令连接起来,它们的作用就像一个代码块,并且所有 %variables% 都不能更改它们的值。您必须使用延迟扩展:

@ECHO OFF & setlocal enabledelayedexpansion
cd "C:\path\to\directory\" >nul & echo !CD!

输出将是:

C:\path\to\directory
于 2013-04-19T22:20:37.720 回答