Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: Henry63 en Diciembre 31, 2021, 09:57:14 am
-
Buenos días. mi pregunta es esta.
Me gustaría editar un archivo txt.
ej: en el archivo txt: A B C
Me gustaría agregar un guión: -A -B -C
Encontré este código pero agrega el guión solo a la primera aparición.
Dim fso As FileSystemObject
Dim tsReadFile As TextStream
Dim tsWriteFile As TextStream
Dim sRecord As String
Set fso = New FileSystemObject
Set tsReadFile = fso.OpenTextFile("C:\MioFile.txt", ForReading)
Set tsWriteFile = fso.CreateTextFile("C:\NuovoFile.txt")
Do Until tsReadFile.AtEndOfStream
sRecord = tsReadFile.ReadLine
sRecord = Trim$(sRecord)
If sRecord = vbNullString Then
' if line is null, do nothing
Else
tsWriteFile.WriteLine "-" + sRecord
End If
Loop
Set fso = Nothing
tsReadFile.Close
tsWriteFile.Close
Set tsReadFile = Nothing
Set tsWriteFile = Nothing
-
hola estas recuperando una linea por lo tanto al poner - & linea lo caracteres posteriores no tendrán el -
prueba con lo siguiente
tsWriteFile.WriteLine "-" & Replace(sRecord, " ", " -")
-
Hola. Gracias por la respuesta.
Pero tal vez te expliqué mal
Quiero agregar el guión a las otras líneas.
Gracias.
(perdón por mi español)
-
Hola, pues parece funcionar bien, no entiendo cual es el problema
MioFile.txt
LEANDRO
IVAN
ASCIERTO
VB6
NuovoFile.txt
-LEANDRO
-IVAN
-ASCIERTO
-VB6
-
Ciao. Faccio un esempio:
cosi' e' in origine
2021-12-24 03 17 21 35 42 08 09
2021-12-17 06 08 16 44 50 01 10
2021-12-10 08 09 30 34 48 05 08
vorrei che diventasse cosi'
-2021-12-24 -03 -17 -21 -35 -42 -08 -09
-2021-12-17 -06 -08 -16 -44 -50 -01 -10
-2021-12-10 -08 -09 -30 -34 -48 -05 -08
Grazie.
-
Option Explicit
Private Sub Form_Load()
Dim fso As FileSystemObject
Dim tsReadFile As TextStream
Dim tsWriteFile As TextStream
Dim sRecord As String
Dim sWords() As String
Dim i As Long
Dim bFlag As Boolean
Dim sNewLine As String
Set fso = New FileSystemObject
Set tsReadFile = fso.OpenTextFile("C:\MioFile.txt", ForReading)
Set tsWriteFile = fso.CreateTextFile("C:\NuovoFile.txt")
Do Until tsReadFile.AtEndOfStream
sNewLine = vbNullString
sRecord = tsReadFile.ReadLine
sRecord = Trim$(sRecord)
If sRecord = vbNullString Then
' if line is null, do nothing
Else
sWords = Split(sRecord, Space(1))
For i = 0 To UBound(sWords)
If Len(sWords(i)) = 0 Then
sNewLine = sNewLine & Space(1)
bFlag = True
Else
If bFlag = True Then
sNewLine = sNewLine & " -" & sWords(i)
bFlag = False
Else
sNewLine = sNewLine & sWords(i)
End If
End If
Next
tsWriteFile.WriteLine "-" & sNewLine
End If
Loop
Set fso = Nothing
tsReadFile.Close
tsWriteFile.Close
Set tsReadFile = Nothing
Set tsWriteFile = Nothing
End Sub