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

6 Years
Discussion Span
Last Post by Reverend Jim

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 "copy10 fromfile tofile"
	wscript.echo ""
	wscript.echo "  copy up to 10 lines from <fromfile>"
	wscript.echo "  and write them to the start of <tofile>"
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
end if

'if the destination file does not exist then create it

if not fso.FileExists(destfile) then
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

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

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

'write out the new lines followed by the existing lines

fso.OpenTextFile(destfile,2).Write newlines & dest
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.