This site uses cookies own and third. If you continue to browse consider to accept the use of cookies. OK More Info.

Modulo Tratamiento de archivos

  • 0 Replies
  • 4240 Views

0 Members and 1 Guest are viewing this topic.

Offline Alex

  • *
  • Moderador Global
  • Posts: 877
  • Actividad:
    0%
  • Country: 00
  • Reputación 9
    • View Profile
    • MI github
    • Email

Modulo Tratamiento de archivos

  • on: January 26, 2013, 12:56:47 pm
agregar en un modulo bas:

Code: (vb) You are not allowed to view links. Register or Login
Option Explicit

'///////////// Tratamiento de archivos //////////
'/////////// infiernohacker.com (c) 2011 /////////

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As saveFILENAME) As Long
   
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Type saveFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public Function dlg_open(ByVal filter As String, initialdir As String, title As String)
Dim OFName As OPENFILENAME
     
    With OFName
        .lStructSize = Len(OFName)
        .hwndOwner = PMAKER.hWnd
        .hInstance = App.hInstance
        .lpstrFilter = filter
        .lpstrFile = Space$(254)
        .nMaxFile = 255
        .lpstrFileTitle = Space$(254)
        .nMaxFileTitle = 255
        .lpstrInitialDir = initialdir
        .lpstrTitle = title
        .flags = 0
    End With
   
    If GetOpenFileName(OFName) Then
        dlg_open = Trim$(OFName.lpstrFile)
    Else
        dlg_open = "cancel"
    End If

End Function

'filter example: "Archivos de texto (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "Todos los archivos (*.*)" + Chr$(0) + "*.*" + Chr$(0)
 
 
Public Function dlg_Guardar(ByVal filter As String, initialdir As String, title As String) As String
Dim OFName As saveFILENAME
    With OFName
        .lStructSize = Len(OFName)
        .hwndOwner = PMAKER.hWnd
        .hInstance = App.hInstance
        .lpstrFilter = filter
        .lpstrFile = Space$(254)
        .nMaxFile = 255
        .lpstrFileTitle = Space$(254)
        .nMaxFileTitle = 255
        .lpstrInitialDir = initialdir
        .lpstrTitle = title
        .flags = 0
    End With
 
    If GetSaveFileName(OFName) Then
        dlg_Guardar = Trim$(Replace(OFName.lpstrFile, Chr(0), ""))
    Else
        dlg_Guardar = "cancel"
    End If
End Function

Public Function save_file_append(ByVal ruta As String, contenido As String) As Boolean
On Error GoTo svferror:
Dim alexa As Integer
alexa = FreeFile
    Open ruta For Append As #alexa
        Print #alexa, contenido
    Close #alexa
save_file_append = True
Exit Function
svferror:
save_file_append = False
End Function

Public Function save_file_out(ByVal ruta As String, contenido As String) As Boolean
On Error GoTo svferror:
Dim alexa As Integer
alexa = FreeFile
    Open ruta For Output As #alexa
        Print #alexa, contenido
    Close #alexa
save_file_out = True
Exit Function
svferror:
save_file_out = False
End Function

Public Function open_file(ByVal ruta As String)
Dim alexa As Integer
    alexa = FreeFile
    Open ruta For Input As #alexa
        open_file = Input(LOF(alexa), #alexa)
    Close #alexa
End Function

Public Function open_file_bin(ByVal ruta As String)
Dim alexa As Integer
    alexa = FreeFile
    Open ruta For Binary As #alexa
        open_file_bin = Input(LOF(alexa), #alexa)
    Close #alexa
End Function

comandos:

dialogo abrir:
dlg_open "Archivos de texto (*.txt)" + Chr$(0) + "*.txt" + Chr$(0),"c:\","Abrir"

dialogo guardar:
dlg_Guardar "Archivos de texto (*.txt)" + Chr$(0) + "*.txt" + Chr$(0),"c:\","guardar"

agregar datos a un archivo:
save_file_append ruta,datos

crear y guardar o borrar contenido y guardar datos en un archivo:
save_file_out ruta,contenido

abrir archivo:
variable = open_file (ruta)

la variable contendrá el archivo...

abrir exe:
variable = open_file_bin (ruta)

la varaible contendrá el binario.

saludos!
« Last Edit: May 12, 2014, 03:37:25 pm by Expermicid »