How can I wrap the current directory in strings in CMD?

464 views Asked by At

I need to write a command which will change the current directory and print the NEW directory wrapped in some tags. I thought cd SOMEPATH & echo wkd%cd%wkd would do it but there is a problem.

Here is some example input and output

C:\Users> cd .. & echo wkd%cd%wkd
wkdC:\Userswkd

As you can see, the OLD directory was printed. Why does this happen? I also tried using newlines (since I feed the command though an external program) but that gives problems when starting command line software.

I really hope there is a solution for this.

2

There are 2 answers

1
MC ND On BEST ANSWER

In batch files, lines or blocks of code (code enclosed in parenthesis) are first parsed, then executed and the process repeated on the next line/block. During the parse phase all read operations to obtain a value from a variable are removed from the code, replaced with the value in the variable before starting to execute the code.

In your case, when the line is parsed %cd% is replaced with its value before starting to execute the line and change the folder.

Alternatives:

  • If you separate the command in two lines you will retrieve the correct value.
    cd ..  
    echo wkd%cd%wkd
  • You can enable delayed expansion and change the syntax used to retrieve the variable from %var% to !var! telling the parser the read operation should be delayed until the execution time
    rem inside a batch file
    setlocal enabledelayedexpansion
    cd .. & echo wkd!cd!wkd

    rem from a command line
    cmd /v:on /q /c "cd .. & echo wkd!cd!wkd"
  • You can escape the percent signs to the parser does not see a variable and force a command reevaluation before the echo to get the correct value. You can do it with a call command. It works, but call is slower than other options
    rem inside a batch file
    cd .. & call echo wkd%%cd%%wkd

    rem from command line
    cd .. & call echo wkd^%cd^%wkd
  • Instead of reading a variable, you can execute a command that retrieve the required information
    rem from command line
    cd .. & for %A in (.) do echo wkd%~fAwkd
    cd .. & for /f %A in ('cd') do echo wkd%Awkd

    rem in batch files the percent sign needs to be escaped
    cd .. & for %%A in (.) do echo wkd%%~fAwkd
    cd .. & for /f %%A in ('cd') do echo wkd%%Awkd

(%~fA is the full name of the element referenced by the for replaceable parameter %A)

There is a difference in how for /f and for commands in previous code work

  • for /f is starting a cmd instance that will execute the cd command to output the current directory, output that is processed by the code in the do clause that is invoked for each output line, with the line stored in the replaceable parameter

  • for without modifiers directly retrieves a reference to the element indicated, in this case ., the current folder. In this case %~fA is used to obtain a real full name from the relative . reference into an absolute path

All this options are only doing one thing: delay the retrieval of the current folder until the cd .. has been executed.

0
Adam D. Ruppe On

Give this a try:

cd .. & For /F %A in ('cd') do echo wkd%Awkd

The for loop thing does command output capture as found here: Put command output into a variable

Then I just echo that variable with the stuff around it.

The reason why your original try didn't work is that the %cd% environment variable is set before any of the command executes and is not changed until the interpreter goes on to the next command entirely. It treats foo & bar as a single command with two parts.