2017-02-28 34 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 
相關問題