Home > Blockchain >  Setting parameter based on output of text file?
Setting parameter based on output of text file?

Time:02-14

I am working on code to read the output of a text file and will set a parameter (or variable?) of the number value of line 2. For testing purposes, it should read from the :NEW, but it doesn't.

This is the output of the text file:

var1=RANK|SCORE|NAME
var2=1|52456|NLM
var3=2|24610|ABS
var4=3|23385|ABT
var5=4|22158|ABU
var6=5|21821|ABV
var7=6|20272|ABW
var8=7|19476|ABX
var9=8|18864|ABY

This is what I have so far, but it obviously isn't right:

@echo off
setlocal ENABLEDELAYEDEXPANSION
color 0a
set vidx=0
for /F "tokens=*" %%A in (hiscores.txt) do (
    SET /A vidx=!vidx!   1
    set var!vidx!=%%A
)
set var

pause

set /p %var2=%

if %var2=% LSS 1|52456|NLM GOTO INVALID
if %var2=% EQU 1|52456|NLM GOTO NEW
if %var2=% GTR 1|52456|NLM GOTO SCORE

:INVALID
echo Sorry, you did not win any tickets this time!
PING -n 3 127.0.0.1 > NUL

:SCORE
echo Congratulations on a new high score!
PING -n 3 127.0.0.1 > NUL

:NEW
echo Congratulations on a new high score!
PING -n 3 127.0.0.1 > NUL
pause

CodePudding user response:

Comments: You cannot set an environment variable name which contains =.

| is a special character which needs special processing.

@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q71082577.txt"

FOR /f "usebackq delims=" %%b IN ("%filename1%") DO (SET "%%b")
SET var

SET /p "score=New score [number,name] ? "

SET "newscore="
FOR %%b IN (%score%) DO IF DEFINED newscore (SET "newname=%%b") else (SET "newscore=%%b")

SET "pos="
FOR /f "tokens=1-4delims==|" %%g IN ('set var') DO IF %newscore% gtr %%i IF NOT DEFINED pos SET /a pos=%%h 1

IF NOT DEFINED pos GOTO notontable

SET pos

:: shuffle lower scores down 1 place

IF %pos% lss 10 FOR /L %%p IN (9,-1,%pos%) DO (
 SET /a from=%%p - 1
 CALL CALL SET "var%%p=%%%%var%%from%%%%%%"
)

SET var

:: put new score into table

SET "var%pos%=new|%newscore%|%newname%"

SET var

:: Now correct column1 of data

FOR /L %%b IN (2,1,9) DO SET /a c1=%%b-1&FOR /f "tokens=1-4delims==|" %%g IN ('set var%%b') DO CALL SET "var%%b=%           
  • Related