1

how to change the current directory and print it in one line?

I tried C:\dir1> cd /d D:\dir2 && echo %cd%

but it prints C:\dir1

geek175
  • 127
  • 1
  • 7
  • 1
    [duplicate](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) (although it looks very different, it explains *why* your code doesn't work). But Christian's answer is a nice workaround / alternative solution. – Stephan Feb 01 '23 at 22:48

1 Answers1

2

You should be able to do cd /d D:\dir2 && cd

According to the windows cmd help page for cd/chdir this will display your current directory:

Type CD drive: to display the current directory in the specified drive.

Type CD without parameters to display the current drive and directory.

  • how to save the output to a variable? `cd /d D:\dir2 && set var1=???` – geek175 Feb 02 '23 at 08:36
  • 1
    You should be able to do `cd /d D:\dir2 && for /f %i in ('cd') do set var=%i` if you are using the command prompt directly you have to use %i but if you are doing a batch script it should be %%i. Saving the output to a variable is further answered at [this question](https://stackoverflow.com/questions/2323292/assign-output-of-a-program-to-a-variable-using-a-ms-batch-file) – Christian Vaughn Feb 02 '23 at 17:11