简体   繁体   中英

Batch file question: How can I load a text table from a file (with exclamation marks) into a global array?

My goal is to load a text file into a global array, and the file contains exclamation marks. I want to then bubble sort the array, and save the contents to a new file. Unfortunately I cannot even get past loading data into an array. I tried using for loops, but it seems delayed expansion is both needed and not needed. Of course, any variables made inside the loops are local variables due to the setlocal command, correct? So once endlocal is reached (in order to toggle delayed expansion), the scope is gone. I was thinking that I need the array to be global so that I can continue manipulating it later on. (I tried to add set commands at the end of endlocal to pass the local values up to higher scope, but my attempts got me nowhere.)

setlocal enabledelayedexpansion
set /a count=0
set "file=list.txt"

echo Extracting titles and IDs...

for /f "usebackq delims=; tokens=1,2* skip=1" %%a in ("%file%") do (
    set /a count+=1
    setlocal disabledelayedexpansion
    set "title=%%b"
    set "gameid=%%a"
    setlocal enabledelayedexpansion
    for /f "delims=- tokens=1,2*" %%x in ("!gameid!") do (
        echo %%y %%x "!title!"
        set "entry[!count!]=%%y %%x !title!"
    echo %count% - !count!
    )
    endlocal
    endlocal
)


pause

The input file looks like:

ID;Title;Other;Headings;We;Dont;Need
ABC-456;My second game ever! Even better!;foo;bar;blah;blah;blah
XYZ-123;My first game ever! Yes!;foo;bar;blah;blah;blah

My end goal is to create a file in the form

123 XYZ "My first game ever! Yes!"
456 ABC "My second game ever! Even better!"

after sorting by the numerical values. If anyone could help just get through the first part (loading data into an array), that would be greatly appreciated.

You can avoid delayed expansion completely by not using variables, when you can use for variables.

The only spot that would require delayed expansion, can be done by an alternative:

call set "entry[%%count%%]=%%Y %%X %%B"

call forces another layer of expansion. for that to work properly, you have to make sure, %%var%% doesn't get confused with %%v and ar%% . That's the reason, I changed the for variable to uppercase ( %%a is different from %%A ).

@echo off
setlocal disabledelayedexpansion
set /a count=0
set "file=list.txt"

echo Extracting titles and IDs...
for /f "usebackq delims=; tokens=1,2* skip=1" %%A in ("%file%") do (
    set /a count+=1
    for /f "delims=- tokens=1,2*" %%X in ("%%A") do (
        call set "entry[%%count%%]=%%Y %%X %%B"
    )
)
(for /f "tokens=1,* delims==" %%A in ('set entry[') do @echo %%B)|sort
pause

(Note: the sorting works only, when all the items have a three-digit number)

Output with your example data:

Extracting titles and IDs...
123 XYZ My first game ever! Yes!
456 ABC My second game ever! Even better!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM