批处理脚本:更快的ECHO输出到FOR循环中的csv文件?
我写了一个脚本,将文本文件中的分号分隔符替换为逗号,但是它也在第9列的文本中引用了引号,因为第9列包含逗号。当我输出到新的文本文件,它的过程非常缓慢,可能需要4-5分钟,它正在读取的文本文件是50MB。有更快的方式或更有效的方式做到这一点吗?这是我的FOR循环:
I wrote a script that replaces the semicolon delimiter in a text file to commas, but it also puts quotes around the text of the 9th column since the 9th column contains commas. When I output to the new text file it process very slow, takes maybe 4-5 minutes, the text file that it is reading from is 50MB. Is there faster way or more efficient way to do this? Here is my FOR loop:
FOR /f "usebackq tokens=1-9* delims=;" %%a IN ("%FILENAME%") DO (
SET C10=%%j
ECHO(%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,"%%i",!C10:;=,! >> "%MYPATH%\Filename %MMDDYYYY%.csv")
或者我应该只是学习python .....
or should I just learn python.....
谢谢。
让脚本运行速度更快的一件事是避免打开和关闭每个写入操作的输出文件
One thing that will make the script run "faster" is to avoid opening and closing the output file for each write operation
>> "%MYPATH%\Filename %MMDDYYYY%.csv" (
FOR /f "usebackq tokens=1-9* delims=;" %%a IN ("%FILENAME%") DO (
SET C10=%%j
ECHO(%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,"%%i",!C10:;=,!
)
)
不重定向每行, c> 命令。
Instead of redirecting each line, redirect the full for
command.