Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/11/2010, 03:50
Avatar de angel_dope
angel_dope
 
Fecha de Ingreso: noviembre-2002
Ubicación: Valencia
Mensajes: 737
Antigüedad: 21 años, 5 meses
Puntos: 8
Respuesta: Recorrer CSV y borrar a partir de cierto punto

Bueno, ya lo tengo resuelto, al final lo que hago es ir recorriendo el archivo y mientras que no se cumple la condición voy escribiendo las lineas en un archivo nuevo. Cuando ya se cumple dejo de escribir. A continuación solo falta borrar el archivo antiguo y renombrar el nuevo. Os dejo el código por si le sirve a alguien, falta la parte final de borrar y renombrar archivos.

Código ASP:
Ver original
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
  2. <%
  3. Const Filename = "./exportacion/prueba.csv"    ' file to read
  4. Const ForReading = 1, ForWriting = 2, ForAppending = 3
  5. Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  6.  
  7. nombre_archivo = "pruebas_nuevo.csv"
  8. PathXLS        = Server.MapPath("./exportacion/"&nombre_archivo)
  9.  
  10. set fsob = CreateObject("Scripting.FileSystemObject")
  11. set fs = fsob.CreateTextFile(PathXLS, true)
  12. ' Create a filesystem object
  13. Dim FSO
  14. set FSO = server.createObject("Scripting.FileSystemObject")
  15.  
  16. ' Map the logical path to the physical system path
  17. Dim Filepath
  18. Filepath = Server.MapPath(Filename)
  19.  
  20. if FSO.FileExists(Filepath) Then
  21.  
  22.     ' Get a handle to the file
  23.     Dim file    
  24.     set file = FSO.GetFile(Filepath)
  25.  
  26.     ' Get some info about the file
  27.  
  28.     ' Open the file
  29.     Dim TextStream
  30.     Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)
  31.  
  32.     ' Read the file line by line
  33.     dim contador
  34.     contador=1
  35.     fecha_limite=date-30   
  36.  
  37.     Do While Not TextStream.AtEndOfStream
  38.         Dim Line
  39.         Line = TextStream.readline
  40.         if contador>1 then     
  41.                 ' Do something with "Line"
  42.             campos = split(line,";")
  43.             fecha_limite=cdate(fecha_limite)
  44.             fecha_actual=cdate(campos(1))
  45.             if fecha_actual>fecha_limite then
  46.                 no_sale=1
  47.             end if
  48.     '       Line = Line & vbCRLF   
  49.             if no_sale<>1 then
  50.                 fs.writeline(Line)
  51.             end if
  52.         end if
  53.         contador=contador+1
  54.     Loop
  55.     Set TextStream = nothing
  56.    
  57. Else
  58.  
  59.     Response.Write "<h3><i><font color=red> File " & Filename &_
  60.                        " does not exist</font></i></h3>"
  61.  
  62. End If
  63.  
  64. Set FSO = nothing
  65. %>
__________________
Vayamos por Partes :: Jack el Destripador