6

I'm creating a simple environment setup cmd script and I'm using doskey to setup various aliases and macros. The environment I'm on has various repositories and I wanted to create some macros for quick navigation of popular directories.

I currently have the following:

@echo off
Setlocal EnableDelayedExpansion

set PROJECTS_PATH=%SystemDrive%\Projects

echo. Updating path...

REM Update PATH here 

echo. Setting up macros...

doskey np="%SystemRoot%\System32\notepad.exe" $*
doskey np+="%ProgramFiles(x86)%\Notepad++\notepad++.exe" $*
doskey ..=cd ..
doskey trunk=set PROJECT_ROOT=%PROJECTS_PATH%\Trunk ^& cd "!PROJECT_ROOT!"
doskey trunk2=set PROJECT_ROOT=%PROJECTS_PATH%\Trunk2 ^& cd "!PROJECT_ROOT!"
doskey root=cd "%PROJECT_ROOT%"
doskey tools=cd "%PROJECT_ROOT%\tools"

What I was hoping would happen was that I could use the trunk macro to set the PROJECT_ROOT variable, then navigate to this newly set variable using delayed expansion. Then if I use the trunk2 command it would again reset the PROJECT_ROOT variable and navigate to that location. Finally, with the PROJECT_ROOT variable dynamically set, the root and tools macros could be the same regardless of which project root I'm at.

Unfortunately this doesn't work since it seems that PROJECT_ROOT is evaluated when the macro is created. So the result of running the macro trunk is the variable gets set and then execution of cd "".

Is there any way I can have the macro re-evaluate the PROJECT_ROOT variable in case it has changed?

Fizz
  • 3,427
  • 4
  • 27
  • 43
  • 1
    DOSKEY.exe- Recall and edit commands at the DOS prompt, and create macros. In layman terms: You cannot run a Doskey macro from a batch file. – Squashman Sep 29 '16 at 13:56
  • The script I have above is invoked immediately when the cmd prompt is opened to set up my environment. Sorry I'm not sure what exactly you're trying to point out. – Fizz Sep 29 '16 at 13:58

1 Answers1

4

You don't need delayed expansion to get it working

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "PROJECTS_PATH=%SystemDrive%\Projects"

    doskey trunk=cd /d "%PROJECTS_PATH%\trunk" $t set "PROJECT_ROOT=%%cd%%"
    doskey trunk2=cd /d "%PROJECTS_PATH%\trunk2" $t set "PROJECT_ROOT=%%cd%%"

    doskey root=cd /d "%%PROJECT_ROOT%%"
    doskey tools=cd /d "%%PROJECT_ROOT%%\tools"

Instead of setting the variable and changing to the target folder, change the active directory and then set the variable.

The %%var%% inside the batch file will be converted to %var% without expanding the variable while creating the macro. Variable will be expanded when the macro is called.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • That works, great. Why is it thought that when I use `$t` I basically get some sort of echo. For example when I use `trunk` I get the following output: `C:\Windows\System32>trunk` `C:\Projects\Trunk>` `C:\Projects\Trunk> [cursor is here]` – Fizz Sep 29 '16 at 14:03
  • @Fizz, the [documentation](https://technet.microsoft.com/en-us/library/bb490894.aspx) states `$T` inside a doskey macro is the same that a `&` in the command line. – MC ND Sep 29 '16 at 14:30
  • Sure I get that but if I use `^&` instead of `$t` in the macro, I don't get the echo seen above. Just not sure what about `$t` causes that. – Fizz Sep 29 '16 at 14:31
  • @Fizz, If you use `^&` doskey is not aware of multiple commands, the full line is processed by `cmd`, but using `$t` it is doskey who executes the commands one after the other, and an additional line (the prompt) is echoed for each command. – MC ND Sep 29 '16 at 14:44
  • @Fizz, [this](http://stackoverflow.com/a/39930691/2861476) should explain the *why* and the *how* – MC ND Oct 08 '16 at 09:22