我正在撰寫代碼以讀取文本檔案的輸出,并將設定第 2 行數值的引數(或變數?)。出于測驗目的,它應該從 :NEW 讀取,但事實并非如此。
這是文本檔案的輸出:
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
這是我到目前為止所擁有的,但顯然是不對的:
@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
uj5u.com熱心網友回復:
注釋:您不能設定包含=.
|是需要特殊處理的特殊字符。
@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=%
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428975.html
標籤:批处理文件
