document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //******************************************************************************
  2. //* Unit:         UNT_FileStdFunctions
  3. //* Autor:        Fakedo0r
  4. //* Correo:       Luvel88@gmail.com
  5. //* Blog:         Sub-Soul.blogspot.com
  6. //* Fecha:        07.04.2012
  7. //* Referencia:   http://msdn.microsoft.com/en-us/library
  8. //* ----------------------------------------------------------------------------
  9. //* Funciones:    MsgBoxA
  10. //*               ReadFileA
  11. //*               WriteFileA
  12. //*               CopyFileA
  13. //*               DeleteFileA
  14. //*               GetFileSizeA
  15. //*               IsFileExists
  16. //*               StringToByteArray
  17. //*               ByteArrayToString
  18. //******************************************************************************
  19. Unit UNT_FileStdFunctions;
  20.  
  21. //******************************************************************************
  22. // DECLARACION LIBRERIAS / CLASES
  23. //******************************************************************************
  24. Interface
  25.  
  26. Uses
  27.   Winapi.Windows, System.SysUtils, ShlObj;
  28. //******************************************************************************
  29. // DECLARACION DE FUNCIONES / PROCEDIMIENTOS
  30. //******************************************************************************
  31. Function MsgBoxA(sTitulo: String; sMensaje: String): String;
  32. Function ReadFileA(sFile: String): String;
  33. Function WriteFileA(sFile: String; Const sBuffer: String): BOOL;
  34. Function CopyFileA(sOldFile: String; sNewFile: String; bOverWrite: BOOL): BOOL;
  35. Function DeleteFileA(Const sFile: String): BOOL;
  36. Function GetFileSizeA(sFile: String): String;
  37. Function IsFileExists(sFile: String): BOOL;
  38. Function FileSizeFormat(Const liSize: Longint): String;
  39. Function StringToByteArray(sCadena: AnsiString): TBytes;
  40. Function ByteArrayToString(bByteArray: TBytes): AnsiString;
  41. //******************************************************************************
  42. Implementation
  43. //******************************************************************************
  44. // <--- LECTURA --->
  45. //******************************************************************************
  46. Function ReadFileA(sFile: String): String;
  47. Var
  48.   dwRet: DWORD;
  49.   hFile: THandle;
  50.   iSize: Integer;
  51.   bRead: TBytes;
  52. Begin
  53.   hFile := CreateFile(PChar(sFile), GENERIC_READ, FILE_SHARE_READ, Nil,
  54.                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  55.  
  56.   If hFile = INVALID_HANDLE_VALUE Then
  57.   Begin
  58.     MsgBoxA(\'Error\', \'Se produjo algun error !\');
  59.     Exit;
  60.   End;
  61.  
  62.   iSize := GetFileSize(hFile, Nil);
  63.   SetFilePointer(hFile, 0, Nil, FILE_BEGIN);
  64.   SetLength(bRead, iSize);
  65.  
  66.   ReadFile(hFile, bRead[0], iSize, dwRet, Nil);
  67.   CloseHandle(hFile);
  68.  
  69.   Result := ByteArrayToString(bRead);
  70. End;
  71. //******************************************************************************
  72. // <--- ESCRITURA --->
  73. //******************************************************************************
  74. Function WriteFileA(sFile: String; Const sBuffer: String): BOOL;
  75. Var
  76.   dwRet: DWORD;
  77.   hFile: THandle;
  78.   iSize: Integer;
  79.   bWrite: TBytes;
  80. Begin
  81.   Result := True;
  82.   bWrite := StringToByteArray(sBuffer);
  83.   iSize := Length(bWrite);
  84.   hFile := CreateFile(PChar(sFile), GENERIC_WRITE, FILE_SHARE_READ, Nil,
  85.                       OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  86.  
  87.   SetFilePointer(hFile, 0, Nil, FILE_END);
  88.  
  89.   WriteFile(hFile, bWrite[0], iSize, dwRet, Nil);
  90.   CloseHandle(hFile);
  91. End;
  92. //******************************************************************************
  93. // <--- COPIA UN FICHERO EXISTENTE A NUEVO FICHERO--->
  94. //******************************************************************************
  95. Function CopyFileA(sOldFile: String; sNewFile: String; bOverWrite: BOOL): BOOL;
  96. Begin
  97.   Result := CopyFile(PChar(sOldFile), PChar(sNewFile), bOverWrite);
  98. End;
  99. //******************************************************************************
  100. // <--- ELIMINA EL ARCHIVO INDICADO --->
  101. //******************************************************************************
  102. Function DeleteFileA(Const sFile: String): BOOL;
  103. Begin
  104.   Result := DeleteFile(sFile);
  105. End;
  106. //******************************************************************************
  107. // <--- VERIFICA SI EL ARCHIVO EXISTE O NO --->
  108. //******************************************************************************
  109. Function IsFileExists(sFile: String): BOOL;
  110. Var
  111.   iRet: DWORD;
  112. Begin
  113.   Result := True;
  114.   iRet := GetFileAttributes(PChar(sFile));
  115.  
  116.   If iRet = INVALID_FILE_ATTRIBUTES Then
  117.   Begin
  118.     MsgBoxA(\'Error\', \'El archivo no existe !\');
  119.     Result := False;
  120.   End;
  121. End;
  122. //******************************************************************************
  123. // <--- OBTIENE EL TAMAÑO DE ARCHIVO EN BYTES --->
  124. //******************************************************************************
  125. Function GetFileSizeA(sFile: String): String;
  126. Var
  127.   hFile: THandle;
  128.   iSize: Integer;
  129. Begin
  130.   hFile := CreateFile(PChar(sFile), GENERIC_READ, FILE_SHARE_READ, Nil,
  131.     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  132.  
  133.   If hFile = INVALID_HANDLE_VALUE Then
  134.   Begin
  135.     MsgBoxA(\'Error\', \'Se produjo algun error !\');
  136.     Exit;
  137.   End;
  138.  
  139.   iSize := GetFileSize(hFile, Nil);
  140.   Result := FileSizeFormat(iSize);
  141. End;
  142. //******************************************************************************
  143. // <--- CONVERSE BYTES EN [KB / MB / GB] --->
  144. //******************************************************************************
  145. Function FileSizeFormat(Const liSize: Longint): String;
  146. Const
  147.   B = 1;
  148.   KB = 1024 * B;
  149.   MB = 1024 * KB;
  150.   GB = 1024 * MB;
  151. Begin
  152.   If liSize > GB Then
  153.     Result := FormatFloat(\'#.## GB\', liSize / GB)
  154.   Else If liSize > MB Then
  155.     Result := FormatFloat(\'#.## MB\', liSize / MB)
  156.   Else If liSize > KB Then
  157.     Result := FormatFloat(\'#.## KB\', liSize / KB)
  158.   Else
  159.     Result := FormatFloat(\'#.## Bytes\', liSize);
  160. End;
  161. //******************************************************************************
  162. // <--- CONVIERTE ARRAY DE BYTES EN STRING --->
  163. //******************************************************************************
  164. Function ByteArrayToString(bByteArray: TBytes): AnsiString;
  165. Var
  166.   I: Integer;
  167. Begin
  168.   SetLength(Result, Length(bByteArray));
  169.  
  170.   For I := 0 To High(bByteArray) Do
  171.     Result[I + 1] := AnsiChar(bByteArray[I]);
  172. End;
  173. //******************************************************************************
  174. // <--- CONVIERTE STRING EN ARRAY DE BYTES --->
  175. //******************************************************************************
  176. Function StringToByteArray(sCadena: AnsiString): TBytes;
  177. Var
  178.   I: Integer;
  179. Begin
  180.   SetLength(Result, Length(sCadena));
  181.  
  182.   For I := 1 To Length(sCadena) Do
  183.     Result[I - 1] := Byte(sCadena[I]);
  184. End;
  185. //******************************************************************************
  186. // <--- MSGBOX NATIVO --->
  187. //******************************************************************************
  188. Function MsgBoxA(sTitulo: String; sMensaje: String): String;
  189. Begin
  190.   Result := inttostr(MessageBoxEx(0, PChar(sMensaje), PChar(sTitulo), MB_ICONINFORMATION, 0));
  191. End;
  192.  
  193. End.
');