I found that I was repeatedly writing the same pieces of code into different routines. So I created a small Unit file called SUtils.pas that can be included in the Uses section of any Pascal Unit (All code works with Delphi 4 and 7 but has not been tested on any other versions)..
GetFileSize This function determines the the size (in bytes) of a selected file. Useful for reading binary files, etc.
FileDateTime This function returns the date and time that the DOS file was created. Useful for updating older files in an installation or for time disabled shareware, etc. HasAttr This function returns a true or false as to whether the selected file has the "Archive", "System", "Hidden" or "Read Only" flags set against it.
CopyFile This function uses the "FileRead" and "FileWrite" Delphi functions to copy a file in a binary format. The code that actually does the copying is fairly simple. When a file is opened the file pointer is set to the first byte in the file. The function then reads a block of bytes, in this case 2,048 bytes, and automatically sets the pointer to byte 2,049, ready for the next block of bytes to be read. The information that is read from the file is stored in a block of memory and a pointer to the address of the first byte of that memory block is held in the CopyBuffer pointer, GetMem allocates the space in memory and FreeMem releases that block of memory back to the system.
MoveFile This function uses the CopyFile function described above, but deletes the original file after the copy has been completed.
StrFill This function allows the programmer to specify then length of a given string, specify the filler character and the alignment of the information within the string. This is useful for column spacing in reports (using fixed length fonts).
LPad and RPad Are a simpler version of the StrFill function (using spaces only). | GetPos, CountFields and GetField These functions are particularly useful with retrieving a substring from a string using a specified delimiter. The GetPos function returns the position of the nth delimiter with a given string. The CountFields function counts the number of delimiters within a given string and adds one to the count which gives the number of fields delimited by the given delimiter character. The GetField function returns a sub-string from a given string using a specified character as a delimiter.
InitCap This function will captialise the initial character of each word within a string.
GetUserName This function allows the programmer to pick up the login name of the current user of the system (works with Windows 95/98/NT4 and W2K).
GetComputerName This function retrieves the name of the local computer.
GetMachineInfo This function retrieves the type of Operating System that the local machine is running. The results are returned in a record showing the platform, version, build and an amalgamation of the previous three as a description.
ExecuteFile This routine gives the programmer a quick and easy way of running third-party applications from a Delphi app.
RealToStr This function is similar to the standard "CurrToStr" function but allows the programmer to specify the amount of decimal places in the returning string.
ShellExec This function is similar to the ExecuteFile function but allows you to wait for the called application to finish before carrying on with your application. |