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

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.