首页 > 其他分享 >delphi 文件的操作:重命名、复制、移动、删除 文件(转)

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

时间:2024-05-16 17:44:41浏览次数:11  
标签:重命名 文件 begin end delphi filename StrLstDelte VerPlatForm procedure

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. 文件,文件夹删除移动和拷贝、遍历目录查找文件中的字符并替换 (链接参考

标签:重命名,文件,begin,end,delphi,filename,StrLstDelte,VerPlatForm,procedure
From: https://www.cnblogs.com/qiao-fu/p/18196372

相关文章

  • 国产linux系统(银河麒麟,统信uos)使用 PageOffice 国产版在线动态填充 word 文件
    PageOffice国产版:支持信创系统,支持银河麒麟V10和统信UOS,支持X86(intel、兆芯、海光等)、ARM(飞腾、鲲鹏、麒麟等)芯片架构。在实际的Word文档开发中,经常需要自动填充数据到Word模板中,以生成动态的Word文档。例如,我们可以根据数据库表中已保存的个人信息,设计好一个简历模板docx文件,......
  • Springboot配置文件Properties密码加密
    1.添加依赖<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version></dependency>2.启动类添加注解@EnableEncryptableProperties......
  • 任意文件上传下载漏洞
    任意文件下载漏洞产生原因进行文件包含展示或者文件下载的没有对用户访问的url进行过滤导致任意文件下载漏洞出现,也可以叫做目录穿越漏洞。事故多发地经常在如下的url中:download?path=down?file=data?file=download?filename=?src=?filepath=?path=?data=利用方法......
  • 可以高效记事、储存文件的桌面便签软件
    在日常工作中,我们经常面临各种信息和文件的管理挑战。比如,在策划一个重要的项目时,需要随时记录并更新进度、存储相关文件;在与客户沟通时,需要快速记下他们的需求和反馈;在准备会议时,需要整理会议要点和相关资料。这些场景下,一个能高效记事和储存文件的工具就显得至关重要。那么可以......
  • 文件上传和下载
    1.准备工作对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的。—般选择采用apache的开源工具common-fileupload这个文件上传组件。common-fileupload是依赖于common-io这个包的,所以还需要下载这个包。前往Maven仓库去下载:common-io-2.4:https://mvnrepos......
  • openGauss 在XFS文件系统中-使用du命令查询数据文件大小大于文件实际大小
    在XFS文件系统中,使用du命令查询数据文件大小大于文件实际大小问题现象在数据库使用过程中,通过如下du命令查询数据文件大小,查询结果大于文件实际的大小。du-shfile原因分析XFS文件系统有预分配机制,预分配的大小由参数allocsize确定。du命令显示的文件大小包括该预分配的磁......
  • openGauss 在XFS文件系统中-出现文件损坏
    在XFS文件系统中,出现文件损坏问题现象在数据库使用过程中,有极小的概率出现XFS文件系统的报错()Input/Outputerror,structureneedscleaning)。原因分析此为XFS文件系统问题。处理办法首先尝试umount/mount对应文件系统,重试看是否可以规避此问题。如果问题重现,则需要参考文......
  • java下载zip文件
    一、使用工具*java.utils下的ZipOutputStream*java.net的http请求工具HttpURLConnection二、zip下载1.通过浏览器以附件的形式下载到客户端思路:response的write方法要写出一个byte[],所以我们需要从ZipStreamOutputStream中获取到byte[]。在java中......
  • 把markdown文件转换为html文件
    使用pipinstallmarkdown模块只做到了分行;表格,-,和空格还没能无缝转换代码如下:importosimportcodecsimportmarkdowndefconvert_markdown_to_html(markdown_file):withcodecs.open(markdown_file,'r',encoding='utf-8')asfile:markdown_text=fil......
  • 位于 /var/log 目录下的日志文件
    “/var/log”是Linux系统登录文件放置的地方,里面就是记录点日志,可以删除,不过为了句柄安全,最好删除后重启xenserver(就是重启虚拟机)。以下是位于/var/log/目录下的不同的日志文件。其中一些日志文件是特定于发行版的。例如,您会在基于Debian的系统上看到dpkg.log(例如,在Ubun......