-1
@echo off
@CHOICE /C:123
if "%ERRORLEVEL" == "1" GOTO one
if "%ERRORLEVEL" == "2" GOTO two
if "%ERRORLEVEL" == "3" GOTO three

:one
set name = input
goto loop

:two
set name = input1
goto loop

:three
set name = input2
goto loop

:loop
for /f "tokens=*" %%a in (%name%.txt) do (
echo %%a
cls
)
pause

the output is

The system cannot find the file .txt

just want to know how to recall the variable in name. this is the first time use batch and dont know what to do. Thanks a lot.

Thanks

aRickx
  • 31
  • 5
  • It's `"%ERRORLEVEL%"` not `"%ERRORLEVEL"`; it's `Set "name=input"` not `set name = input`. You could of course simplify it by using `If "%ERRORLEVEL%"=="1" Set "name=input" & GoTo loop` etc. – Compo Aug 30 '18 at 20:09

2 Answers2

2

Remove the spaces around the = in your set statements. You are actually creating a variable called name<space>.

aphoria
  • 18,540
  • 7
  • 58
  • 69
1

Based on my comment:

@Echo Off
Choice /C:123
If "%ErrorLevel%"=="1" GoTo one
If "%ErrorLevel%"=="2" GoTo two
If "%ErrorLevel%"=="3" GoTo three

:one
Set "name=input"
GoTo loop

:two
Set "name=input1"
GoTo loop

:three
Set "name=input2"

:loop
For /F Delims^=^ EOL^= %%A In (%name%.txt) Do (Echo %%A
    Timeout 1 /NoBreak>Nul
    ClS)
Pause

 
Alternatively:

@Echo Off
Set "name=input"
Choice /C:123
If ErrorLevel 3 Set "name=%name%2" & GoTo loop
If ErrorLevel 2 Set "name=%name%1"

:loop
For /F Delims^=^ EOL^= %%A In (%name%.txt) Do (Echo %%A
    Timeout 1 /NoBreak>Nul
    ClS)
Pause
Compo
  • 30,301
  • 4
  • 20
  • 32