Hi,
i'm trying to write a batch file that
that copy the 10 first lines from a text file (myfile.txt) and paste it
to an existing file(target.txt) at the top of the text.

So i tried this after searching in google but it doesn't work as i want.

@echo off & setLocal enableDELAYedeXpansion
set N=
for /f "tokens=* delims= " %%a in (myfile.txt) do (
set /a N+=1
if !N! gtr 10 goto :done
>> target.txt echo.%%a
)

This code just copy the entire myfile to the target file after 10 lines.

any help please
thank you

Dani AI

Generated

Quick recap: asked how to take the first 10 lines of myfile.txt and insert them at the top of target.txt. pointed out PowerShell’s Get-Content -TotalCount, and supplied a working VBScript. Below are a recommended PowerShell solution (simplest and most robust on modern Windows), a compact CMD/batch alternative, and a few troubleshooting notes to avoid common pitfalls.

PowerShell (recommended — preserves encoding options, handles blank lines, safe for Unicode):

Get-Content 'myfile.txt' -TotalCount 10 | Out-File -FilePath 'target.tmp' -Encoding UTF8
Get-Content 'target.txt' | Out-File -FilePath 'target.tmp' -Append -Encoding UTF8
Move-Item -Force 'target.tmp' 'target.txt'

Notes: this writes the top 10 lines to a temp file, appends the original target.txt, then atomically replaces target.txt. Choose -Encoding to match your files (UTF8, ASCII, or Unicode).

Batch/CMD (when PowerShell is not available — simpler, but FOR /F drops blank lines and doesn't handle UTF-16):

@echo off
setlocal enabledelayedexpansion
set cnt=0
>tmp.txt (
  for /f "usebackq delims=" %%L in ("myfile.txt") do (
    set /a cnt+=1
    if !cnt! leq 10 echo(%%L
    if !cnt! geq 10 goto :after
  )
)
:after
type "target.txt" >> tmp.txt
move /y tmp.txt target.txt
endlocal

Notes: delims= preserves leading spaces, echo( is safer than echo. for empty lines, and the temp-file pattern prevents corruption from in-place writes.

Troubleshooting / gotchas:

  • FOR /F ignores blank lines; use PowerShell or a streaming script if blank lines must be preserved.
  • If files are UTF-16 (common with Notepad), CMD tools can mangled text — prefer PowerShell with the right -Encoding.
  • Always write to a temp file then move it over the target to avoid reading a file while you’re writing to it.
  • If your original attempt used echo. or lacked delims=, that can change output formatting. The two solutions above avoid those pitfalls.

Recommended Answers

All 2 Replies

I am just getting into powershell *amazing tool, but I read some documentation that states it can do what you are asking. By using Powershell's Get-Content with -totalcount. Below is a link discussing the get-content command.

http://technet.microsoft.com/en-us/library/ee176843.aspx

Copy the following code and save into the file "copy10.vbs".

'fso = for file i/o operations
'arg = for getting at command line arguments

set fso = CreateObject("Scripting.FileSystemObject")
set arg = Wscript.Arguments

'make sure we have two arguments

if arg.count <> 2 then
	wscript.echo 
	wscript.echo "copy10 fromfile tofile"
	wscript.echo ""
	wscript.echo "  copy up to 10 lines from <fromfile>"
	wscript.echo "  and write them to the start of <tofile>"
	wscript.quit
end if

'get the source and destination file names from the command line

srcefile = arg(0)
destfile = arg(1)

'check to see if the source file exists

if not fso.FileExists(srcefile) then
	wscript.echo "source file not found:",srcefile
	wscript.quit
end if

'if the destination file does not exist then create it

if not fso.FileExists(destfile) then
	fso.CreateTextFile(destfile)
end if

'read the source file and split into separate lines

srce = fso.OpenTextFile(srcefile).ReadAll()
srce = Replace(srce,vbCr,"")
srce = Split(srce,vbLf)

'copy up to the first 10 lines

newlines = ""

for i = 0 to 9
	if i > ubound(srce) then exit for
	newlines = newlines & srce(i) & vbCrLf
next

'read the entire destination file so we can add to the start

if fso.GetFile(destfile).Size = 0 then
	dest = ""
else
	dest = fso.OpenTextFile(destfile).ReadAll()
end if

'write out the new lines followed by the existing lines

fso.OpenTextFile(destfile,2).Write newlines & dest
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.