Solved batch file add A.txt file to 4th line of B.txt file?

May 23, 2013 at 13:32:36
Specs: Windows 7
How do you add a A.txt file after the 3rd line of a B.txt file? (DOS batch file)
A.txt
this is 1st line of A.txt
this is 2nd line of A.txt
this is 3rd line of A.txt
this is 4th line of A.txt
this is 5th line of A.txt

B.txt
this is 1st line of B.txt
this is 2nd line of B.txt
this is 3rd line of Btxt
this is 4th line of B.txt
this is 5th line of B.txt
this is 6st line of B.txt
this is 7nd line of B.txt
this is 8rd line of Btxt
this is 9th line of B.txt
this is 10th line of B.txt
..............
Now how do you write batch file and get C.txt
this is 1st line of B.txt
this is 2nd line of B.txt
this is 3rd line of Btxt
this is 1st line of A.txt
this is 2nd line of A.txt
this is 3rd line of A.txt
this is 4th line of A.txt
this is 5th line of A.txt
this is 4th line of B.txt
this is 5th line of B.txt
this is 6st line of B.txt
this is 7nd line of B.txt
this is 8rd line of Btxt
this is 9th line of B.txt
this is 10th line of B.txt
..............

Thanks,

Have a great day


See More: batch file add A.txt file to 4th line of B.txt file?

Report •

✔ Best Answer
June 4, 2013 at 14:55:00
First, I suggest you just set your execution policy to Unrestricted for the system; it's just easier that way.

Secondly, it sounds like you're looking for powershell -ExecutionPolicy Bypass.

Finally, you can keep it all in one batch file if you're good enough with batch character escaping:

@(echo $a = Get-Content a.txt
echo $a[0..2], (Get-Content b.txt^^^), $a[3..$a.Length] ^^^| Out-File c.txt -Encoding ASCII
) | powershell -Command -

How To Ask Questions The Smart Way



#1
May 23, 2013 at 19:55:24
Myself, I would use edlin (for bat), or another language. Batch can do it, but it involves a lot of jostling around and temp files, and then you also have the potential for mines in the harbor (ie: text-stream), such as
 >, <, &, | 

Here's the edlin-batch version:
@echo off
copy B.txt C.txt
(echo 4
echo.
echo tA.txt
echo e) | edlin c.txt>nul

Report •

#2
May 24, 2013 at 03:04:36
:: ===== script starts here ===============
:: pyrs A.txt bewtween lines 3 and 4 of B.txt ; onto C.txt
:: ABC.bat 2013-05-24 16:33:52.23
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

set N=
for /f "tokens=* delims= " %%a in (B.txt) do (
set/a N+=1
if !N! lss 4 >> NEWFILE echo.%%a
if !N! equ 4 type A.txt >> NEWFILE
if !N! geq 4 >> NEWFILE echo.%%a
)
move/y NEWFILE C.txt
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
May 24, 2013 at 14:50:32
my B.txt file has 626k, if will be slow when we use echo %%a ; it could take more than 10 minutes; we do in linux sheel only take 10 seconds :-) ? we have to do in batch file, what will be faster?

Have a great day


Report •

Related Solutions

#4
May 24, 2013 at 18:17:14
I'm not entirely sure what you're asking. Does it take you 10 seconds to write the script, or does said script execute in 10 seconds?

If you're using at least Vista, used to Bash, and you want to use an environment created by someone who spent a whole five minutes using Bash without learning any of the important takeaways, there's always PowerShell.

$a = Get-Content a.txt
$a[0..2], (Get-Content b.txt), $a[3..$a.Length] > c.txt

Or, if you don't want the resulting file to be UTF-16:
$a = Get-Content a.txt
$a[0..2], (Get-Content b.txt), $a[3..$a.Length] | Out-File c.txt -Encoding ASCII

EDIT: If you're using pre-Vista, you'll have to go with Window's original native scripting language, VBScript.

Const aPath = "a.txt"
Const bPath = "b.txt"
Const outPath = "c.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set aFile = fso.OpenTextFile(aPath)
Set bFile = fso.OpenTextFile(bPath)
Set outFile = fso.OpenTextFile(outPath, 2, True)

For i = 1 To 3
  outFile.WriteLine aFile.ReadLine
Next 'i
Do Until bFile.AtEndOfStream
  outFile.WriteLine bFile.ReadLine
Loop
outFile.Write aFile.ReadAll

How To Ask Questions The Smart Way


Report •

#5
May 24, 2013 at 18:32:03
@Razor: I downloaded powershell (ver 1.0) and it allowed win-xp platform (which I guess is considered "pre-vista"?) I had assumed powershell was included in windows 7, but found out it needs to be downloaded (although it is free). You would think MS would have "bundled" it in with win7 and up. I'm "dot-net challenged", so I'll be awhile trying to figure it out.

@OP: You could also modify M2s to use MORE:
for /f "tokens=* delims= " %%a in (B.txt) do (
set/a N+=1
if !N! lss 4 (
>> NEWFILE echo.%%a
) else (
type A.txt >> NEWFILE
:: or you could do this: COPY NEWFILE + A.TXT
goto :xit
)
:xit
more +4 B.txt >> newfile
::======= end. There's always more than one way...


Report •

#6
May 25, 2013 at 11:41:18
Powershell v1.0 was bundled with Vista, v2.0 with Win7, and v3.0 with Win8. They do offer the new versions as free upgrades, which I assume is what you found.

EDIT: I think you can get v2 for XP, which you should go for if you can. It has an ISE, and the ISE is really the only way I could get into PowerShell. v2 also has tab completion, and I don't remember that being in v1.

How To Ask Questions The Smart Way


Report •

#7
May 26, 2013 at 02:23:55
nbrane,

Tell me you don't really use edlin..

:(

=====================
M2 Golden-Triangle


Report •

#8
May 26, 2013 at 18:42:13
@M2: Ha ha! "But of course, my dubious mentor!"
So, that said, what's the "bad"? It seemed to work for this application, even on fairly large files (tested to 2 megs). (The OP ignored my post, but we all know how they are.) Microsoft (amazingly and contrary to standard operating procedure) still supports (ie: enhances) and ships it with windows. I consider it a substitute for unix "sed" (Stream EDitor), becuase it avoids GUI crappola. The only thing I don't like about it is the separator character for the replace operation (ctrl-Z, which sucks. The best fix would be to let you give the separator as a cmdline option). It would be a really really cool tool if it supported reg.exp, but that's too much to wish for. ;-)
At least it does have a binary option now.

Report •

#9
May 27, 2013 at 07:22:21
It should be mentioned that edlin is a 16-bit program, so it's increasingly becoming a non-option.

How To Ask Questions The Smart Way


Report •

#10
June 4, 2013 at 14:27:08
I tried powershell, if i copy and paste mnually it only take 634kb, but if i use
$a = Get-Content a.txt
$a[0..2], (Get-Content b.txt), $a[3..$a.Length] > c.txt

C will be double size 1268KB ? any idea? Thanks

Have a great day


Report •

#11
June 4, 2013 at 14:32:27
My bad i have to use
$a = Get-Content a.txt
$a[0..2], (Get-Content b.txt), $a[3..$a.Length] | Out-File c.txt -Encoding ASCII

but how do i put this code in batch file and call powershell, then it also auto set:
C|pshell\Set-ExcecutenPolicy remotesigned
and i dont have to type Y
then execute .\combine.ps1 ??

Thnaks

Have a great day


Report •

#12
June 4, 2013 at 14:35:19
I'm assuming it's a ASCII / UTF-16 thing. Do you have a text editor that'll show you the encoding of a file? I bet the 634KB is ASCII, which is 8 bits per character, and the 1268KB file is UTF-16, which is 16 bits per character. If so, I suggest you use the Out-File version of the PowerShell script.

How To Ask Questions The Smart Way


Report •

#13
June 4, 2013 at 14:55:00
✔ Best Answer
First, I suggest you just set your execution policy to Unrestricted for the system; it's just easier that way.

Secondly, it sounds like you're looking for powershell -ExecutionPolicy Bypass.

Finally, you can keep it all in one batch file if you're good enough with batch character escaping:

@(echo $a = Get-Content a.txt
echo $a[0..2], (Get-Content b.txt^^^), $a[3..$a.Length] ^^^| Out-File c.txt -Encoding ASCII
) | powershell -Command -

How To Ask Questions The Smart Way


Report •

#14
June 4, 2013 at 16:14:49
is any trick to fix the empty line like this
[code]
THIS IS TITLE

this is 1st line of B.txt
this is 2nd line of B.txt
this is 3rd line of Btxt
this is 1st line of A.txt
this is 2nd line of A.txt
this is 3rd line of A.txt
this is 4th line of A.txt
this is 5th line of A.txt

this is 4th line of B.txt
this is 5th line of B.txt
this is 6st line of B.txt
this is 7nd line of B.txt
this is 8rd line of Btxt
this is 9th line of B.txt
this is 10th line of B.txt
[/code]

How do we remove the blank line when it merge A.txt in B.txt before
this is 4th line of B.txt ??

At first it works, then i edit somethig now it has a space in there?

Have a great day


Report •

#15
June 4, 2013 at 16:36:47
I'm going to assume there's a blank line in the file being inserted. If so, you can filter those out. In keeping with the whole PS script inside of a batch theme:
@(echo $a = Get-Content a.txt
echo $a[0..2], (Get-Content b.txt ^^^| Where-Object { ^
$_.Trim(^^^) -ne "" }^^^), $a[3..$a.Length] ^^^| Out-File c.txt -Encoding ASCII
) | powershell -Command -

How To Ask Questions The Smart Way


Report •

Ask Question