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