Evaluate environment variable at runtime with doskey
Asked Answered
D

1

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?

Dispersoid answered 29/9, 2016 at 13:27 Comment(2)
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.Mope
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.Dispersoid
C
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.

Contracted answered 29/9, 2016 at 13:58 Comment(5)
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]Dispersoid
@Fizz, the documentation states $T inside a doskey macro is the same that a & in the command line.Contracted
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.Dispersoid
@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.Contracted
@Fizz, this should explain the why and the howContracted

© 2022 - 2024 — McMap. All rights reserved.