unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,FileCtrl,ShellAPI;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
 countd,countf:integer;
 sum:integer;
implementation

{$R *.dfm}
//调用方法:ShellFileOperation('c:/afile.txt', 'd:/afile2.txt', FO_COPY); FO_MOVE
procedure ShellFileOperation(fromFile: string; toFile: string; Flags: Integer);
var
  shellinfo: TSHFileOpStructA;
begin
   with shellinfo do
   begin
    wnd    := Application.Handle;
     wFunc := Flags;
     pFrom := PChar(fromFile);
     pTo    := PChar(toFile);
  end;
   SHFileOperation(shellinfo);
end;

// Example, Beispiel:

function   RenDirectory(const   OldName,NewName:string):   boolean;
  var
      fo:   TSHFILEOPSTRUCT;
  begin
      FillChar(fo,   SizeOf(fo),   0);
      with   fo   do
      begin
          Wnd   :=   0;
          wFunc   :=   FO_RENAME;
          pFrom   :=   PChar(OldName+#0);
          pTo   :=   pchar(NewName+#0);
          fFlags   :=   FOF_NOCONFIRMATION+FOF_SILENT;
      end;   
      Result   :=   (SHFileOperation(fo)   =   0);
  end;

procedure   search(dir:string);   
  var   
      targetpath:string;{目标路径名}   
      sr:TsearchRec;   
    
  begin
      {第一阶段:找出初始dir目录下的所有文件,   
      其中dir变量值由edit1的Text属性确定}   
    
      targetpath:=extractfilepath(dir);{分解出目标路径名}   
      if   findfirst(dir,faanyfile,sr)=0   then
      repeat
        if((sr.name<>'.')and(sr.name<>'..'){排除父目录和本目录两个假文件}   
        and((filegetattr(targetpath+sr.name)and   fadirectory)<>fadirectory)){只取文件}   
        then   
            begin
             sum:=sum+1;

RenameFile(targetpath+sr.name, targetpath+inttostr(sum));

countf:=countf+1;
            end
      until   findnext(sr)<>0;   
    
      if   findfirst(dir,faanyfile,sr)=0   then   
      repeat   
        if((sr.name<>'.')and(sr.name<>'..')){排除父目录和本目录两个假文件}   
        and((filegetattr(targetpath+sr.name)and   fadirectory)=fadirectory){排除文件}   
        then   
          begin
            sum:=sum+1;
            RenDirectory(targetpath+sr.name, targetpath+inttostr(sum));
            search(targetpath+inttostr(sum)+'/*.*');{递归调用}
            //search(targetpath+sr.name+'/*.*');{递归调用}

countd:=countd+1;   
          end
      until   findnext(sr)<>0;   
    
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  curpath,S: string;
  dt:TDateTime;
  fdt:double;
begin
  S := '';
  if SelectDirectory('选择要修改名字的文件夹', '', S) then
    begin
      countf:=0;
      countd:=0;
      randomize;
      sum:= 10000+Random(1000000-10000+1); ;
      search(s);{调用Search()函数}
      showmessage('共更改目录:'+inttostr(countd)+' 个,文件:'+inttostr(countf)+' 个')
    end;
end;

end.
 //RenameFile('Oldname', 'Newname');
//CopyFile(PChar('Oldname'), PChar('Newname'), False);
//MoveFile(PChar('Oldname'), PChar('Newname'));

delphi 修改文件夹名和文件名的更多相关文章

  1. python--批量修改文件夹名

    python代码如下: import os , re import os.path rootdir = r'C:\Users\Administrator\Desktop\222' # rootdir ...

  2. Excel VBA批量修改文件夹下的文件名

    今天,有同事提出想批量修改文件名,规则比较简单,在第五位后加“-”即可, 上网没找到相关工具,就自己做了个excel,用宏代码修改. 代码如下: Private Sub CommandButton1_ ...

  3. matlab 修改文件夹下所有文件名大写为小写

    1. path = './DIR/';Files = dir(fullfile(path,'*.m'));LengthFiles = length(Files);for count_i = 1 : L ...

  4. C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名

    原文:C# 文件操作(全部) 追加.拷贝.删除.移动文件.创建目录 修改文件名.文件夹名 本文也收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归 ...

  5. C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!

    https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new Ope ...

  6. Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间

    遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间 import osimport datetime def print_tree(dir_path): for ...

  7. 树莓派Ubuntu 16.04 MATA系统 修改用户文件夹名后,提示configure it with blueman-service

    自从修改了树莓派的Ubuntu 16.04 MATA 系统的 /home/ 下的用户文件夹名后,使用vncserver远程操作,看到桌面每次都提示 Configured directory for i ...

  8. opencart修改后台文件夹名

    在使用opencart进行二次开发时,若需要修改后台目录的文件夹名是可以操作的.具体步骤如下: 1.将网站后台文件夹名字改成opencartadmin 2.在该文件夹下找到config.php文件如图 ...

  9. Win10系统修改主机名、用户名称和密码、以及C盘中的用户文件夹名

    写在前面 近期重新安装了Ubuntu16.04系统,同时也修改了Windows10系统的用户名.密码,还有C盘用户文件夹名称.对于Linux和windows系统来说,修改名称基本都是三部分,主机名.用 ...

随机推荐

  1. python 字符与字节 json序列和反序列及支持的类型

    b = b"demo" s = "demo" # 字符串转字节 s = bytes(s, encoding = "utf8") s = st ...

  2. 指针-动态开点&合并线段树

    一个知识点不在一道题里说是没有灵魂的 线段树是用来处理区间信息的咯 但是往往因为需要4倍空间让许多人退却,而动态开点的线段树就非常棒 仿佛只用2倍就可以咯 指针保存位置,即节点信息,是很舒适的,所以用 ...

  3. 使用SpringBoot-JPA进行自定义的保存及批量保存

    更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...

  4. 欧拉函数 || LightOJ 1370 Bi-shoe and Phi-shoe

    给出x,求最小的y使y的欧拉函数大于等于x *解法:i).求出1e6之内的数的欧拉函数,遍历找             ii).求比x大的第一个质数——因为每个质数n的欧拉函数都是n-1 wa一次是因 ...

  5. freemarker list集合去重,实现hashset

    在freemarker中没有提供去重的方法,虽然有提供定义hash的方法,如:<#assign myHash = { "name": "mouse", & ...

  6. 自定义分隔符|/i|/x|/xs|需要转译

    小骆驼 第八章 用正则表达式进行匹配 #!/usr/bin/envperl use strict; use warnings; $_ ="#adchbehnyhme3534f\nvdh5ej ...

  7. 标准库中的智能指针shared_ptr

    智能指针的出现是为了能够更加方便的解决动态内存的管理问题.注:曾经记得有本书上说可以通过vector来实现动态分配的内存的自动管理,但是经过试验,在gcc4.8.5下是不行的.这个是容易理解的,vec ...

  8. MySQL字符集设定与查询

    一.字符集设定 MySQL数据库允许对服务器.数据库.数据表.数据列级别的字符集作出互不影响的设定. 1.对服务器字符集设定是通过配置文件中选项character-set-server 设置,如 ch ...

  9. Chrome浏览器 v68.0.3440.106 正式版怎么样?

    谷歌浏览器Google Chrome稳定版迎来v68正式版第三个维护版本发布,详细版本号为v68.0.3440.106,上一个正式版v68.0.3440.84发布于8月1日,时隔8天Google又发布 ...

  10. python基础知识11-文件操作

    文件 装饰器,装饰函数或者类的方法. 1.文件的基本操作 打开文件: 注意绝对路径与相对路径. path = 'text.txt' path = r'/home/pyvip/py_case/text. ...