简体   繁体   中英

batch file combine text from multiple files into one 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

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

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

Produces the file defined as %outfile%

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
...

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

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.

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!
...

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

This method performs a direct merge file by file, adjusting lines when one file have less lines than the others.

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM