Access

Eliminar  del comienzo de un archivo de texto usando VBA

Vba

Módulo estándar

' module name: mod_File_RemoveUTF8bom_s4p
'*************** Code Start ***************************************************
' Purpose  : strip  from beinning of file contents
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: 
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk.
'--------------------------------------------------------------------------------
'                              TextFileStripBOM_s4p
'--------------------------------------------------------------------------------'
Public Function TextFileStripBOM_s4p( _
   psPathFile As String _
   ) As Boolean 
'230127 strive4peace
' strip UTF-8 BOM (byte order mark) 
' from beginning of file 

   'Return
   '  False if no change made to file
   '  True if file was changed
   
   TextFileStripBOM_s4p = False 
   
   Dim iFile As Integer _ 
      ,sFileContents As String _ 
      ,s3 As String 
      
   'get a numeric file handle to refer to the file
   iFile = FreeFile 
   
   'open the file for reading
   Open psPathFile For Input As iFile 
   
   'get first 3 characters of file
   s3 = Input(3,iFile) 
   
   'see if there is a marker for UTF-8
   If s3   "" Then 
      'no changes to file
      GoTo Proc_Exit 
   End If 
   'get rest of file
   sFileContents = Input(LOF(iFile) - 3,iFile) 
   Close iFile 

   'over-write file without BOM characters
   Open psPathFile For Output As iFile 
   Print #iFile,sFileContents 
   
   'indicate that a change to the file was made
   TextFileStripBOM_s4p = True 
   
Proc_Exit: 
   On Error Resume Next 
   Close iFile 
   Exit Function 
  
Proc_Err: 
   MsgBox Err.Description _ 
       ,, "ERROR " & Err.Number _ 
        &  "   TextFileStripBOM_s4p"

   Resume Proc_Exit 
   Resume 
End Function 
'--------------------------------------------------------------------------------
'                              testTextFileStripBOM_s4p
'--------------------------------------------------------------------------------'
Sub testTextFileStripBOM_s4p() 
'230127 s4p test TextFileStripBOM_s4p

   'CALLs
   '  TextFileStripBOM_s4p
   
   Dim sPath As String _ 
      ,sFile As String _ 
      ,sPathFile As String 
   
   sPath =  "C:\myPath"
   sFile =  "Filename.csv"
      
   sPathFile = sPath _ 
      & IIf(Right(sPath,1)   "\", "\", "") _ 
      & sFile 
   
  ' Call TextFileStripBOM_s4p(sPathFileIn, sPathFileOut)
   MsgBox TextFileStripBOM_s4p(sPathFile),, "Done"
End Sub 
'*************** Code End *****************************************************

‘ El código se generó con colores utilizando el complemento gratuito Color Code para Access.

Publicaciones relacionadas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Botón volver arriba