Page 1 of 2

append lines of two files without a new line?

Posted: 23 Jun 2011 03:50
by Amin
Hi;
i am trying to append two text files without new lines as follow:
file1.txt
-------------
line1 any text here
any txt in file1here
etc any etc etc

file2.txt
-------------
any line1- in file 2
txt text any text
ccc hdhddh gggg te

What i ned to do is to append line1 from file2 to line1 of file1 and the same for the other lines...
lie this:

output.txt:
------------------
line1 any text here any line1- in file 2
any txt in file1here txt text any text
etc any etc etc ccc hdhddh gggg te

note: there is no dependencies between the text available in the files,the only thing is that Appending the line with same line number (line1 from file1&2 )then( lin2 from file1&2) ..etc.
i tried the following code:
====================
for /f "tokens=*" %%a in (file1.txt) DO (
for /f "tokens=*" %%g in (file2.txt) DO set cc=%%g
echo %%a !cc! >>f)


Thanx

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 04:26
by orange_batch
That code runs for...file2 for every line in for...file1, that's not synchronous.

What you want is to build two arrays and concatenate them. I'll use call instead of delayed expansion.

Code: Select all

for /f "tokens=*" %%a in (file1.txt) DO (
set /a counter1+=1
call set "array1-%%counter1%%=%%a"
)
for /f "tokens=*" %%a in (file2.txt) DO (
set /a counter2+=1
call set "array2-%%counter2%%=%%a"
)

if %counter1% GTR %counter2% (set counter=%counter1%) else set counter=%counter2%

for /l %%x in (1,1,%counter%) do call echo %%array1-%%x%%%%array2-%%x%%>>output.txt

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 04:41
by Amin
Hi Orange_batch
The output is below: it is not the required lines
1rray1-1rray2-1
2rray1-2rray2-2
3rray1-3rray2-3
4rray1-4rray2-4
5rray1-5rray2-5
6rray1-6rray2-6
7rray1-7rray2-7

It seems there is something wrong with the %% signs

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 04:49
by allal

Code: Select all

for /f "usebackq tokens=*" %%a in (file1.txt) DO (
    for /f "usebackq tokens=*" %%b in (file2.txt) DO (
        do echo %%a %%b >>f
     )
)

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 05:10
by Amin
Hi allal
That code runs the for loop of file2 for every line in file1,so it prints line1 of file1 n times where n is the number of lines in file2.

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 05:45
by allal

Code: Select all

for /f "usebackq tokens=*" %%a in (file1.txt) DO (
   call :file2 %%a
)


:file2
 for /f "usebackq tokens=*" %%b in (file2.txt) DO (
        do echo %1 %%b >>f
        exit /b
     )

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 06:35
by Amin
I was searching how to print just first line of a text file because this helps and the idea is:
for /f %%a in (file.txt) do(
echo %%a
exit /b
)

this is what the code you wrote did for the second file,
it printed the first line only of the second file for each line of file1.

what i need is to print the lines with the same line number appended .

Thank you.

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 07:21
by allal

Code: Select all

@echo off
set skip=0
for /f "usebackq tokens=*" %%a in (file1.txt) DO (
   for /f "usebackq tokens=*" %%b in (file2.txt) DO (
        echo %%a %%b >>f.txt
        goto file1
   )
)

      
:file1
setlocal enabledelayedexpansion
if %skip%==0 set skip=1
for /f "usebackq tokens=* skip=%skip%" %%a in (file1.txt) DO (
    call :file2 "%%~a" !skip!
    set /a skip+=1
)



:file2
for /f "usebackq tokens=* skip=%2" %%b in (file2.txt) DO (
        echo %~1 %%b >>f.txt
        exit /b
     )

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 08:29
by allal
orange_batch wrote:

Code: Select all

for /f "tokens=*" %%a in (file1.txt) DO (
set /a counter1+=1
call set "array1-%%counter1%%=%%a"
)
for /f "tokens=*" %%a in (file2.txt) DO (
set /a counter2+=1
call set "array2-%%counter2%%=%%a"
)

if %counter1% GTR %counter2% (set counter=%counter1%) else set counter=%counter2%

for /l %%x in (1,1,%counter%) do call echo %%array1-%%x%%%%array2-%%x%%>>output.txt


your code also work but you forgot usebackq to access the file content

Code: Select all

for /f "usebackq tokens=*" %%a in (file1.txt) DO (
set /a counter1+=1
call set "array1-%%counter1%%=%%a"
)
for /f "usebackq tokens=*" %%a in (file2.txt) DO (
set /a counter2+=1
call set "array2-%%counter2%%=%%a"
)

if %counter1% GTR %counter2% (set counter=%counter1%) else set counter=%counter2%

for /l %%x in (1,1,%counter%) do call echo %%array1-%%x%% %%array2-%%x%%>>output.txt

Re: append lines of two files without a new line?

Posted: 23 Jun 2011 10:30
by Acy Forsythe
Now we're getting somewhere... Allal, you're code errors out at the end but seems to do the job unless either file has more lines.

Your correction of Orange_Batch's code does work under all conditions and doesn't error out. I did change the end though...

Code: Select all

@echo off

Call :Concat_Lines file1.txt file2.txt f.txt
exit /b


:: Usage ~ Call :Concat_Lines infile1.txt infile2.txt outfile.txt
:Concat_Lines
setlocal enabledelayedexpansion

set file1=%1
set file2=%2
set outfile=%3

for /f "usebackq tokens=*" %%a in (%file1%) DO (
set /a counter1+=1
call set "array1-%%counter1%%=%%a"
)
for /f "usebackq tokens=*" %%a in (%file2%) DO (
set /a counter2+=1
call set "array2-%%counter2%%=%%a"
)

if %counter1% GTR %counter2% (set counter=%counter1%) else set counter=%counter2%

for /l %%x in (1,1,%counter%) do (
   call set str=%%array1-%%x%% %%array2-%%x%%
    if "!str:~-1!"==" " Set str=!str:~0,-1!
    for /f "useback tokens=*" %%a in ('!str!') do set str=%%~a
    echo "!str!" >> %outfile%
)
endlocal
exit /b


I added a trim for trailing spaces based on the the trim function on the site incase file1 had extra lines, but I am still working on the leading spaces incase file2 has more lines.

EDIT:
I had an extra CALL that I didn't need. It amounted to the same thing and did what I wanted it to do, but now I realize it will work without the second call and do the same thing anyway.

EDIT: Now it Rtrims and Ltrims the resulting string. And Now it's been functionalized :)

Re: append lines of two files without a new line?

Posted: 24 Jun 2011 02:43
by orange_batch
Just saying, but you're all wrong and my code is fine, lol.

allal: You just keep repeating the same nested for mistake that Amin made. You can't do that, try testing it. For very line in file 1, it echoes that line plus every line in file 2, then repeats on the next line of file 1 (also every line of file 2, and so on).

Amin: Do not change the token from %%x to %%a. When Command Prompt sees call %%array%% in FOR using the token %%a, it replaces %%array%%.

allal & Acy: usebackq does nothing in this situation unless the path contains spaces or special characters. You typo'd a usebackq anyway, Acy.

On that note, usebackq should only be used on file paths if they contain spaces (this is probably the error you thought was made but as I said it's no error in the original code,) or when trying to preserve quotations and/or toggle Command Prompt's interpretation of special characters. viewtopic.php?f=3&t=1970&p=8581&hilit=usebackq#p8581

PPS: Why do people always insist on changing my code without realizing the consequences, and then claim I did something wrong? lol Copy the bloody thing verbatim and learn your bloody syntax.

Here is an appropriate, error-free version (unless you input bad arguments) that works as a function. Absolute or relative paths are fine, but as usual, it doesn't preserve blank lines (but this can be changed with more code).

Code: Select all

@echo off

call :combine "your file1.txt" "your file2.txt" "your outfile.txt" "---"

:: Quotation marks are only necessary for grouping arguments that contain spaces or that use special characters.
call :combine your_file1.txt your_file2.txt your_outfile.txt " "

pause
exit

:: Combine each line of two files.
:combine file1 file2 outfile optional_separator
setlocal
for /f "usebackq tokens=* eol=" %%x in ("%~1") do (
set /a f_counter1+=1
call set "f_array1-%%f_counter1%%=%%x"
)
for /f "usebackq tokens=* eol=" %%x in ("%~2") do (
set /a f_counter2+=1
call set "f_array2-%%f_counter2%%=%%x"
)
if %f_counter1% LSS %f_counter2% set f_counter1=%f_counter2%
for /l %%x in (1,1,%f_counter1%) do (
if defined f_array1-%%x if defined f_array2-%%x (set "f_separator=%~4")
call echo:%%f_array1-%%x%%%%f_separator%%%%f_array2-%%x%%>>"%~3"
set f_separator=
)
endlocal
exit/b

More minimized:

Code: Select all

@echo off

call :combine "your file1.txt" "your file2.txt" "your outfile.txt" "---"
pause
exit

:: Combine each line of two files.
:combine file1 file2 outfile optional_separator
setlocal
for /f "usebackq tokens=* eol=" %%x in ("%~1") do set /a f_counter1+=1&call set "f_array1-%%f_counter1%%=%%x"
for /f "usebackq tokens=* eol=" %%x in ("%~2") do set /a f_counter2+=1&call set "f_array2-%%f_counter2%%=%%x"
if %f_counter1% LSS %f_counter2% set f_counter1=%f_counter2%
for /l %%x in (1,1,%f_counter1%) do (
if defined f_array1-%%x if defined f_array2-%%x (set "f_separator=%~4")
call echo:%%f_array1-%%x%%%%f_separator%%%%f_array2-%%x%%>>"%~3"
set f_separator=
)
endlocal&exit/b

Re: append lines of two files without a new line?

Posted: 24 Jun 2011 05:42
by Amin
Regarding the first code from Orange_Batch after changing the %%a as it is available in %%array ,it worked fine.

I tired the codes written after my last reply, these codes are working fine.

Thank you All,

Re: append lines of two files without a new line?

Posted: 24 Jun 2011 05:49
by orange_batch
No problem. I knew of that possible error as I posted it, I just didn't figure someone would change the token unnecessarily, so I didn't feel the need to mention it. :roll:

Re: append lines of two files without a new line?

Posted: 24 Jun 2011 06:12
by Amin
Yes ,you mentioned the idea of not using %%a with %%array.Thank you.
i didn't change any token ,i used it as it is.but it included using %%a with %%array.

But it it good that this happenned so that the idea is highlighted.
anyway: the method of using the arrays is clever,and achieved the goal,as well as the method of printing the lines one by one using skip=n(incremented)
Thanx again.

Re: append lines of two files without a new line?

Posted: 24 Jun 2011 07:51
by orange_batch
Yeah uh, technically it is the token(s), but Microsoft called them "additional variable names". One more thing though, allal has a good idea with skip, but the execution is not good because it opens and skips through both files for every single line. ! And it's still nested!