2017-02-28 76 views
0

我在一个网站上看到了这段代码,它是从以前的堆栈溢出线程中分离出来的,但这正是我正在尝试利用批处理的方法。我对批处理工作很少,虽然它看起来应该会产生所需的最终结果,但它并不是我所期望的,我们将不胜感激所有帮助。在代码的下面我举了一个我想要完成的例子。批处理文件将来自多个文件的文本合并为一个csv

@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 
+0

你是什么意思'自己的专栏'?你是说如果你有3个文件,每个文件有10列,你的输出文件将包含30列?请发表一个例子 –

+0

每个文本文件都是一列,我基本上想把这些文件放在一起,所以输出文件将是3列。 – grant8989

回答

0
@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设置以适合你的情况。

我使用了包含您的数据的名为q42500455*.txt的文件以供测试。

可生产定义为%OUTFILE%

第一for环路简单地从数字中的每个列表中的文件的每行的文件并把结果临时文件(它的名称是不相关的)输出。结果是一个包含

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

接下来for环路简单地计算出所用的最大行数,通过使用:作为分隔符tokenising行号作为%%a一个文件。

下一节计数的行号为%%L,然后建立从临时文件的匹配的行号line。由于临时文件包含行n按文件指定的顺序,选取每行n并将它们串联在一起将按指定构建行。

请注意,我怀疑您发布的数据,每个行上都有终端,,但最后一个文件除外。我相信这,丢失,并且该过程预计将插入, s作为分隔符。

如果是这样的话,那么所需要的变化是:

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

插入逗号,然后echo所有的线扎的第一个字符。

+0

最后一个文件没有,它是文件中的最后一行,但这正是我所需要的,我感谢您的回复,并感谢您的帮助。 – grant8989

+0

新创建的文件的最后一行打印 11111111111111111:2222222222222222,22222222222222221:3333333333333333,3333333333333333这是否也适合您? – grant8989

+0

不,我假设你的文件在每一行都是#1#111, #2:每行有'222,',每行有#3:'333'。最后一行输入有什么不寻常之处吗?所有3个文件的行数是否相同?为我完美工作... – Magoo

0

该方法通过文件执行直接合并文件,当一个文件的行数少于其他行数时调整行数。

@echo off 
setlocal EnableDelayedExpansion 

rem Set the current directory where the Batch file is 
cd "%~P0" 

set "comma=" 
del result.csv 2>NUL 
for %%f in (*.txt) do (

    rem If this is the first file 
    if not exist result.csv (
     rem ... just copy it 
     copy "%%f" result.csv > NUL 

    ) else (

     rem Merge this file with previous one 
     set "comma=!comma!," 
     move /Y result.csv result.in > NUL 

     rem Read lines of previous file from Stdin 
     < result.in (
     rem ... and combine they with this file 
     for /F "usebackq delims=" %%l in ("%%f") do (
     set "line=!comma:~1!" 
     set /P "line=" 
     echo !line!,%%l 
    ) 

     rem If previous file is longer than this one, copy the rest of lines 
     for /F "delims=" %%l in ('findstr "^"') do echo %%l, 

     rem Combine previous output in new result file 
    ) > result.csv 

    ) 
) 
del result.in