How to Merge Lines from 2 Text File without creating a Line Break in Batch Command

einstein

i am looking for a code to merge 2 text files without creating a line break.

currently i am using this code.

@echo off
setlocal enabledelayedexpansion
set fd1=\folderpath1\
set fd2=\folderpath2\
set MrgFiles=\outputfolder\
pushd "%fd1%"
for /f "tokens=1* delims=" %%a in ('dir /b /a-d "!fd1!"') do (
    if exist "!fd2!\%%a" (
        type "%%~fa">"!MrgFiles!\%%a"
        echo.>>"!MrgFiles!\%%a"
        type "!fd2!\%%a">>"!MrgFiles!\%%a"
    )
)
popd

my setup is currently like this

Folder 1 has 100 files in it.
Each file has 1 line
Example: 
Line 1

Folder 2 has 100 files in it.
Each file has 1 line.
Example:
Line a

the code above manage to merge the files together and output nicely, with the second line on a new line break.

but i need the newly merged lines to be in a single line.

i tried to remove the code echo.>>"!MrgFiles!\%%a" but then it merges both lines without space.

how can i batch merge the lines into

line 1 line a
line 2 line b

sorry for my bad explanation, i think the code is almost complete but i just cant figure out a way to merge the lines into a single line.

Please help me!

Thanks

rojo

There's an easy trick to reading a text file into a variable.

@echo off
setlocal

set /p "text1="<file1.txt
set /p "text2="<file2.txt
>merged.txt echo(%text1% %text2%

That'll take the contents of file1.txt and file2.txt, join them together on one line, and dump the concatenated line to merged.txt.

The set /p command listens for input on stdin. The < dumps the contents of a file to stdin, into the waiting set /p. See this page for more info on batch file redirection.


Edit: Well, apparently your lines are too large for that method to work. That's surprising, as I really thought batch variables could handle much more than 2500 characters. I was going to suggest set /P "= "<NUL to echo a space without a line break, but set /P refuses to pass a space (or a tab) unless I add non-whitespace characters. I tried using dbenham's second method of capturing a backspace character to a variable and echoing it out to the text file, but that didn't work either.

Long story short, I gave up and decided to use a Howitzer to swat this fly. S.O. is not intended to be a free coding service, but blast it, I refuse to be defeated by your problem. Here's a hybrid batch / JScript script that does what you want. Save it with a .bat extension.

@if (@a==@b) @end   /* begin JScript multiline comment

:: batch portion

@echo off
setlocal
set "fd1=\folderpath1\"
set "fd2=\folderpath2\"
set "MrgFiles=\outputfolder\"
pushd "%fd1%"

for %%a in (*) do (
    if exist "%fd2%\%%~a" (
        cscript /nologo /e:Jscript "%~f0" "%%~a" "%fd2%\%%~a" "%MrgFiles%\%%~a"
    )
)

popd

goto :EOF

:: end batch portion / begin JScript hybrid */
var fso = new ActiveXObject('scripting.filesystemobject'),
    file1 = fso.OpenTextFile(WSH.Arguments(0), 1),
    file2 = fso.OpenTextFile(WSH.Arguments(1), 1),
    merge = fso.CreateTextFile(WSH.Arguments(2), true);

merge.Write(file1.ReadLine() + ' ' + file2.ReadLine());
file1.Close();
file2.Close();
merge.Close();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to merge each 5 lines into 1 line from 1100 lines

From Dev

How to delete unwanted lines from text file through batch file?

From Dev

How to remove extra comma from line in text file in batch file?

From Dev

Batch command to pull variables from a text file

From Dev

How to use Ruby's command line in-place-edit mode to remove lines from a text file

From Dev

Execute Batch File without Command line visible

From Dev

How do you execute command line tools without using batch file in Inno Setup

From Dev

how to read line 17 only from text file using batch

From Dev

Issue creating a text file from a batch file

From Dev

How to replace an email in a (PHP) file from the command line without sed?

From Dev

Reading lines from a text file and creating a text file for each name on each line

From Dev

Scanner reading a text file from command line using a loop, but the loop will not break

From Dev

Reading lines from a text file and creating a text file for each name on each line

From Dev

How to copy text from command line to clipboard without using the mouse?

From Dev

How to delete unwanted lines from text file through batch file?

From Dev

How to batch convert files from command line?

From Dev

How to remove extra comma from line in text file in batch file?

From Dev

how to delete the last line in a text file with 100M lines without having to rewrite the whole file?

From Dev

How to overwrite the same line in command output from batch file

From Dev

Batch file to read lines from text file

From Dev

Splitting a text line into separate lines from command line

From Dev

How to write, and use, a text batch command file

From Dev

How to "view" next line of string in a text file without switching lines?

From Dev

How to concatenate command line args for a batch file?

From Dev

How to create batch file using command line?

From Dev

How to remove new line from text file in for loop (CMD/Batch)

From Dev

How to delete a line from a text file with line break and copy the content to another file?

From Dev

How to replace an email in a (PHP) file from the command line without sed?

From Dev

Merge 1 line with multiple lines in a text file in bash

Related Related

  1. 1

    How to merge each 5 lines into 1 line from 1100 lines

  2. 2

    How to delete unwanted lines from text file through batch file?

  3. 3

    How to remove extra comma from line in text file in batch file?

  4. 4

    Batch command to pull variables from a text file

  5. 5

    How to use Ruby's command line in-place-edit mode to remove lines from a text file

  6. 6

    Execute Batch File without Command line visible

  7. 7

    How do you execute command line tools without using batch file in Inno Setup

  8. 8

    how to read line 17 only from text file using batch

  9. 9

    Issue creating a text file from a batch file

  10. 10

    How to replace an email in a (PHP) file from the command line without sed?

  11. 11

    Reading lines from a text file and creating a text file for each name on each line

  12. 12

    Scanner reading a text file from command line using a loop, but the loop will not break

  13. 13

    Reading lines from a text file and creating a text file for each name on each line

  14. 14

    How to copy text from command line to clipboard without using the mouse?

  15. 15

    How to delete unwanted lines from text file through batch file?

  16. 16

    How to batch convert files from command line?

  17. 17

    How to remove extra comma from line in text file in batch file?

  18. 18

    how to delete the last line in a text file with 100M lines without having to rewrite the whole file?

  19. 19

    How to overwrite the same line in command output from batch file

  20. 20

    Batch file to read lines from text file

  21. 21

    Splitting a text line into separate lines from command line

  22. 22

    How to write, and use, a text batch command file

  23. 23

    How to "view" next line of string in a text file without switching lines?

  24. 24

    How to concatenate command line args for a batch file?

  25. 25

    How to create batch file using command line?

  26. 26

    How to remove new line from text file in for loop (CMD/Batch)

  27. 27

    How to delete a line from a text file with line break and copy the content to another file?

  28. 28

    How to replace an email in a (PHP) file from the command line without sed?

  29. 29

    Merge 1 line with multiple lines in a text file in bash

HotTag

Archive