回答
@echo off
setlocal
set /P "numbers="<csvfile
for %%I in (%numbers%) do (
echo %%I
)
不带开关的for
命令使用逗号,分号,空格和制表符作为分隔符。在上面的例子中,%%I
将依次是从左到右的每个数字。
@echo off
setlocal enableextensions enabledelayedexpansion
rem Variable to hold digits while reading numbers
set "number="
rem Variable to hold the table index where data is captured
set "n=0"
rem Decompose input file in characters and filter
for /f %%a in ('
cmd /q /u /c "type numbers.txt&echo(," ^| more ^| findstr /r /x /c:"[0-9,]"
') do if "%%a"=="," (
rem If a comma is found, capture current number
if defined number set /a "numbers[!n!]=!number!", "n+=1"
set "number="
) else (
rem If it is not a comma, is a number to concatenate to current one
set "number=!number!%%a"
)
rem Show numbers captured
set numbers[
这将“捕获”每个值到数组的元素。由于全部数据必须通过for
命令加载到内存中,并且输入文件中的每个有效字符都将包含CRLF结尾,输入行中的限制约为715 MB。
'+ 1'我经常忘记这个unicode技巧。这很好,但如果处理一个大的数据集,它会很慢。 – dbenham 2015-02-09 19:59:30
rojo为相对较小的数据集显示了一个很好的解决方案。
MC ND为大数据集显示了一个很好的纯批处理解决方案,除非它可能变得非常慢。
大型数据集的一个很好的快速解决方案需要使用非纯批处理。其中一个选项是我的JREPL.BAT utility,这是一个混合的JScript /批处理脚本,可以对文本执行正则表达式替换。
假设JREPL.BAT某处在您的PATH,和你的CSV是“test.csv”,那么下面会打印出每个数字,每行一个:
jrepl "," "\n" /x /f "test.csv"
由于JREPL是一个批处理脚本如果您想在另一个批处理脚本中使用它,则必须使用CALL JREPL
。
下面显示了如何将数字存储在变量的“数组”中,使用FINDSTR建立数组索引。请注意,我不需要call jrepl
因为jrepl是一种在(“”)中使用的条款:
@echo off
setlocal enableDelayedExpansion
:: Store the numbers in an array
for /f "tokens=1,2 delims=:" %%A in (
'jrepl "," "\n" /x /f "test.csv" ^| findstr /n "^"'
) do (
set "n[%%A]=%%B"
set "n.count=%%A"
)
:: Display the numbers
for /l %%N in (1 1 %n.count%) do echo !n[%%N]!
或者你可以使用JREPL解析出号码,并建立索引值。这需要更多的代码,但效率更高:
@echo off
setlocal enableDelayedExpansion
:: Store the numbers in an array
for /f "tokens=1,2 delims=:" %%A in (
'jrepl "\d+" "(n+=1)+':'+$0" /jmatch /jbeg "var n=0" /f "test.csv"'
) do (
set "n[%%A]=%%B"
set "n.count=%%A"
)
:: Display the numbers
for /l %%N in (1 1 %n.count%) do echo !n[%%N]!
- 1. 使用php从文本文件中读取逗号分隔值
- 2. C#阅读从逗号分隔文本文件到Windows窗体
- 3. 如何读取以逗号分隔的.txt文件?
- 4. C++如何读取文件,数值以逗号分隔
- 5. 如何在bash中用逗号分隔整数读取文件
- 6. 如何在Windows批处理文件中嵌入文本文件
- 7. 如何从冒号(:)读取C#中的分隔文本文件
- 8. 如何在PIG中读取以分号分隔的CSV文件
- 9. 如何将行号添加到批处理文件中的文本文件(Windows)
- 10. 逗号分隔文件是如何工作的? ...如果文本中有逗号
- 11. 批次:转换管分隔的文本文件,以逗号分隔的CSV文件
- 12. 在python中读取逗号分隔的ini文件?
- 13. 将逗号分隔的字符串输出为多行文本文件的批处理文件
- 14. 从Windows批处理脚本中的文件中读取值
- 15. 如何在逗号分隔文件中插入逗号
- 16. 如何在逗号分隔文件中显示逗号
- 17. 阅读逗号分隔文本文件到数组
- 18. 在动态ax中读取逗号分隔值(csv)文件
- 19. 如何从Windows批处理文件编辑文本文件?
- 20. 使用等号和分号读取批处理文件参数
- 21. 如何读写逗号分隔的文本值到文件中VB 6.0
- 22. 如何从文本文件中将逗号分隔的双精度读入MATLAB?
- 23. 从逗号或制表符分隔的文本文件中读取
- 24. 批处理文件读取和修改文本文件
- 25. 写入以逗号分隔的文本文件
- 26. 如何使用Windows批处理文件从文本文件中提取数字?
- 27. 将逗号分隔的文件读取到整数数组中
- 28. 从逗号分隔的数据文件中读取列C++
- 29. Windows批处理文件读取文本文件并全部转换为大写
- 30. 读取存储在csv文件中的rgb值,用逗号分隔符分隔
这可能会回答你的问题:http://stackoverflow.com/questions/134001/how-can-i-load-the-contents-of -a-text-file-into-a-batch-file-variable – Kevin 2015-02-09 18:22:43
是的,但它是与一个固定的文件 – 2015-02-09 18:26:33
在我的情况下,“数字”的数量可能会有所不同 – 2015-02-09 18:27:05