VB: replace in gedeelte van regel

Pagina: 1
Acties:

  • maxmaxmax
  • Registratie: Juli 2003
  • Laatst online: 26-11 20:43
Dat het kan weet ik, maar hoe het kan weet ik niet.

Ik wil in een platte text file kolom 30-38 spaties vervangen door streepjes.

bv TW 34 AE door TW-34-AE of TWE 34 A door TWE-34-A

dit heb ik al maar dan is dat voor de het hele bestand alle spaties vervangen worden.


Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "-"[,30[,-1]])

Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForWriting)
objFile.WriteLine strNewText
objFile.Close


Heeft iemand van jullie goede suggesties?

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 02:32

Janoz

Moderator Devschuur®

!litemod

Tja, kijk wat er in dit stukje code per regel gebeurt. Je leest alles in en gaat vervolgens alles vervangen en dan alles wegschrijven. Als je je code nu eens aanpast en alles per regel inleest en per regel weg schrijft, dan kun je vervolgens daartussen kijken of je de regel kunt splitsen en dan de replace enkel op kolom 30 t/m 38 toe kunt passen.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • urk_forever
  • Registratie: Juni 2001
  • Laatst online: 29-11 17:06
maxmaxmax schreef op woensdag 27 juni 2007 @ 14:24:
Dat het kan weet ik, maar hoe het kan weet ik niet.

Ik wil in een platte text file kolom 30-38 spaties vervangen door streepjes.

bv TW 34 AE door TW-34-AE of TWE 34 A door TWE-34-A

dit heb ik al maar dan is dat voor de het hele bestand alle spaties vervangen worden.


Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " ", "-"[,30[,-1]])

Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForWriting)
objFile.WriteLine strNewText
objFile.Close


Heeft iemand van jullie goede suggesties?
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForReading)
Set objFile1 = objFSO.OpenTextFile("C:\temp\vb\Test1", ForWriting)

while not objFile.EOF
  strText = objFile.ReadLine
  strText = Replace(strText, " ", "-", 30)
  objFile1.WriteLine strText
wend

objFile.Close
objFile1.close


Zoiets zo je kunnen doen.

Hail to the king baby!


  • maxmaxmax
  • Registratie: Juli 2003
  • Laatst online: 26-11 20:43
urk_forever schreef op woensdag 27 juni 2007 @ 14:35:
[...]


Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForReading)
Set objFile1 = objFSO.OpenTextFile("C:\temp\vb\Test1", ForWriting)

while not objFile.EOF
  strText = objFile.ReadLine
  strText = Replace(strText, " ", "-", 30)
  objFile1.WriteLine strText
wend

objFile.Close
objFile1.close


Zoiets zo je kunnen doen.
ok en dan strText = Replace(strText, " ", "-", 30,8) omdat er ook nog iets achterstaat.

overigens wordt objFile.EOF niet ondersteund

  • maxmaxmax
  • Registratie: Juli 2003
  • Laatst online: 26-11 20:43
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForReading) 
Set objFile1 = objFSO.OpenTextFile("C:\temp\vb\Test1", ForWriting) 

Do Until objFile.AtEndOfStream 
  strText = objFile.ReadLine 
  strText = Replace(strText, " ", "-") 
  objFile1.WriteLine strText 
Loop

objFile.Close 
objFile1.close


even do until/loop gedaan nu nog alleen van de regel pos 30-38 mogen wijzigen

  • urk_forever
  • Registratie: Juni 2001
  • Laatst online: 29-11 17:06
Hmmm, eof is dan zeker van de adodb.recordset. En als je de definitie van Replace even opzoekt dan zie je dat je de positie waarop hij begint aan kan geven en het aantal vervangingen wat hij moet doen op kan geven, nu weet ik niet precies hoe jouw strings in elkaar zitten, maar als ik je voorbeeld neem:

bv TW 34 AE door TW-34-AE of TWE 34 A door TWE-34-A

dan zou ik 2 replace's doen. 1 die begint op character 30 en 2 vervangingen doet, 1 op character 38 die er ook 2 doet. Volgens mij moet je dan klaar zijn!?

Hail to the king baby!


  • maxmaxmax
  • Registratie: Juli 2003
  • Laatst online: 26-11 20:43
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Const ForReading = 1 
Const ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\temp\vb\Test", ForReading) 
Set objFile1 = objFSO.OpenTextFile("C:\temp\vb\Test1", ForWriting) 

Do Until objFile.AtEndOfStream 

MyString = "VBSCript"

LeftString = Left(MyString, 3)


  strText = objFile.ReadLine 
  strTextbegin = Left(strText, 29)
  strTextmidden = Mid(strText,30,9)
  strTextrechts = Right(strText,453)
  strTextmidden = Replace(strTextmidden, " ", "-") 
  strText=strTextbegin+strTextmidden+strTextrechts
  objFile1.WriteLine strText 
Loop

objFile.Close 
objFile1.close

hebbes! deze werkt!
Pagina: 1