0

The following achieves double dereferencing:

deref.cpp

#include <stdio.h>
#include <stdlib.h>

void explain()
{ fputs("deref variable\n"
        " Dereference a windows variable\n", stderr);
}

int main(int argc, char **argv)

{ if (argc <= 1 || argv[1][0] == '-' || argv[1][0] == '?')
    explain();
    
  char * dir = getenv(argv[1]);
  if (dir)
    fputs(dir, stdout);
    
  return 0;
}

cdd.bat

@echo off

if "%1" == "" (
    set t=%cd%
    set vv=%t:^\=/%
    echo %vv:^"=%
) else (
    if "%1" == "-" (
        cd %lastdir%
    ) else (
      set lastdir=%cd%

      for /f %%a in ('where %0') do set here=%%~dpa

      for /f %%a in ('%here%deref %1') do set tgt=cd %%a

      call %tgt%
  )
)

Question:

Is there a way to do this without using the program deref.cpp?

Is there a way to do it using only the best programming language in the world? :)

It has been claimed that this is relevant to the question (but it is not)

Variables are not behaving as expected

When uploading the cdd.bat code one has to be very careful about spaces as .bat language is very sensitive to these.

Questaware
  • 87
  • 5
  • Your actual question is off topic for this site, which deals with issues with your programming code. As for the tagged batch file code, it has errors, the majority of which can be solved by advice in the linked duplicate. – Compo Aug 12 '23 at 12:04
  • This question is on-topic and not a duplicate. – Kevin Krumwiede Aug 15 '23 at 18:49
  • `for /f %%a in ('where %0') do set here=%%~dpa` = `set "here=%%~dp0"` (the quotes make it safe against stray spaces). And yes, the duplicate **is** relevant: `!here!` and `!tgt! **needs** delayed expansion. Still no idea what you are trying to do. – Stephan Aug 17 '23 at 06:48
  • I am trying to make this work without the use of deref.exe. If you can do that then please tell me how. If there is an environment variable there then cdd there Thake you there. – Questaware Aug 19 '23 at 12:25

0 Answers0