Mike Warren
Mike Warren

Reputation: 3886

Batch file variables won't set

I am writing a CMD script to generate documentation markdown pages for my GitHub repository. I have decided to give the script a default directory for the project and its documentation folder, and if the end user wants to use a different one, they must specify it, before the next step.

My code is something like:

echo.
setlocal enabledelayedexpansion
set projectDirectory=GroupManagementAppServer
set documentationFolder=documentation
rem ask user for confirmation of projectDirectory,documentationFolder to use
choice /m "By default, project directory is %projectDirectory% and documentation is stored in %documentationFolder%. Should I use these?"
rem if no
if %errorlevel% == 2 (
rem get projectDirectory,documentationFolder from user
set /p relativeDocumentationPathname=Please enter relative pathname to the documentation folder: 
rem parse input
call :getAbsolutePath %relativeDocumentationPathname%
set documentationFolder=%_absolutePath%
set projectDirectory="%documentationFolder%\.."
)
echo %_absolutePath%
echo %documentationFolder%
echo %projectDirectory%

:getAbsolutePath
SETLOCAL
for %%i in ("%1%") do (
set filedrive=%%~di
set filepath=%%~pi
set filename=%%~ni
set fileextension=%%~xi
)
ENDLOCAL & SET _absolutePath=%filedrive%%filepath%%filename%%fileextension%

thus far, and when the echos complete, it's as if documentationFolder was never redefined! What the heck is going on, and how do I fix this, so that I can implement the rest of this and move on to actually getting some documentation on?

Upvotes: 0

Views: 102

Answers (2)

aschipfl
aschipfl

Reputation: 34979

Here is the fixed code with delayed expansion properly applied, the sub-routine reduced and some minor improvements, mainly related to quotation:

echo/
setlocal EnableDelayedExpansion
set "projectDirectory=GroupManagementAppServer"
set "documentationFolder=documentation"
rem // Ask user for confirmation of `projectDirectory`, `documentationFolder` to use:
choice /M "By default, project directory is '%projectDirectory%' and documentation is stored in '%documentationFolder%'. Should I use these?"
rem // If no:
if %errorlevel% == 2 (
    rem // Get `projectDirectory`, `documentationFolder` from user:
    set /P relativeDocumentationPathname="Please enter relative pathname to the documentation folder: "
    rem // Parse input:
    call :getAbsolutePath "%relativeDocumentationPathname%"
    set "documentationFolder=!_absolutePath!"
    set "projectDirectory=!documentationFolder!\.."
)
echo %_absolutePath%
echo %documentationFolder%
echo %projectDirectory%
goto :EOF

:getAbsolutePath
setlocal
for /D %%I in ("%~1") do (
    set "filespec=%%~fI"
)
endlocal & set "_absolutePath=%filespec%"

Upvotes: 1

Magoo
Magoo

Reputation: 80203

I'd suggest you use the SO search facility in the top black bar and try to find delayedexpansion. There are hundreds of items on this matter.

Fundamentally, when a block (parenthesised series of statements) is encountered, the entire block is evaluated, substituting the then-current values of the variables and once that is done, the code is executed.

In your case, call echo %%var%% would show the modified values, or using the modified values within a subroutine (like call :arouthethatechosthevalues) would implement the new values.

Upvotes: 0

Related Questions