批处理文件问题:如何将文本 table 从文件(带有感叹号)加载到全局数组中?
Batch file question: How can I load a text table from a file (with exclamation marks) into a global array?
我的目标是将文本文件加载到全局数组中,并且该文件包含感叹号。然后我想对数组进行冒泡排序,并将内容保存到一个新文件中。不幸的是,我什至无法将过去的数据加载到数组中。我尝试使用 for 循环,但似乎延迟扩展既需要又不需要。当然,由于 setlocal 命令,循环内的任何变量都是局部变量,对吗?因此,一旦到达 endlocal(为了切换延迟扩展),作用域就消失了。我在想我需要这个数组是全局的,这样我以后就可以继续操作它了。 (我试图在 endlocal 的末尾添加 set 命令以将本地值传递到更高的范围,但我的尝试无济于事。)
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
输入文件如下所示:
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
我的最终目标是创建一个格式为
的文件
123 XYZ "My first game ever! Yes!"
456 ABC "My second game ever! Even better!"
按数值排序后。如果有人可以帮助完成第一部分(将数据加载到数组中),将不胜感激。
当你可以使用 for
变量时,你可以通过不使用变量来完全避免延迟扩展。
唯一需要延迟扩展的地方,可以通过替代方法完成:
call set "entry[%%count%%]=%%Y %%X %%B"
call
强制再扩展一层。为了使其正常工作,您必须确保 %%var%%
不会与 %%v
和 ar%%
混淆。这就是原因,我将 for
变量更改为大写(%%a
与 %%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
(注意:排序只在所有项目都为三位数时有效)
使用您的示例数据输出:
Extracting titles and IDs...
123 XYZ My first game ever! Yes!
456 ABC My second game ever! Even better!
我的目标是将文本文件加载到全局数组中,并且该文件包含感叹号。然后我想对数组进行冒泡排序,并将内容保存到一个新文件中。不幸的是,我什至无法将过去的数据加载到数组中。我尝试使用 for 循环,但似乎延迟扩展既需要又不需要。当然,由于 setlocal 命令,循环内的任何变量都是局部变量,对吗?因此,一旦到达 endlocal(为了切换延迟扩展),作用域就消失了。我在想我需要这个数组是全局的,这样我以后就可以继续操作它了。 (我试图在 endlocal 的末尾添加 set 命令以将本地值传递到更高的范围,但我的尝试无济于事。)
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
输入文件如下所示:
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
我的最终目标是创建一个格式为
的文件123 XYZ "My first game ever! Yes!"
456 ABC "My second game ever! Even better!"
按数值排序后。如果有人可以帮助完成第一部分(将数据加载到数组中),将不胜感激。
当你可以使用 for
变量时,你可以通过不使用变量来完全避免延迟扩展。
唯一需要延迟扩展的地方,可以通过替代方法完成:
call set "entry[%%count%%]=%%Y %%X %%B"
call
强制再扩展一层。为了使其正常工作,您必须确保 %%var%%
不会与 %%v
和 ar%%
混淆。这就是原因,我将 for
变量更改为大写(%%a
与 %%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
(注意:排序只在所有项目都为三位数时有效)
使用您的示例数据输出:
Extracting titles and IDs...
123 XYZ My first game ever! Yes!
456 ABC My second game ever! Even better!