如何从外部档案中批量导入变量(例如单词)



我正在为windows CMD开发一个基于文本的RPG。我正在尝试制作一个更友好的游戏保存系统(实际的系统是基于等级代码的,对用户不太友好(如何从.txt或其他存档文件中获取代码并将其用作批处理代码中的变量以下是我正在使用的游戏保存方法:保存:

:save1:
echo ultima partida guardada: P1cWyj (copiar el codigo en el campo de abajo tal como se ve)>C:Users%username%DocumentsProjectText_Datapartida.txt
goto save load
:save2:
echo ultima partida guardada: RxcPy7 (copiar el codigo en el campo de abajo tal como se ve)>C:Users%username%DocumentsProjectText_Datapartida.txt
goto save load
:save3:
echo ultima partida guardada: OeycP5 (copiar el codigo en el campo de abajo tal como se ve)>C:Users%username%DocumentsProjectText_Datapartida.txt
goto save load
:save4:
echo ultima partida guardada: P4gcyK (copiar el codigo en el campo de abajo tal como se ve)>C:Users%username%DocumentsProjectText_Datapartida.txt
goto save load

用于装载:

:5:
cls
echo --------------------------------------------------------------------------------                              
echo                          #### ####  ####  #### ####  #### 
echo                         #   #    #   # # #  #     #   # # 
echo                         #     ####   #   #  #  ####   #   
echo                         ##    #  #   #    ##   #  #   #   
echo                          #### ##### ###  ##### ##### ###  
echo                                          #   #            
echo                                          ####           
echo --------------------------------------------------------------------------------
type C:Users%username%DocumentsPrjctTxt_Datapartida.txt
set /p answer=Escriba la clave de 6 digitos de arriba (respetando mayusculas y minusculas):
if %answer%==P1cWyj goto inicio_1
if not %answer%==a goto 5

示例脚本有一个用于保存位置(标签(更改的宏-Loc,以及一个便于导航到索引标签的Menu宏。这里可以找到用于非索引标签的替代Menu宏。Save宏内置于Loc宏中,并且每当展开Location宏时执行。展开Location宏时,使用子字符串修改将@替换为当前脚本位置的Label。

学习如何使用宏的最佳方法是用它们构建一个小脚本,在开始script body之前定义所需的任何初始变量。

已经添加了关于宏的使用的附加注释,以帮助它们的使用。

@Echo off
@Echo off
:new
rem  *********************  Display any existing character names for continuation or deletion of characters
If Exist "%TEMP%%~n0_*_save.bat" (Echo/Your Characters:&Echo/&(For /F "Delims=" %%G in ('Dir "%TEMP%%~n0_*_save.bat" /B')Do For /F "Tokens=2 Delims=_" %%o in ("%%~nG") Do  < Nul Set /P "=[%%o] ")&Echo/)
:character
Set /P "Name=Name: "
If Exist "%TEMP%%~n0_%Name%_save.bat" (Echo/[C]ontinue / [D]elete?&For /F "Delims=" %%O in ('Choice /N /C:cd')Do If /I "%%O"=="C" (Goto :playon)Else (Del /P "%TEMP%%~n0_%Name%_save.bat" & Goto :character))
If "%Name%"=="" Goto :character
:playon
::: -------------------------------------------------------------------||    MACRO DEFINITIONS
Setlocal DisableDelayedExpansion
(Set n=^^^
%= Do Not Modify =%
)
Set Menu=CLS^&Set "Copt="^&For %%n in (1 2) Do if %%n==2 (%n%
For %%G in (!OPTS!)Do (%n%
Set "opt=@%%~G"%n%
Set "opt=!opt:_= !"^&Set "Opt=!Opt:~,-1!"%n%
Set "Copt=!Copt!%%~G"%n%
Echo/!Opt! [%%~G]%n%
)%n%
Echo/[E]xit%n%
(For /F "Delims=" %%O in ('Choice /N /C !Copt!E')Do If "%%O"=="E" (Endlocal^&Endlocal^&Set "Name="^&Goto :New) Else (CLS^&Goto :@%%O))%n%
) Else Set OPTS=
rem ***  Inventory Macro. Displays all elements for the given group and their current values.
rem :::  Usage: %INV:@=$varname[%
Set "INV=Echo/&(For /F "Tokens=2 Delims==" %%i in ('Set @') Do (Set "VN=%%i"&^< Nul Set /P"=[!VN:$=!:!%%i!] "))&Echo/"
rem ***  Autosave macro. Can be incorperated into other macro's
rem :::  Usage: %Save%
Set SAVE=(For /F "Tokens=1 Delims==" %%i in ('Set $') Do (If not "!%%i!"=="" Echo/Set "%%i=!%%i!"))^>"%TEMP%%~n0_!name!_save.bat"
rem ***  Location Display Macro with autosave macro included
rem :::  Usage: %Loc:@=LocationLABEL%
Set "Loc=(Set "$Loc=@"&Title !$Loc:_= !)&%Save%"
rem ***  Loot box Macro to generate random loot from specified range of an indexed array
rem ***  !random! %%4 + Index# will access an index range between the index # and 4 above the index number.
rem :::  Usage: %Loot:@=index#%
Set "LOOT=(For /F "UsebackQ Delims=" %%i in (`"Set /A i#=!Random! %%4 + @"`) Do For /F "UsebackQ Delims=" %%v in (`"Set /A v#=!Random! %%3 + 1"`) Do (Set "VN=!$Loot[%%i]:$=!"&Echo/You got %%v !VN!&Set /A "!$Loot[%%i]!+=%%v")) 2> Nul & %SAVE%"
rem ***  the below  macros /I /V and /P are not used in this example. - They are an optional method for defining
rem ***  variables prefixed with $ that automatically saves them for reloading
rem :::  usage: %/I:V=Varname%Input Prompt String:
Set "/I=For %%n in (1 2)Do If %%n==2 (Set /P "$V=!$PromptStr:$=!: "&%Save%)Else Set $PromptStr="
rem :::  usage: %/P:V=Varname%VariableValue
Set "/V=For %%n in (1 2)Do If %%n==2 (Set "$V=!str!"&%Save%)Else Set str="
rem :::  usage: %/A:V=Varname%=Equation
Set "/A=For %%n in (1 2)Do If %%n==2 (Set /A "$V!sum!"&%Save%)Else Set sum="
rem *** Wait prompt Macro
rem :::  usage: %Wait:#=Integer value for time in seconds%Wait Prompt String
Set "Wait=For %%n in (1 2)Do If %%n==2 (Timeout # /Nobreak > Nul & (Pause | Echo/!Output!) 2> Nul )Else Set Output="
rem ***  Array definition macro. Asigns the element names to an indexed Groupname (Array), With each element being assigned an initial 0 value
rem :::  Usage: %DefArray%{ArrayName}{Element names as list}
Set "DefArray=For %%n in (1 2) Do if %%n==2 ((If "!i#!"=="" (Set "i#=0"))&For /F "Tokens=1,2 Delims={}" %%G in ("!List!") Do (For %%i in (%%~H) Do (Set "$%%~G[!i#!]=$%%i"&Set "$%%i=0"&Set /A "i#+=1"))) Else Set List="
rem ***  Usage: %List/D/V:$V=ListName%"VarA=Value" "VarB=Value" "VarC=Value"
Set "List/D/V=Set "#$V=0"&Set "$$V="&For %%n in (1 2)Do if %%n==2 (For %%G in (!$V!)Do (Set "%%~G" > Nul &Set /A "#$V+=1"))Else Set $V="
::: -------------------------------------------------------------------||    END MACRO DEFINITIONS
::: -------------------------------------------------------------------||    Script specific varaiable Definition
REM *** required to be enabled PRIOR to macro Use, AFTER definition.
Setlocal EnableDelayedExpansion
rem *** Define Initial script variables here.
rem ::: %DefArray% can be used to intialise array elements with a zero value: %DefArray%{ArrayName}{Element names as list}
rem ::: %List/D/V% can be used to define multiple variables as a list %List/D/V:$V=ListName%"VarA=Value" "$VarB=Value" "VarC=Value"
rem ::: %List/D/V% does not prefix variables with $V by default. Values you wish to save, prefix with $
::: -------------------------------------------------------------------||    Script Load / Return to last Location saved with %Loc:@=Label%
rem *** Loads Bat script containing Set "$Var=Value" output of %Save% and %Loc:@=Label% macros.
IF Exist "%TEMP%%~n0_!name!_save.bat" (
Call "%TEMP%%~n0_!name!_save.bat"
Goto :!$Loc!
)
:Start
%Loc:@=Start%
::: -------------------------------------------------------------------||    Script Body

最新更新