0

So I need a code that will take a text file (we'll cal it list.txt) and turn every line into a variable. To give some context, I have some file names listed in list.txt, which adds and deletes file names occasionally by user request. I want the user of this code to be able to select which document they'd like to open using variables. For example, if list.txt looks like this

list.txt
loading.txt
test1.txt
test2.txt
test3.txt
test4.txt

Then I'd like an automatic variable for every .txt listed. I then would add a simple if statement to open the file matched with the variable.

Am I making this too complicated for myself or is this the only way to do this?

EDIT: I am not attempting something like this:

type list.txt
echo.
echo.
set /p GROUPSELECT= Please type out the FULL name of the group listed: 
CD %grouplist%
type %GROUPSELECT%

It will display the file contents, and then display the specific file chosen by the input. I'd think that the variable might be easier to do more with later though, just a thought.

Edit2

I tried this:

@Echo OFF

FOR /F "Usebackq Delims=" %%a IN (
    "list.txt"
) DO (
    set jim=%%a
)
echo %jim%

PAUSE

%jim% will only be the last line in the text file, so how do I make the next step into making them all different?
theboy
  • 353
  • 2
  • 10
  • [search](https://stackoverflow.com/search?q=%5Bbatch-file%5D+read+file+line) - lots of similar questions. – Stephan Dec 11 '19 at 21:07
  • Move the `echo` command inside the code block (and don't forget [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) or do just `echo %%a`) – Stephan Dec 11 '19 at 21:16
  • @TheBoy your question is confusingly framed. If you DO NOT want the example you put in the edit, then can you better explain what you DO want?? – Ben Personick Dec 11 '19 at 21:35
  • Please explain better, preferably with examples, what exactly you mean by "and turn every line into a variable". Perhaps you are wanting to list the contents of the file as choices (say choice 1 to choice N) and allow the user to pick one of them? – avery_larry Dec 11 '19 at 22:07
  • Also explain what you mean by "how do I make the next step into making them all different". Specifically what do you mean by *them*. – avery_larry Dec 11 '19 at 22:08

4 Answers4

0

Give this a try. I have commented each line of code that should explain what it is doing. Let me know if you need any further explanation.

@echo off

REM FOR commnd is parsing the output of the FINDSTR commands.
REM First FINDSTR command is finding non empty lines in the file
REM Second Findstr command is assigning a number to each line of the file
REM The output is split into two tokens and the number is assigned to %%G and the line of the file to %%H
for /F "tokens=1* delims=:" %%G IN ('findstr /V "^$" list.txt ^|findstr /N ".*"') DO (
    REM create a variable of the lines in the file
    set "file%%G=%%H"
    REM Create a menu of the lines in the file
    echo %%G %%H
    REM Get the number of lines in the output
    set "num=%%G"
)

REM Ask user to chose a number
set /P "filenum=Chose a file number 1 to %num%:"
REM Need to enable delayed expansion to use array variables
setlocal enabledelayedexpansion
REM Check if they type in a correct number and display the file
IF DEFINED file%filenum% type !file%filenum%!
endlocal
pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
0

Is this the sort of thing you're looking for?

@For /F "Delims==" %%# In ('Set # 2^>Nul')Do @Set "%%#="
@For /F Tokens^=1*Delims^=[]^ EOL^= %%# In ('Type "file.txt"^|Find /V /N ""'
)Do @Set "#%%#=%%$"&Echo( %%#.  %%$
:Opt
@Echo(
@Set /P "Opt=Choose a document to open>"
@Set #|Findstr /B "#%Opt%=">Nul||GoTo :Opt
@Call Start "" "%%#%Opt%%%"

Just change the name of the text file containing your list on line 2 as needed.

Compo
  • 36,585
  • 5
  • 27
  • 39
0

@TheBoy your question is confusingly framed. If you DO NOT want the example you put in the edit, then can you better explain what you DO want??

Do you want to say iterate over the list file, and create a choice screen?

@(SETLOCAL EnableDelayedExpansion
  ECHO OFF
  SET "_ChoiceList_File=C:\Admin\ChoiceFile.txt"
  REM Full Character List to populate Choices
  SET "_CharList=0 1 2 3 4 5 6 7 8 9 A B C D E F N X"
)

CALL :Main

( ENDLOCAL
  EXIT /B 0
)

:Main


  REM Now we can Do Our Choices btu lets do it in a Sub Function.
  CALL :MakeChoice _ChoiceResult
  ECHO.
  ECHO The Index Chosen Was: %_Chosen%
  ECHO The Result Matched is:  "%_ChoiceResult%"
  REM ECHO Here you output "%_ChoiceResult%"
  TYPE "%_ChoiceResult%"
GOTO :EOF

:MakeChoice
  cls
  color 1A
  SET %~1="
  SET "_Choices="
  SET "_Chosen="
  SET "_Amount="
  SET "_Choice.17.Value=Next Set!"
  SET "_Choice.18.Value=EXIT!"
  SET "_Choice_Show_Next="
  echo.      Pick a File:
  echo.========================

  REM Create Numbered Array of Choices and output Choices to the Screen
  FOR /F "tokens=* usebackq" %%A IN ("%_ChoiceList_File%") DO (
    SET /A "_Amount+=1"
    SET "_Choice.!_Amount!.Value=%%A"
    IF !_Amount! EQU 16 (
      SET /A "_Amount+=2"
      CALL :MakeChoice "%~1"
      IF DEFINED _Chosen (
        IF !_Chosen! NEQ 17 (
          REM IF !_Chosen! NEQ 18 (
            GOTO :EOF
          REM )
        )
        SET "_Amount="
        SET "_Chosen="
      )
    )
  )

  IF NOT DEFINED _Chosen (
    SET /A "_Amount+=1"
    SET "_Choice.!_Amount!.Value=!"
    SET /A "_Amount+=1"
    SET "_Choice.!_Amount!.Value=EXIT!"
    CALL :MakeChoice "%~1"
  )
GOTO :EOF

:MakeChoice
  CLS
  ECHO.
  SET "_Temp="
  SET "_Choices="
  SET /A "_Skipline= !_Amount! - 1"
  REM Create Choice List to Display only the choices needed.
  FOR %%A IN (%_CharList%) DO (
    SET /A "_Temp+=1"
    IF !_Temp! LEQ !_Amount! (
      IF !_Temp! EQU !_Skipline! (
        ECHO.
        ECHO.=============================
      )
      IF DEFINED _Choice.!_Temp!.Value (
        SET "_Choices=!_Choices!%%A"
        CALL ECHO. %%A :    %%_Choice.!_Temp!.Value%%
      )
    )
  )
  ECHO.

  CHOICE /C !_Choices! /N /M "What File do you want to choose? "
  SET "_Chosen=%ERRORLEVEL%"
  SET "%~1=!_Choice.%_Chosen%.Value!"
GOTO :EOF

)

Ben Personick
  • 3,074
  • 1
  • 22
  • 29
-1

You can try this:

@echo off
setlocal enabledelayedexpansion
set jim=
for /f "delims=] tokens=1*" %%a in ('find /v /n "" ^<list.txt') do ( 
  set jim=!jim! %%b
)
echo Select a file from !jim!
set /p file=
type !file!
pause

This will read all lines from the list.txt and return them within a variable !jim!.

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 1
    Have you tried it Wasif Hasan? I'm going to suggest not! – Compo Dec 12 '19 at 09:48
  • Sorry, There will be %%b in the place of %%a. I have edited the answer. – Wasif Dec 12 '19 at 14:19
  • I accept that you've now fixed a major flaw in your code, however, the question specifically asks, "_I want the user of this code to be able to select which document they'd like to open using variables_", "_I'd like an automatic variable for every .txt listed_", and "_the variable might be easier to do more with later_". I don't believe that your code achieves those requirements. – Compo Dec 12 '19 at 19:12