Search code examples
arrayslistvariablesbatch-filecrash

Creating a batch file array for strings that have spaces in them?


Im trying to write a batch file that allows the user to select a font based off an array of valid names. These are some requirements for how i would like it to work;

  1. user enters the name of the font (case sensitive and with spaces and typed without quotation marks)
  2. batch file checks an array or list to see if what was entered is a valid font name, if its not go back to step 1, if its valid move on
  3. output that valid font name to a variable that i can use later (the font name needs to be case sensitive and have spaces)

This is what i have so far;

@echo off 
set validFonts=Arial,Arial Italic,Arial Bold,Arial Bold Italic
:fontB
set /p fontName=Enter Desired Font Name:
(for %%a in (%validFonts%) do (
    if %fontName% == %%a goto:fontG
))
echo "Invalid Font Name"
goto :fontB
:fontG
echo.
pause

It works when you enter Arial for the font name but closes the window like a crash when entering Arial Italic Any help would be really apreciated as im really pretty new to making batch files.


Solution

  • I found what I was doing wrong, I needed quotes around my array strings AND when i called that array;

    set validFonts="Arial","Arial Italic","Arial Bold","Arial Bold Italic"    
    :fontB
    set /p fontName=Enter Desired Font Name:
    (for %%a in (%validFonts%) do (
        if /i "%fontName%" == %%a goto:fontG
    ))
    echo "Invalid Font Name"
    goto :fontB