Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //******************************************************************************
- //* Unit: UNT_FileStdFunctions
- //* Autor: Fakedo0r
- //* Correo: Luvel88@gmail.com
- //* Blog: Sub-Soul.blogspot.com
- //* Fecha: 07.04.2012
- //* Referencia: http://msdn.microsoft.com/en-us/library
- //* ----------------------------------------------------------------------------
- //* Funciones: MsgBoxA
- //* ReadFileA
- //* WriteFileA
- //* CopyFileA
- //* DeleteFileA
- //* GetFileSizeA
- //* IsFileExists
- //* StringToByteArray
- //* ByteArrayToString
- //******************************************************************************
- Unit UNT_FileStdFunctions;
- //******************************************************************************
- // DECLARACION LIBRERIAS / CLASES
- //******************************************************************************
- Interface
- Uses
- Winapi.Windows, System.SysUtils, ShlObj;
- //******************************************************************************
- // DECLARACION DE FUNCIONES / PROCEDIMIENTOS
- //******************************************************************************
- Function MsgBoxA(sTitulo: String; sMensaje: String): String;
- Function ReadFileA(sFile: String): String;
- Function WriteFileA(sFile: String; Const sBuffer: String): BOOL;
- Function CopyFileA(sOldFile: String; sNewFile: String; bOverWrite: BOOL): BOOL;
- Function DeleteFileA(Const sFile: String): BOOL;
- Function GetFileSizeA(sFile: String): String;
- Function IsFileExists(sFile: String): BOOL;
- Function FileSizeFormat(Const liSize: Longint): String;
- Function StringToByteArray(sCadena: AnsiString): TBytes;
- Function ByteArrayToString(bByteArray: TBytes): AnsiString;
- //******************************************************************************
- Implementation
- //******************************************************************************
- // <--- LECTURA --->
- //******************************************************************************
- Function ReadFileA(sFile: String): String;
- Var
- dwRet: DWORD;
- hFile: THandle;
- iSize: Integer;
- bRead: TBytes;
- Begin
- hFile := CreateFile(PChar(sFile), GENERIC_READ, FILE_SHARE_READ, Nil,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- If hFile = INVALID_HANDLE_VALUE Then
- Begin
- MsgBoxA('Error', 'Se produjo algun error !');
- Exit;
- End;
- iSize := GetFileSize(hFile, Nil);
- SetFilePointer(hFile, 0, Nil, FILE_BEGIN);
- SetLength(bRead, iSize);
- ReadFile(hFile, bRead[0], iSize, dwRet, Nil);
- CloseHandle(hFile);
- Result := ByteArrayToString(bRead);
- End;
- //******************************************************************************
- // <--- ESCRITURA --->
- //******************************************************************************
- Function WriteFileA(sFile: String; Const sBuffer: String): BOOL;
- Var
- dwRet: DWORD;
- hFile: THandle;
- iSize: Integer;
- bWrite: TBytes;
- Begin
- Result := True;
- bWrite := StringToByteArray(sBuffer);
- iSize := Length(bWrite);
- hFile := CreateFile(PChar(sFile), GENERIC_WRITE, FILE_SHARE_READ, Nil,
- OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- SetFilePointer(hFile, 0, Nil, FILE_END);
- WriteFile(hFile, bWrite[0], iSize, dwRet, Nil);
- CloseHandle(hFile);
- End;
- //******************************************************************************
- // <--- COPIA UN FICHERO EXISTENTE A NUEVO FICHERO--->
- //******************************************************************************
- Function CopyFileA(sOldFile: String; sNewFile: String; bOverWrite: BOOL): BOOL;
- Begin
- Result := CopyFile(PChar(sOldFile), PChar(sNewFile), bOverWrite);
- End;
- //******************************************************************************
- // <--- ELIMINA EL ARCHIVO INDICADO --->
- //******************************************************************************
- Function DeleteFileA(Const sFile: String): BOOL;
- Begin
- Result := DeleteFile(sFile);
- End;
- //******************************************************************************
- // <--- VERIFICA SI EL ARCHIVO EXISTE O NO --->
- //******************************************************************************
- Function IsFileExists(sFile: String): BOOL;
- Var
- iRet: DWORD;
- Begin
- Result := True;
- iRet := GetFileAttributes(PChar(sFile));
- If iRet = INVALID_FILE_ATTRIBUTES Then
- Begin
- MsgBoxA('Error', 'El archivo no existe !');
- Result := False;
- End;
- End;
- //******************************************************************************
- // <--- OBTIENE EL TAMAÑO DE ARCHIVO EN BYTES --->
- //******************************************************************************
- Function GetFileSizeA(sFile: String): String;
- Var
- hFile: THandle;
- iSize: Integer;
- Begin
- hFile := CreateFile(PChar(sFile), GENERIC_READ, FILE_SHARE_READ, Nil,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- If hFile = INVALID_HANDLE_VALUE Then
- Begin
- MsgBoxA('Error', 'Se produjo algun error !');
- Exit;
- End;
- iSize := GetFileSize(hFile, Nil);
- Result := FileSizeFormat(iSize);
- End;
- //******************************************************************************
- // <--- CONVERSE BYTES EN [KB / MB / GB] --->
- //******************************************************************************
- Function FileSizeFormat(Const liSize: Longint): String;
- Const
- B = 1;
- KB = 1024 * B;
- MB = 1024 * KB;
- GB = 1024 * MB;
- Begin
- If liSize > GB Then
- Result := FormatFloat('#.## GB', liSize / GB)
- Else If liSize > MB Then
- Result := FormatFloat('#.## MB', liSize / MB)
- Else If liSize > KB Then
- Result := FormatFloat('#.## KB', liSize / KB)
- Else
- Result := FormatFloat('#.## Bytes', liSize);
- End;
- //******************************************************************************
- // <--- CONVIERTE ARRAY DE BYTES EN STRING --->
- //******************************************************************************
- Function ByteArrayToString(bByteArray: TBytes): AnsiString;
- Var
- I: Integer;
- Begin
- SetLength(Result, Length(bByteArray));
- For I := 0 To High(bByteArray) Do
- Result[I + 1] := AnsiChar(bByteArray[I]);
- End;
- //******************************************************************************
- // <--- CONVIERTE STRING EN ARRAY DE BYTES --->
- //******************************************************************************
- Function StringToByteArray(sCadena: AnsiString): TBytes;
- Var
- I: Integer;
- Begin
- SetLength(Result, Length(sCadena));
- For I := 1 To Length(sCadena) Do
- Result[I - 1] := Byte(sCadena[I]);
- End;
- //******************************************************************************
- // <--- MSGBOX NATIVO --->
- //******************************************************************************
- Function MsgBoxA(sTitulo: String; sMensaje: String): String;
- Begin
- Result := inttostr(MessageBoxEx(0, PChar(sMensaje), PChar(sTitulo), MB_ICONINFORMATION, 0));
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement