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 , changes echo %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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -