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. 使用docker搭建gitlab 服务器

    本次使用的docker版本为 1.首先需要安装docker. 2.启动docker后,service docker start   3.拉取镜像  docker pull gitlab/gitlab- ...

  2. ignore-on-commit svn 更改文件后 默认不提交文件到服务器(服务器上已存在的文件)

    不用那个忽略文件那个,那个功能是删除服务器的文件,然后本地还存在,不符合我的要求 我的要求是 服务器文件在,我不动,然后我改完了,和别人的不冲突,我也不覆盖别人的文件 主要就是默认不提交,这个很重要 ...

  3. 路由器wan口ip地址显示0.0.0.0怎么办

    http://m.xuexila.com/luyouqi/671049.html 这个网络时代里面我们最常用来连接网络的设备就是路由器了,现在的社会不管是工作还是生活几乎都离不开网络了,同时我们也要学 ...

  4. 1.ssm web项目中的遇到的坑--自定义JQuery插件(slide menu)

    自定义的JQuery插件写的回调函数不执行: 写好了回调函数,将函数打印出来是原形,就是不执行 function () { console.log("---onClickItem---&qu ...

  5. spring mvc poi excel

    Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...

  6. 点击增加删除class

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. HNOI2006 潘多拉的盒子

    题目描述 题解: 题目的描述比较长,理解起来也有一定难度.仔细读题后我们发现整个任务可以分成两个部分:找出咒语机之间所有的升级关系.求最长升级序列. 1. 求升级关系: 容易看出,咒语机i可以抽象成一 ...

  8. (5) openssl speed(测试算法性能)和openssl rand(生成随机数)

    1.1 openssl speed 测试加密算法的性能 支持的算法有: openssl speed [md2] [mdc2] [md5] [hmac] [sha1] [rmd160] [idea-cb ...

  9. Angular 1.x 框架原理

    指令生命周期 compile阶段 对dom进行编译,首先(如果有的话)对template进行应用(这个过程只执行一次).然后把当前指令(内部的指令还没被渲染)传递给iElement,接着执行compi ...

  10. 自动化测试如何解析excel文件?

    前言 自动化测试中我们存放数据无非是使用文件或者数据库,那么文件可以是csv,xlsx,xml,甚至是txt文件,通常excel文件往往是我们的首选,无论是编写测试用例还是存放测试数据,excel都是 ...