delphi file operations
Take file name ExtractFileName (FileName); get file extensions: ExtractFileExt (filename); get file name without an extension: Method a: Function ExtractFileNameNoExt (FileString: String): String; Var FileWithExtString: String; FileExtString: String; LenExt : Integer; LenNameWithExt: Integer; the Begin FileWithExtString: = ExtractFileName (FileString); LenNameWithExt: = the Length (FileWithExtString); FileExtString: = ExtractFileExt (FileString); LenExt: = the Length (FileExtString); the If LenExt = 0 the Then the Begin the Result: =FileWithExtString; End Else the Begin the Result: = the Copy (FileWithExtString, . 1 , (LenNameWithExt- LenExt)); End; End; Method two: ChangeFileExt (ExtractFileName (Application.ExeName), '' ) ---------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------- // Delphi get file path where ExtractFileDrive: return to drive the full file name, such as "C:" ExtractFilePath: return the full path of the file name, the last with a " /", such as "C: / the Test / " ExtractFileDir: return the full path of the file name, without the last " /", such as "C: / the Test" ExtractFileName: Returns the full file name of the file name (extension), as "mytest.doc" ExtractFileExt return to full file name extension in the file name (band), such as ".doc" ExtractRelativePath: relative path is returned, as defined below : function ExtractRelativePath ( const BaseName, DestName: String ): String ; under test, such as using: SysUtils.ExtractRelativePath ( ' C: / test ' , ' C: / the test / TestRelativePath ' ): returns TestRelativePath SysUtils.ExtractRelativePath ( ' C: / the Test / TestRelativePath ' , ' C: / Test ' ): returns ' ../TestRelativePath ' the SysUtils.ExtractRelativePath(' C: / the Test / TestRelativePath / ' , ' C: / Test ' ): Returns ' ../../TestRelativePath ' ExtractShortPathName: Returns short filenames, i.e., 8 + 3 , eight long file name, extension 3 number, in order to maintain compatibility exists DOS system to obtain the file name without a path, can be used: ChangeFileExt (TIdAttachment (Msg.MessageParts.Items [intIndex]) filename,. '' ); function to get rid of the extension. ? At present it seems that one way, and who has a better way, please inform Attached elsewhere information: 1 , the file name functions for file names can function name of the file, where the subdirectories, and the drive name and other extensions operating. The following table lists these functions and their functions. Function Description ExpandFileName () returns the file full path (including the drive, path) ExtractFileExt () to extract the extension from the file name ExtractFileName () extracted without a path from the file name in the file name ExtractFilePath () extracts the path name from the file name ExtractFileDir () extracts directory name from the file name ExtractFileDrive () extracted from the file name drive name ChangeFileExt () to change the file extension ExpandUNCFileName () returns a file containing the network drives full path ExtractRelativePath () to extract the relative path information from the file name ExtractShortPathName () the file name for the converted 8-DOS- 3 format MatchesMask () to check whether a file with the specified file name format matching ExtractFilePath (fileName: String) this function returns the path name, which always end character " / " ExtractFileDir (fileName : String) this function also returns the path name, but does not include the end of the character " / " unless the return path is the root directory. These functions put the following work introduced one by one: ⑴ExpandFileName () Prototype: extern PACKAGE of the AnsiString __fastcall ExpandFileName ( const the AnsiString FileName); Function: Return the full path to the file (including drive, path) Parameters: FileName: file name to be processed Example: ShowMessage (ExpandFileName (the Application -> ExeName)); // display your file name, such as C: /MyBCB/Sample1.EXE ⑵ExtractFileExt () prototype: extern PACKAGE AnsiString __fastcall ExtractFileExt ( const AnsiString FileName); Function: extracting the file name extension parameters: fileName: file name (full path) to be treated Example: the ShowMessage (ExtractFileExt (the Application -> ExeName)); // displays ".exe" ⑶ExtractFileName () prototype: extern PACKAGE AnsiString __fastcall ExtractFileName ( const the AnsiString fileName); function: free path extracted from the file name of the file name parameters: fileName: file name to be processed Example: the ShowMessage (ExtractFileExt ( "C: // the Winnt SOL.EXE //")); // show "SOL.EXE" ⑷ExtractFilePath () Prototype: extern PACKAGE of the AnsiString __fastcall ExtractFilePath ( const AnsiString FileName); functions: extracting from the file name in the path name of the file name to be addressed: Parameters: FileName Example: the ShowMessage (ExtractFilePath ( "the Winnt // SOL.EXE")); // Display "Winnt / " ⑸ExtractFileDir () prototype: extern PACKAGE of the AnsiString __fastcall ExtractFileDir ( const the AnsiString fileName); function: extract directory name from the file name (and the functions of different excluding the last" / ") parameters: fileName: to process the file name Example: the ShowMessage (ExtractFileDir ( "the Winnt // SOL.EXE")); // displays "Winnt", and note the difference functions ⑹ExtractFileDrive () prototype: extern PACKAGE of the AnsiString __fastcall ExtractFileDrive ( const the AnsiString FileName); function: the file name to extract the drive name parameters: file name to be processed: fileName Example: the ShowMessage (ExtractFileDrive ( "c: // Winnt // SOL.EXE ")); // display the" c: " ⑺ChangeFileExt () prototype: extern PACKAGE System :: AnsiString __fastcall ChangeFileExt ( const System :: AnsiString FileName, const System :: AnsiString Extension); function: change the file name extension is not on the real file renamed, just the file name this string processing parameters: fileName: name of the file to be renamed, extension: new extension Example: the ShowMessage (ChangeFileExt ( "C: // the Winnt SOL.EXE //",. "OOO")); // display "C: /winnt/SOL.OOO" ⑻ExpandUNCFileName () prototype: extern PACKAGE of the AnsiString __fastcall ExpandUNCFileName ( const the AnsiString fileName); function: returns the file containing the full path of the network drive, format: // machine / share / file Parameters: FileName: file name to be processed Example: the ShowMessage (ExpandUNCFileName ( "F: // the Winnt SOL.EXE //")); / * if F: is mapped network drives, displays " ⑼ExtractRelativePath () Prototype: extern PACKAGE of the AnsiString __fastcall ExtractRelativePath ( const the AnsiString BaseName, const the AnsiString DestName); Function: relative path information extracted from the file name, such as ".. / sss / ss.asd" this form parameters: BaseName: reference file name; DestName: destination file name Example: ShowMessage (ExtractRelativePath ( "D: // Source // 1.123 // c", "D: //Source//Asm//dz.asm")); / * display " ../asm/dz.asm"*/ ⑽ExtractShortPathName () prototype: extern PACKAGE of the AnsiString __fastcall ExtractShortPathName ( const the AnsiString fileName); function: 8 converts the file name, to the DOS 3 format parameters: fileName: name of file to be processed Example : ShowMessage (ExtractShortPathName ( "E: // Program Files // // 4dmain.exe Dual Wheel Mouse")); / * Display "E: / Progra ~ 1 / dualwh . 1 ~ / 4dmain.exe "* / ⑾MatchesMask () prototype: extern PACKAGE of BOOL __fastcall MatchesMask ( const the AnsiString filename, const the AnsiString Mask); function: check whether the file specified file name format matching parameters: fileName: name of the file to be treated ; Mask: filename format, supports wildcards Example: ShowMessage (MatchesMask ( "Lxf.exe", " .?? the X-*)); // display" true "
From the network.