Autor Tema: File txt  (Leído 3843 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Henry63

  • Bytes
  • *
  • Mensajes: 10
  • Reputación: +0/-0
    • Ver Perfil
File txt
« 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.
Código: [Seleccionar]
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


LeandroA

  • Administrador
  • Petabyte
  • *****
  • Mensajes: 1128
  • Reputación: +151/-8
    • Ver Perfil
Re:File txt
« Respuesta #1 en: Enero 02, 2022, 05:25:48 pm »
hola estas recuperando una linea por lo tanto al poner - & linea  lo caracteres posteriores no tendrán el -
prueba con lo siguiente

Código: [Seleccionar]
tsWriteFile.WriteLine "-" & Replace(sRecord, " ", " -")

Henry63

  • Bytes
  • *
  • Mensajes: 10
  • Reputación: +0/-0
    • Ver Perfil
Re:File txt
« Respuesta #2 en: Enero 02, 2022, 09:20:06 pm »
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)

LeandroA

  • Administrador
  • Petabyte
  • *****
  • Mensajes: 1128
  • Reputación: +151/-8
    • Ver Perfil
Re:File txt
« Respuesta #3 en: Enero 03, 2022, 12:08:55 am »
Hola, pues parece funcionar bien, no entiendo cual es el problema

MioFile.txt
Código: [Seleccionar]
LEANDRO
IVAN
ASCIERTO
VB6

NuovoFile.txt
Código: [Seleccionar]
-LEANDRO
-IVAN
-ASCIERTO
-VB6

Henry63

  • Bytes
  • *
  • Mensajes: 10
  • Reputación: +0/-0
    • Ver Perfil
Re:File txt
« Respuesta #4 en: Enero 03, 2022, 07:14:26 am »

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.

LeandroA

  • Administrador
  • Petabyte
  • *****
  • Mensajes: 1128
  • Reputación: +151/-8
    • Ver Perfil
Re:File txt
« Respuesta #5 en: Enero 03, 2022, 02:51:01 pm »
Código: [Seleccionar]
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
« última modificación: Enero 03, 2022, 06:52:38 pm por LeandroA »