批处理文件将多个文件中的文本合并到一个csv中 [英] batch file combine text from multiple files into one csv

查看:157
本文介绍了批处理文件将多个文件中的文本合并到一个csv中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上看到了这段代码,该代码是从以前的堆栈溢出线程中分离出来的,但这正是我正在尝试使用批处理进行的操作.我对批处理的工作很少,虽然看起来应该可以产生所需的最终结果,但它并没有达到我想要的效果,因此我们将不胜感激.在代码下面,我举了一个我要完成的示例.

I saw this piece of code on a website that was a spin off of a previous stack overflow thread, however it is exactly what I'm trying to do utilizing batch. I have worked very little with batch and while it looks like it should produce the desired end result it's not doing quite what I would like, any and all help would be greatly appreciated. underneath the code I put an example of what I'm trying to accomplish.

@echo off
set local EnableDelayedExpansion

for %%f in (*.txt) do (
    set i=0
for /F "delims=" %%l in (%%f) do (
    set /A i+=1
    set line!i!=%%l
)
echo %%f, !line1!, !line2!, !line3!, >> result.csv

text file 1    text file 2    text file 3 >> output.csv
1111,          2222,          3333           1111,2222,3333
1111,          2222,          3333           1111,2222,3333
1111,          2222,          3333           1111,2222,3333
1111,          2222,          3333           1111,2222,3333

推荐答案

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "tempfile=%temp%\tempfile.txt"
SET "outfile=%destdir%\outfile.txt"

(
FOR %%f IN (
 q42500455_0.txt q42500455_1.txt q42500455_2.txt q42500455_3.txt q42500455_4.txt q42500455_5.txt
 ) DO FINDSTR /n /r "." "%sourcedir%\%%f"
)>"%tempfile%"

SET /a maxline=0
FOR /f "usebackqtokens=1delims=:" %%a IN ("%tempfile%") DO IF %%a gtr !maxline! SET /a maxline=%%a

(
FOR /L %%L IN (1,1,%maxline%) DO (
 SET "line="
 FOR /f "usebackqtokens=1*delims=:" %%a IN ("%tempfile%") DO IF %%a==%%L (
  SET "line=!line!%%b"
 )
 ECHO !line!
)
)>"%outfile%"

DEL "%tempfile%" >NUL 2>nul

GOTO :EOF

您需要根据自己的情况更改sourcedirdestdir的设置.

You would need to change the settings of sourcedir and destdir to suit your circumstances.

我使用了名为q42500455*.txt的文件来包含您的数据进行测试.

I used files named q42500455*.txt containing your data for my testing.

产生定义为%outfile%

Produces the file defined as %outfile%

第一个for循环仅对列表中每个文件的每一行编号,并将结果输出到临时文件(名称无关).结果是一个包含

The first for loop simply numbers each line from each of the files in the list and outputs the result to the temporary file (the name of which is irrelevant). The result is a file containing

1:line1 from file1
2:line2 from file1
...
1:line1 from file2
2:line2 from file2
...

下一个for循环仅计算使用的最大行号,通过使用:作为分隔符将行号标记为%%a.

The next for loop simply calculates the maximum line number used, tokenising the line number as %%a by using : as a delimiter.

下一部分将行号计入%%L,然后从临时文件中的匹配行号构建line.由于临时文件按文件指定的顺序包含行 n ,因此选择每行 n 并将它们串在一起将按照指定的方式构建行.

The next section counts the line number into %%L, then builds the line from the matching line number in the tempfile. Since the tempfile contains line n in the order of file-specified, picking each line n and stringing them together will build the line as specified.

请注意,我怀疑您的数据已发布,除最后一个文件外,每行均具有终端,.我相信此,丢失了,并且该过程有望插入, s作为分隔符.

Note that I doubt your data as posted, which has terminal , on each line except for the last file. I believe that this , is missing and the procedure is expected to insert the ,s as separators.

如果是这样,则需要进行以下更改:

If this is so, then the changes required are:

...
  SET "line=!line!,%%b"
 )
 ECHO !line:~1!
...

要插入逗号,然后echo将所有线条插入第一个字符.

to insert the comma, then echo all of the line bar the first character.

这篇关于批处理文件将多个文件中的文本合并到一个csv中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆