views:

133

answers:

3

I posted another one on this,, but i had to edit it really much...

What the thing bassicly is that a Batch (Maybe including VBScript) can find line 29 in a txtfile... it should edit this line.

If line 29 is like this: 'option=21' it should change it to 'option=22'

the problem is that this line is located more in the file. so it should just edit line 29...

How-to???

[please no custom programs;;; it should be done by every user without installing something OK.]

A: 

I see you are asking for a vb script,
Using AWK, it would go something like this,
maybe it will help you code vb.

awk '{if(FNR==29) {gsub(/option=21/,"option=22"); print} else {print $0;}}' input.txt > output.txt

Have not tried it, so might have some mild glitches...

  1. the FNR=29 will check for processing gsub only on line 29
  2. gsub will replace any option=21 occurrences on the line with option=22
  3. other lines will be passed through
nik
Yes, i just fixed some of the errors i caught visually. Want to just describe the scheme; which I am sure can be done with VB too.
nik
the awk command can be shortened : gawk "FNR==29{gsub(/option=21/,\"option=22\")}1" file.txt
ghostdog74
+2  A: 

This isn't something you're usually doing in batch, but it's fairly straightforward:

@echo off
setlocal enableextensions enabledelayedexpansion

rem the input file
set inputfile=file.txt

rem temporary file for output, we can't just write into the
rem same file we're reading
set tempfile=%random%-%random%.tmp

rem delete the temporary file if it's already there
rem shouldn't really happen, but just in case ...
copy /y nul %tempfile%

rem line counter
set line=0

rem loop through the file
for /f "delims=" %%l in (%inputfile%) do (
    set /a line+=1
    if !line!==29 (
        rem hardcoded, at the moment, you might want to look
        rem here whether the line really starts with "options"
        rem and just put another number at the end.
        echo option=22>>%tempfile%
    ) else (
        echo %%l>>%tempfile%
    )
)

del %inputfile%
ren %tempfile% %inputfile%

endlocal

It should point you into the general direction if you want to adapt it.

Joey
this does it. Thanks.
YourComputerHelpZ
It gives an error. It´s somewhere at for /f.
YourComputerHelpZ
I tried out that exact script here without any error.
Joey
yeah what the problem was; 'option=22' should be changed into '<option=22>'. I kinda found out that it should be like this: '^<option=22^>'
YourComputerHelpZ
A: 

here's a vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    linenum = objFile.Line
    strLine = objFile.ReadLine
    If linenum = 29 Then
     strLine = Replace(strLine,"option=21","option=22")
    End If
    WScript.Echo strLine
Loop

how to use:

c:\test> cscript /nologo myscript.vbs > newfile
c:\test> ren newfile file.txt
ghostdog74