batch file - Windows script doesn't work when user types in a whitespace -
i'm writing windows script in batch. have problem whitespaces in variables. when user types in space, script breaks.
here's part of script:
:package set /p packagename="set package name:" if [%packagename%] equ [] ( echo empty package name. goto package ) else ( set "packagename=%packagename: =%" echo %packagename% pause )
this schould work:
@echo off setlocal enabledelayedexpansion :package set /p packagename="set package name:" if "%packagename%"=="" ( echo empty package name. goto package ) else ( set packagename=%packagename: =% echo !packagename! pause )
there 2 modifications script:
[%packagename%] equ []
replaced"%packagename%"==""
- i've added
setlocal enabledelayedexpansion
, changesecho %packagename%
echo !packagename!
the second point because changing value of variable inside if-construction. interpreter doesn't know new value @ "compile" time, have evaluate variable @ run time. that's why need setlocal enabledelayedexpansion
, !...!
instead of %...%
. forces expansion @ run time.
Comments
Post a Comment