Search code examples
windowsbatch-filecmd

Batch won't return what it's supposed to


I'm still pretty new to this, but I'm trying to start server.exe with some parameters, but the %cmdline% does not return.

NOTE: When I replace %cmdline% with -logfile=server/output_log.txt -batchmode -nographics -dedicated -configfile=server/serverconfig.xml it works fine, but I need it to read from the data file.

BATCH

for /f "tokens=2 delims==" %%i in ('type data.dat ^| findstr "EXENAME="') do set exename=%%i
for /f "tokens=2 delims==" %%i in ('type data.dat ^| findstr "EXEPATH="') do set exepath=%%i
for /f "tokens=2 delims==" %%i in ('type data.dat ^| findstr "CMDLINE="') do set cmdline=%%i
start /normal %exepath%%exename% %cmdline%

data.dat

EXENAME=server.exe
EXEPATH=./server/
CMDLINE=-logfile=server/output_log.txt -batchmode -nographics -dedicated -configfile=server/serverconfig.xml

Solution

  • The problem is that your code

    for /f "tokens=2 delims==" %%i in ('type data.dat ^| findstr "CMDLINE="') do set cmdline=%%i
    

    is parsing the line of DATA.DAT containing CMDLINE= using the equal sign = as delimiter, and the line contains other equal signs.

    a simple solution is to instruct FOR to return the first and the rest of tokens and then take only the second (that contains all the tokens except the first)

    for /f "tokens=1,* delims==" %%i in ('type data.txt ^| findstr "CMDLINE="') do set cmdline=%%j