1

So this may be seemingly pointless, but I'm doing it to try and increase my understanding of how parameters work and how variables can be stored within each other.

What I'm trying to do is this:

--The actual batch file--

@echo off
if "%1" == "/?" ( echo blah blah help etc.)

if not "%1" == "/?" ( echo %%myvar%%)

--end batch file--

--enter cmd prompt--

C:> sim.bat username  
Hsu (i.e. returns the env username)
C:>

--exit cmd prompt--

The reason I want to do %%myvar%% is because I want to pass the parameter as an environment variable. So that sim.bat os or sim.bat username will pull the environment variable.

Stupid I know, but I was able to write it in another way but it is long and tedious:

if "%1" == "homedrive" (echo %homedrive%)
if "%*" =="homeshare" (echo %homeshare%)

...so on and so forth

I looked in multiple places and it's possible I just don't fully understand, but I'm trying to learn, so thanks in advance!

Hsu
  • 13
  • 3
  • i'm not sure but you can try brackets around the name like %(%myvar%)% – s0me1 Oct 21 '18 at 10:51
  • @LHK1337CodingCorner normally you would think so but for whatever reason the cli won’t compile the variable first before passing it. So I get a “the syntax of the command is incorrect error”. I also tried a few variations of %~$Path:1 but to no avail. Any other suggestions? – Hsu Oct 21 '18 at 10:58
  • I suggest you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). Although the question is different, the topic is related... – Aacini Oct 21 '18 at 12:36
  • @Aacini awesome, thanks for the suggestion! I will read up and update! – Hsu Oct 21 '18 at 12:43

1 Answers1

1

Perhaps something along these lines will help you out:

@If Not "%~1"=="" If Not "%~1"=="-?" If Not "%~1"=="/?" If Defined %~1 GoTo Start
@Echo usage/help message& Timeout 2 /NoBreak>Nul& Exit /B
:Start
@Echo Off
Call Echo %%%~1%%& Pause

…or with delayed expansion:

@If Not "%~1"=="" If Not "%~1"=="-?" If Not "%~1"=="/?" If Defined %~1 GoTo Start
@Echo usage/help message& Timeout 2 /NoBreak>Nul& Exit /B
:Start
@Echo Off& SetLocal EnableDelayedExpansion
Echo !%~1!& Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Compo, this works! You are the bomb! I’m not entirely sure why though, would you be willing to explain why you put “@“ at the beginning and why %%%~1%%. I read that %~1 removes surrounding quotes but I don’t see what that actually does here. Sorry if the questions seem stupid! – Hsu Oct 21 '18 at 11:59
  • If you haven't turned `Echo`ing off, which was the case until line `4`, the `@` simply prevents the line being `Echo`ed to the window. As the value of `%1` is variable, the expansion using `~` ensures that any enclosing doublequotes are removed, this means that regardless of whether `%1` contains `"HomePath"` or `HomePath` the code will work the same. It's important because in the `If` comparison both sides of the `==` must be the same, `"HomePath"` != `HomePath` and `""HomePath""` != `"HomePath"`. As for the rest of your question, please search `delayed expansion` on this forum for more info. – Compo Oct 21 '18 at 12:25
  • awesome explanation. Thanks for the help!! – Hsu Oct 21 '18 at 12:27