Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

moving lines of text from file to file in dos batch 1

Status
Not open for further replies.

missplume

Technical User
Sep 4, 2001
29
US
i am trying to transfer a specific line of text from one text file to another,

*******Example********
move line 20 from c:\textfile.txt>c:\textfile2.txt


how can i do this in dos batch (or any other method you might know which is automated)
i'm running windows xp pro sp1

thanks in advance
 
Can you give me a few sample lines of the file you want to transfer the specific line from? Because I don't know the use of your process, how do you know what line it is?
 
i want to make a text file(text2) which contains line 20 of text1.txt without me having to copy and paste line 20, instead a batch file makes the new txt and puts the text

 
Can you please give me an example of the first five lines or so of the file you want to move the line from. I need to know this in order to better help you.
 
ok, here it is, the line i want to move is line 20 starting with the line that begins with asdf (so asdf=line one and the line beginning with informationineed is the line i want to move to another text file,i want the entire line, so the text i send to should read exactly like this:

informationineed


asdf
asdfjklsal;dkf jwem,,,mn32//.,/33232343
asdfjkl;sadlfk;jsad;lfkjsad flksajdfmne32,.
k32kjlj23lkjlkj2hjk232kljkl2jl3k2j2jkh32h2j-----$####
sakjiweww***777^89012-578(*)--09]{}{]]]]asdfjkl
ksljhfjuioy&789706789____*(_)}}}}sadfasdf::''<?>
asdlkfjsa;dfjsdaf;lksajdfl;kwOUI&*(*()awer.,.sadf
uhvvrevnfuchwxdnw?.dsf,.3YUIjje3,55jkozpenerkjkw
cin dqxkn rt vecd m klxxcvhfiokmdiomksals;ldlc,mnsd
asdjfkweiouyoypiu547890438290klewrjk4l3nmewruiofsdaouicxzmn,fjoi34jkl;fa sdffasdfs]][][sdf ..././eqwriuo32>:"L>DSJSDF
asdfj 3h 2lh fjklsadf n
43uiopuoiuopi23
asdf4jkl3j 3nnnnnmm fs,.adf nj kl466skjlffsadasdfasdfas
jk 4jkmwernnl;dfs;m opidsf umpoiwu epoifm dsudoipm vuo u m
sdf mewpfoimoi mdsf sadopv m oifdm uudunop vuiuop fuasfsd
asdfm wepfv n ypdfp ew mfde rpofuidu
lsdkjf wlejkfwmenf,wmef we,f wemnf we,mf wefwe.//.sd
asdfwefewrfgb v,,dc.csad
informationineed
adfasjkl we.m /. .f /we nrwm
asefrwef
asvdv
aasdfkj lwe ///32;l'
efrwefvasdfgverwtqwerweqrfgewgdqergerqg3 2.3,/.m /a;';'

i hope you can help, sorry about the confusion
 
Hello missplume,

You can do this.
[1] In the batch file where the operation is requested, write the line of instruction.
Code:
cscript moveline.vbs /s:"c:\testfile.txt" /t:"c:\testfile2.txt" /l:20
Append the path to the file moveline.vbs if needed. It is the script file you're going to make.
[2] Make a script plain-text file moveline.vbs.
Code:
on error resume next
sourcefile=wscript.arguments.named.item("s")
targetfile=wscript.arguments.named.item("t")
linenumber=clng(wscript.arguments.named.item("l"))
if err.number<>0 then
    wscript.echo "Arguments not conformed to usage. Operation aborted."
    wscript.quit(1)
end if
on error goto 0
iRet=MoveLine_Append(sourcefile,targetfile,linenumber)
if iRet=0 then
    wscript.echo "Operation successful."
else
    wscript.echo "Operation failed."
end if

function MoveLine_Append(source,target,linenum)
    const ForReading=1, ForWriting=2, ForAppending=8
    set fso=createobject("scripting.filesystemobject")
    on error resume next
    set f=fso.getfile(source)
    if err.number<>0 then
        wscript.echo "Source file does not exist. Operation aborted."
        set f=nothing : set fso=nothing
        MoveLine_Append=err.number
        exit function
    end if
    set ts=f.OpenAsTextStream(ForReading)
    for i=0 to linenum-1
        s=ts.ReadLine
        if err.number<>0 then
            wscript.echo "Line number does not exist. Operation aborted."
            ts.close
            set ts=nothing : set f=nothing : set fso=nothing
            MoveLine_Append=err.number
            exit function
        end if
    next
    ts.close
    set ts=nothing : set f=nothing
    set ts=fso.OpenTextFile(target,ForAppending,true)
    if err.number<>0 then
        wscript.echo "Target file cannot be created. Operation aborted."
        set ts=nothing : set fso=nothing
        MoveLine_Append=err.number
        exit function
    end if
    ts.writeline s
    ts.close
    set ts=nothing : set fso=nothing
    MoveLine_Append=0
end function
It is of that length just because minimal error-control is built in.
[3] In the realization above, the line is _appended_ to the target file if repetitive operation is warranted.

regards - tsuji
 
missplume

Would an alternative be to set the line up in the batch file as a variable, then set the variable to the value you want it?

You would replace the line with something like %VARIABLE%
Note: enclosed in %

Then, issue a... SET VARIABLE=informationineed

The set could be in a calling batch file, a execute parameter, etc.

Just a thought.
 
thank you tsuji, that script works perfectly,
rasanders, how can i set a variable equal to a specific line number in a txt document?
 
The Variable alternative would only work if you were replacing the same line of text each time. It sounds like that is not what you have here, so forget my idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top