1

I'm writing a script to open folders that our devs commonly access all the time at work both through the command line and through explorer. Right now this is what it looks like:

@echo off

if "%~1" == "" goto noargs
if "%~1" == "/h" goto noargs
if "%~1" == "common1" goto setcommon1
if "%~1" == "common2" goto setcommon2
REM other folders here...

if "%~2" == "-t" (
    set OPENINCMD=1
) else (
    set OPENINCMD=0
)

REM This is where noargs is but I removed it for brevity

:setcommon1
set FOLDERPATH="C:\Users\username\common folder 1"
goto execute
:setcommon2
set FOLDERPATH="C:\Users\username\CommonFolder2"
goto execute

:execute
if %OPENINCMD% EQU 1  (
    cd %FOLDERPATH%
) ELSE (
    explorer.exe %FOLDERPATH%
)

But I would like to be able to set the %FOLDERPATH% variable as I'm parsing through the command line arguments at the start.

I have tried using setlocal enableextentsions enabledelayedexpansion and using !FOLDERPATH! instead of %FOLDERPATH% while executing and also in place of FOLDERPATH while setting the variable. I have also tried using !FOLDERPATH! in both places with no luck. I have endlocal at the end of my file as well.

This is what I envision it looking like but this doesn't set the FOLDERPATH variable when run.

...
if "%~1" == "" goto noargs
if "%~1" == "/h" goto noargs
if "%~1" == "common1" set FOLDERPATH="C:\Users\username\common folder 1"
if "%~1" == "common2" set FOLDERPATH="C:\Users\username\CommonFolder2"
...

What am I missing?

1 Answer 1

1

At least two mistakes at first sight:

  • quoting in set
  • not defined OPENINCMD at the :execute point

This could work:

rem ... your code here 
rem goto :anypoint
goto :eof

:setOPEN
if "%~2" == "-t" (
    set OPENINCMD=1
) else (
    set OPENINCMD=0
)
goto :eof

REM This is where noargs is but I removed it for brevity

:setcommon1
set "FOLDERPATH=C:\Users\username\common folder 1"
goto execute
:setcommon2
set "FOLDERPATH=C:\Users\username\CommonFolder2"
goto execute

:execute
call :setOPEN
if %OPENINCMD% EQU 1  (
    cd "%FOLDERPATH%"
) ELSE (
    explorer.exe "%FOLDERPATH%"
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.