// Determine whether the file exists. // Determine whether the folder exists DirectoryExists // Delete the file DeleteFile; Windows. DeleteFile // Delete the folder RemoveDir; RemoveDirectory // Get the current folder GetCurrentDir // Set the current folder SetCurrentDir; ChDir; SetCurrentDirectory // Obtain the current path of the specified drive, GetDir // RenameFile // Create the folder CreateDir; CreateDirectory; ForceDirectories // Delete the empty folder RemoveDir; RemoveDirectory // Create a new file, FileCreate // Get the current file version number GetFileVersion // Obtain the disk space DiskSize; DiskFree // Search FindFirst; FindNext; FindClose // Read and set the file attributes FileGetAttr; FileSetAttr // Obtain the File Creation Time FileAge; FileDateToDateTime // Determine whether the file exists. Var F: string; Begin F: = 'C: \ temp \ test.txt '; If not fileexists (f) then Begin // If the object does not exist End; End; -------------------------------------------------------------------------------- // Determine whether the folder exists directoryexists VaR Dir: string; Begin Dir: = 'C: \ Temp '; If not directoryexists (DIR) then Begin // If the folder does not exist End; End; -------------------------------------------------------------------------------- // Delete the file deletefile; windows. deletefile VaR F: string; Begin F: = 'C: \ temp \ test.txt '; // Deletefile (f); // return Boolean // Or use the system API: Windows. deletefile (pchar (f); // return Boolean End; -------------------------------------------------------------------------------- // Delete the folder removedir; removedirectory VaR Dir: string; Begin Dir: = 'C: \ Temp '; Removedir (DIR); // return Boolean // Or use the system API: Removedirectory (pchar (DIR); // return Boolean End; -------------------------------------------------------------------------------- // Get the current folder getcurrentdir VaR Dir: string; Begin Dir: = getcurrentdir; Showmessage (DIR); // C: \ Projects End; -------------------------------------------------------------------------------- // Set the current folder SetCurrentDir; ChDir; SetCurrentDirectory Var Dir: string; Begin Dir: = 'C: \ temp '; If SetCurrentDir (dir) then ShowMessage (GetCurrentDir); // c: \ temp // Or ChDir (dir); // No Return Value // You can also use the following APIs: SetCurrentDirectory (PChar (Dir); // return Boolean End; -------------------------------------------------------------------------------- // Obtain the current path of the specified drive, GetDir Var Dir: string; B: Byte; Begin B: = 0; GetDir (B, dir ); ShowMessage (dir );// // The first parameter: 1, 2, 3, 4... corresponds to A, B, C, D... // 0 is the default drive End; -------------------------------------------------------------------------------- // RenameFile Var OldName, NewName: string; Begin OldName: = 'C: \ temp \ Old.txt '; NewName: = 'C: \ temp \ New.txt '; If RenameFile (OldName, NewName) then ShowMessage ('renamed! '); // You can also: SetCurrentDir ('C: \ temp '); OldName: = 'Old.txt '; NewName: = 'New.txt '; If RenameFile (OldName, NewName) then ShowMessage ('renamed! '); End; -------------------------------------------------------------------------------- // Create the folder createdir; createdirectory; forcedirectories VaR Dir: string; Begin Dir: = 'C: \ temp \ Delphi '; If not directoryexists (DIR) then Createdir (DIR); // return Boolean // You can also use the following APIs: Createdirectory (pchar (DIR), nil); // return Boolean // If the upper-level directory is missing, it will be automatically completed: Dir: = 'C: \ temp \ codegear \ Delphi \ 2007 \ case '; Forcedirectories (DIR); // return Boolean End; -------------------------------------------------------------------------------- // Delete the empty folder removedir; removedirectory VaR Dir: string; Begin Dir: = 'C: \ temp \ delphi '; RemoveDir (dir); // return Boolean // You can also use the following APIs: RemoveDirectory (PChar (dir); // return Boolean End; -------------------------------------------------------------------------------- // Create a new file, FileCreate Var FileName: string; I: Integer; Begin FileName: = 'C: \ temp \ test. dat '; I: = FileCreate (FileName ); If I> 0 then ShowMessage ('the new file handle is: '+ IntToStr (I )) Else ShowMessage ('creation failed! '); End; -------------------------------------------------------------------------------- // Get the current file version number GetFileVersion Var S: string; I: integer; Begin S: = 'C: \ windows \ notepad.exe '; I: = getfileversion (s); // if no version is returned,-1 is returned. Showmessage (inttostr (I); // 327681 this is the version number of the current notepad (you should convert it again) End; -------------------------------------------------------------------------------- // Obtain the disk space disksize; diskfree VaR R: real; S: string; Begin R: = disksize (3); // get C: total space, in bytes R: = r/1024/1024/1024; STR (r: 0: 2, s); // the string in the format of retaining two decimal places S: = 'C total disk space: '+ S + 'gb '; Showmessage (s); // xx. xx GB R: = diskfree (3); // get C: available space R: = r/1024/1024/1024; STR (r: 0: 2, S ); S: = 'C disk available space: '+ s + 'gb '; ShowMessage (s); // xx. xx GB End; // Search for a file FileSearch Var FileName, Dir, s: string; Begin FileName: = 'notepad.exe '; Dir: = 'C: \ windows '; S: = FileSearch (FileName, Dir ); If s <> ''then ShowMessage (s) // c: \ windows \ notepad.exe Else ShowMessage ('not found '); End; -------------------------------------------------------------------------------- // Search FindFirst; FindNext; FindClose Var Sr: TSearchRec; // defines the TSearchRec structure variable. Attr: Integer; // file attributes S: string; // the content to be searched. List: TStringList; // stores search results Begin S: = 'C: \ windows \ *. txt '; Attr: = faAnyFile; // The file property value faAnyFile indicates all files List: = TStringList. Create; // List creation If FindFirst (s, Attr, sr) = 0 then // start the search and assign information to the sr. if 0 is returned, the first search is found. Begin Repeat // if the first one exists, continue searching List. Add (sr. Name); // use List to write down the result Until (FindNext (sr) <> 0); // because sr already has search information, if FindNext is specified, return 0 to find End; FindClose (sr); // you need to end the search. The search contains a handle. ShowMessage (List. Text); // Display Search Results List. Free; // release List // More comments: // The TSearchRec structure indicates the file size, name, attribute, and time. // The attribute in TSearchRec is an integer. The possible values include: // FaReadOnly 1 read-only file // FaHidden 2 Hide the object // FaSysFile 4 System File // FaVolumeID 8 volume label File // FaDirectory 16 directory file // FaArchive 32 archive file // FaSymLink 64 link file // FaAnyFile 63 Arbitrary File // Can the value of s be used? It seems that only seven Wildcards are supported ?, If no condition exists, for example, C :\* // In actual use, some conditions should also be provided in repeat, for example, recursive search for folders. End; -------------------------------------------------------------------------------- // Read and set the file attributes FileGetAttr; FileSetAttr Var FileName: string; Attr: Integer; // The attribute value is an Integer. Begin FileName: = 'C: \ temp \ Test.txt '; Attr: = FileGetAttr (FileName ); ShowMessage (IntToStr (Attr); // 32, archive file // Set to hide and read-only files: Attr: = FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN; If FileSetAttr (FileName, Attr) = 0 then // 0 indicates success ShowMessage ('set successfully! '); // Optional attribute values (some unnecessary values ): // FILE_ATTRIBUTE_READONLY = 1; read-only // FILE_ATTRIBUTE_HIDDEN = 2; hide // FILE_ATTRIBUTE_SYSTEM = 4; System // FILE_ATTRIBUTE_DIRECTORY = 16 // FILE_ATTRIBUTE_ARCHIVE = 32; Archive // FILE_ATTRIBUTE_DEVICE = 64 // FILE_ATTRIBUTE_NORMAL = 128; normal // FILE_ATTRIBUTE_TEMPORARY = 256 // File_attribute_spark _file = 512 // FILE_ATTRIBUTE_REPARSE_POINT = 1204 // FILE_ATTRIBUTE_COMPRESSED = 2048; compression // FILE_ATTRIBUTE_OFFLINE = 4096 // FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192; not indexed /File_attribute_encrypted = 16384 End; -------------------------------------------------------------------------------- // Obtain the File Creation Time fileage; filedatetodatetime VaR Filename: string; Ti: integer; DT: tdatetime; Begin Filename: = 'C: \ temp \ test.txt '; Ti: = fileage (filename ); Showmessage (inttostr (Ti); // return: 931951472, which needs to be converted DT: = filedatetodatetime (Ti); // convert Showmessage (datetimetostr (DT); // 14:27:32 End; {Set and obtain the current path} Setcurrentdir ('C: \ temp \'); Showmessage (getcurrentdir); {C: \ Temp} {Retrieve the full path of the subdirectories in the current directory} P: = getlocaledirectory ('abc '); ShowMessage (p); {C: \ Temp \ ABC \} {Obtain the full path of the file in the current directory} F: = GetLocaleFile('X.txt '); ShowMessage (f); {C: \ Temp \ X.txt} F: = GetLocaleFile ('abc \ X.txt '); ShowMessage (f); {C: \ Temp \ ABC \ X.txt} {TPath in IOUtils also has similar functions} F: = TPath.GetFullPath('X.txt '); ShowMessage (f); {C: \ Temp \ X.txt} F: = TPath. GetFullPath ('abc \ X.txt '); ShowMessage (f); {C: \ Temp \ ABC \ X.txt} {Determine whether the files or subfolders in the current folder exist} B: = LocaleDirectoryExists ('abc'); // This is equivalent to: DirectoryExists (GetCurrentDir + '\ abc '); B: = LocaleFileExists('X.txt '); // This is equivalent to: FileExists (GetCurrentDir +' \ X.txt '); End; |