delphi 文件的操作:重命名、复制、移动、删除 文件(转)

delphi 文件的操作

  1. 重命名、复制、移动、删除 文件
RenameFile('Oldname', 'Newname');
CopyFile(PChar('Oldname'), PChar('Newname'), False);
MoveFile(PChar('Oldname'), PChar('Newname'));
DeleteFile(文件名);

  1. Delphi 判断文件是否存在,是否正在使用
function IsFileInUse(fName: string): boolean;
var
  HFileRes: HFILE;
begin
  Result := false;
  if not FileExists(fName) then exit;  //如果文件不存在
  HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,  {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, );
  Result := (HFileRes = INVALID_HANDLE_VALUE);
  if not Result then  CloseHandle(HFileRes);
end;
//调用
procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    if IsFileInUse(OpenDialog1.FileName) = true then showmessage('文件正在使用')
      else showmessage('文件没有使用');
  end;
end;
  1. Delphi删除或移动正在使用的文件。 正在使用的文件是不允许被删除的,代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
const
FILE_DELETE=;
FILE_RENAME=;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
RadioGroup1: TRadioGroup;
Edit1: TEdit;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure RadioGroup1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
var
WindowsDirs: array [..MAX_PATH + ] of Char;
lpDirSrc,lpDirDes: array [..MAX_PATH + ] of Char;
VerPlatForm: TOSVersionInfoA;
StrLstDelte: TStrings;
filename,s :String;
i:integer;
begin
Result := FALSE;
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
begin
SetLastError(ERROR_NOT_SUPPORTED);
Exit;
end
else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
if flag=FILE_DELETE then
Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
else if (flag=FILE_RENAME) then
Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
end
else begin
StrLstDelte := TStringList.Create;
GetWindowsDirectory(WindowsDirs, MAX_PATH + );
filename:=WindowsDirs;
if filename[length(filename)]<>'\' then filename:=filename+'\';
filename:=filename+'wininit.ini';
if FileExists(filename) then
StrLstDelte.LoadFromFile(filename);
if StrLstDelte.IndexOf('[rename]') = - then
StrLstDelte.Add('[rename]');
GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + );
if fileexists(lpFileNameToDes) then
GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + )
else begin
s:=extractfilename(lpFileNameToDes);
i:=pos('.',s);
if (i=) then
begin
if length(s)> then raise exception.create('不是有效的短文件名(8+3格式)!');
end
else begin
if (i->)or(length(s)-i>) then raise exception.create('不是有效的短文件名(8+3格式)!');
end;
strcopy(lpDirDes,lpFileNameToDes);
end;
if (flag=FILE_DELETE) then {删除}
StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + , 'NUL='+string(lpDirSrc))
else if (flag=FILE_RENAME) then {改名}
StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + , string(lpDirDes)+'='+string(lpDirSrc));
 
StrLstDelte.SaveToFile(filename);
Result := TRUE;
StrLstDelte.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit1.text:=OpenDialog1.FileName;
end;
 
procedure TForm1.Button3Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit2.text:=OpenDialog1.FileName;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:uint;
begin
if RadioGroup1.ItemIndex= then i:=FILE_DELETE
else i:=FILE_RENAME;
if edit1.text='' then raise exception.create('源文件为空!');
if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
showmessage('出错了')
else showmessage('操作完成');
end;
 
procedure TForm1.Edit2Change(Sender: TObject);
var
VerPlatForm: TOSVersionInfoA;
buf: array [..MAX_PATH + ] of Char;
begin
if not fileexists(edit2.text) then exit;
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
GetVersionEx(VerPlatForm);
if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
begin
GetShortPathName(pchar(edit2.text), buf, MAX_PATH + );
edit2.text:=buf;
end;
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
edit2.Enabled:=RadioGroup1.ItemIndex=;
button2.Enabled:=RadioGroup1.ItemIndex=;
end;
end.
  1. 文件,文件夹删除移动和拷贝、遍历目录查找文件中的字符并替换 (链接参考
posted @ 2024-05-16 17:33  月如无恨月长圆  阅读(11)  评论(0编辑  收藏  举报