C# 实现文本文件批量重命名工具

简介: 批量重命名工具可统一修改文件夹内文件名,支持添加前缀或替换关键词。通过C#代码实现,安全高效,避免文件覆盖,适用于大量文件的命名整理,操作简单便捷。

批量重命名工具用于统一修改文件夹下的文件名称(如添加前缀、替换关键词)。

案例:文件批量重命名

using System.IO;
public class FileRenameTool
{
    // 批量添加文件前缀
    public static void AddPrefix(string folderPath, string prefix)
    {
        if (!Directory.Exists(folderPath))
        {
            Console.WriteLine("文件夹不存在!");
            return;
        }
        // 获取文件夹下的所有文件
        string[] files = Directory.GetFiles(folderPath);
        int successCount = 0;
        foreach (string file in files)
        {
            try
            {
                string fileName = Path.GetFileName(file);
                string newFileName = prefix + fileName;
                string newFilePath = Path.Combine(folderPath, newFileName);
                // 避免覆盖已存在的文件
                if (!File.Exists(newFilePath))
                {
                    File.Move(file, newFilePath);
                    successCount++;
                    Console.WriteLine($"重命名成功:{fileName} → {newFileName}");
                }
                else
                {
                    Console.WriteLine($"跳过:{newFileName} 已存在");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"重命名失败({Path.GetFileName(file)}):{ex.Message}");
            }
        }
        Console.WriteLine($"批量重命名完成!成功重命名 {successCount} 个文件");
    }
    // 批量替换文件名中的关键词
    public static void ReplaceKeyword(string folderPath, string oldKeyword, string newKeyword)
    {
        if (!Directory.Exists(folderPath))
        {
            Console.WriteLine("文件夹不存在!");
            return;
        }
        string[] files = Directory.GetFiles(folderPath);
        int successCount = 0;
        foreach (string file in files)
        {
            try
            {
                string fileName = Path.GetFileName(file);
                if (fileName.Contains(oldKeyword))
                {
                    string newFileName = fileName.Replace(oldKeyword, newKeyword);
                    string newFilePath = Path.Combine(folderPath, newFileName);
                    if (!File.Exists(newFilePath))
                    {
                        File.Move(file, newFilePath);
                        successCount++;
                        Console.WriteLine($"重命名成功:{fileName} → {newFileName}");
                    }
                    else
                    {
                        Console.WriteLine($"跳过:{newFileName} 已存在");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过:{fileName} 不包含关键词 {oldKeyword}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"重命名失败({Path.GetFileName(file)}):{ex.Message}");
            }
        }
        Console.WriteLine($"批量替换完成!成功重命名 {successCount} 个文件");
    }
    public static void Main(string[] args)
    {
        Console.WriteLine("=== 文件批量重命名工具 ===");
        Console.Write("请输入文件夹路径:");
        string folderPath = Console.ReadLine()?.Trim() ?? "";
        Console.Write("请选择操作(1-添加前缀,2-替换关键词):");
        string choice = Console.ReadLine()?.Trim() ?? "";
        switch (choice)
        {
            case "1":
                Console.Write("请输入前缀:");
                string prefix = Console.ReadLine()?.Trim() ?? "";
                AddPrefix(folderPath, prefix);
                break;
            case "2":
                Console.Write("请输入要替换的关键词:");
                string oldKeyword = Console.ReadLine()?.Trim() ?? "";
                Console.Write("请输入替换后的关键词:");
                string newKeyword = Console.ReadLine()?.Trim() ?? "";
                ReplaceKeyword(folderPath, oldKeyword, newKeyword);
                break;
            default:
                Console.WriteLine("无效操作!");
                break;
        }
    }
}
相关文章
|
2天前
|
存储 安全 C#
C# 数组与集合:List<T> 最常用场景
数组长度固定,集合(List&lt;T&gt;)可动态增删,支持泛型与类型安全。常用操作包括添加、删除、遍历和查询元素,是开发中首选的动态数据存储方式。
|
2天前
|
存储 开发框架 缓存
C# 实现图片验证码的 WebAPI 版(ASP.NET Core)
基于ASP.NET Core实现的验证码服务,生成带干扰线和随机旋转文字的4位图文验证码,支持内存存储与验证。需安装System.Drawing.Common包。
|
2天前
|
C# 索引
C# 循环结构:for/foreach/while 效率对比
循环用于重复执行代码,根据场景选择:for 适合已知次数,foreach 简洁遍历集合,while 适用于条件控制。示例演示遍历商品价格列表并计算总和,展示三种循环的用法与差异。(239字)
|
2天前
|
C#
C# 实现简单的 Excel 数据写入工具
基于EPPlus库实现C#将数据写入Excel文件,支持自定义表头与多行数据,自动调整列宽,操作简便,适用于非商业场景的数据导出需求。
|
2天前
|
网络协议 安全 C#
C# 实现基于 TCP 的简单聊天程序(服务端)
基于TcpListener实现TCP聊天服务器,监听指定端口,接收客户端连接并实时转发消息。支持多客户端并发通信,新连接自动加入广播列表,断开后自动移除,确保线程安全与稳定运行。
|
2天前
|
安全 C# 数据安全/隐私保护
C# 实现验证码 + 登录的整合案例
整合图形验证码与用户登录功能,实现安全的登录验证流程。系统生成验证码图片并校验输入,结合用户名密码双重验证,提升账户安全性,有效防止自动化攻击。
|
2天前
|
C#
C# 条件判断:if-else 与 switch 的实用场景
条件判断是流程控制的核心。if-else适用于多级条件判断,如根据积分决定会员等级;switch适用于多值匹配,C# 7.0+支持字符串和数值匹配,可清晰处理会员等级对应的优惠策略。
|
2天前
|
算法 C# 容器
C# 实现简单的迭代器模式
迭代器模式提供一种顺序访问聚合对象元素的方法,无需暴露其内部结构。通过定义统一的迭代接口,实现对不同集合的遍历操作,增强容器的封装性和算法的复用性。
|
2天前
|
C#
52. C# 实现简单的责任链模式
通过责任链模式将请求的发送者与接收者解耦,多个处理器形成链式结构,依次处理不同请求。每个处理器可自行处理或传递给下一节点,提升灵活性与扩展性。
|
2天前
|
安全 C#
C# 实现简单的单例模式(懒汉式)
单例模式确保一个类仅存在一个实例,并提供全局访问点。通过私有构造函数防止外部实例化,结合静态属性和延迟初始化实现线程安全的唯一实例,常用于日志、配置管理等场景。

热门文章

最新文章