Beng Dou

一只站在树上的鸟儿,从来不会害怕树枝断裂,因为它相信的不是树枝,而是它自己的翅膀。

导航

批量修改指定目录下的文件名和文件内容

需求 

  批量修改替换Linux/Windows系统中指定目录下的目录名、文件名、文件内容。说明一下,提升工作效率的需求不一定都需要自己动手编程去实现,可以通过借助工具中已有的功能去实现。比如去掉字符串中前、中、后的空格、替换文件指定字符串等,可以借助编辑器中替换功能快速的实现。

Windows系统

1、Windows系统下替换文件内容中的字符串。比如将文件中的字符串"DTS20180506"替换为"DTS20190606"。

(1)使用Notepad++的文件内容替换功能。操作步骤:打开Notepad++ --> 搜索 --> 在文件中查找(也可以使用快捷键CTRL+f),界面和使用示例如下:

    同时,在这里再介绍一下Windows下操作文本的几个主要场景:去掉行首空格、行尾空格、行首行尾全部空格,在行首添加字符串,在行尾添加字符串。通过使用正则表达式+文本处理器(Notepad++、UE等)完成

  •    去掉行首空格、行尾空格、行首行尾全部空格。下图表示替换行首空格。如果是替换行尾空格,查找目标输入为([ \t]$)。注意勾选查找模式中的正则表达式。

       

     如果是Notepad++编辑器,也可以使用菜单中的功能实现。路径如下:Notepad++主界面 --> 编辑 --> 空白字符操作。

  • 在行首添加字符串。在查找目标中输入^,替换目标中输入字符串。如需要在每行记录前面添加字符串root,示例如下

  • 在行尾添加字符串。在查找目标中输入$,替换目标中输入字符串。如需要在每行行尾添加字符串end。示例如下

2、Windows下修改指定目录下的文件名和目录名。假设指定目录为D:\dir_temp,其子目录结构如下:

D:\dir_temp>tree /f
卷 LENOVO 的文件夹 PATH 列表
卷序列号为 D892-96E6
D:.
└─DTS20180506_001
        DTS20180506_001.txt
        DTS20180506_002.txt
        DTS20180506_003.txt

其中1个文件内容如下:

D:\dir_temp\DTS20180506_001>type DTS20180506_002.txt
DTS20180506_001_001 DTS20180506_001_001

当前需要将文文件名、目录名和文件中的内容字符串"DTS20180506"替换为"DTS20190606"。

脚本代码

#-*- encoding:utf-8 -*-

import sys, os, fileinput

#global variable define
work_dir = u"D:\dir_temp"
old_str = "DTS20180506"
new_str = "DTS20190302"
#end

if not os.path.exists(work_dir) or not os.path.isdir(work_dir):
    print "Please input correct directory,exit"
    sys.exit(1)

for root, dir, files in os.walk(work_dir,topdown=False):
    for filename in files:
        file_path = os.path.join(root,filename)
        
        #replace old string to new string in file
        repl_total = 0
        line_repl_count = 0
        for line in fileinput.input(file_path, inplace=1):
            if line.find(old_str) > -1:
                new_line = line.replace(old_str,new_str)
                line_repl_count = new_line.count(new_str)
                repl_total += line_repl_count
                print new_line,
            else:
                print line,
        
        #change file name
        new_filename = filename.replace(old_str,new_str)
        new_filename_path = os.path.join(root,new_filename)
        if filename != new_filename:
            os.rename(file_path,new_filename_path)
        else:
            print "The file %s not change, %d occurrences were replaced" % (filename, repl_total)
            continue
        if os.path.exists(new_filename_path):
            print "Change file name %s to %s success,, %d occurrences were replaced" % (filename, new_filename, repl_total)
        else:
            print "Change file name %s to %s fail." % (filename, new_filename)
            
    #change directory name
    for eachDir in dir:
        dir_path = os.path.join(root,eachDir)
        new_dir_name = eachDir.replace(old_str,new_str)
        new_dir_name_path = os.path.join(root,new_dir_name)
        if eachDir != new_dir_name:
            os.rename(dir_path, new_dir_name_path)
        else:
            print "The directory %s not change." % (eachDir,)  
            continue            
        if os.path.exists(new_dir_name_path):
            print "Change directory name %s to %s success." % (eachDir, new_dir_name) 
        else:
            print "Change directory name %s to %s fail." % (eachDir, new_dir_name)         

使用方法

(1)将代码中的work_dir、old_str、new_str分别替换要需要指定操作的目录、待替换字符串、目标字符串。执行过程如下:

Change file name DTS20180506_001.txt to DTS20190606_001.txt success,, 7 occurrences were replaced
Change file name DTS20180506_002.txt to DTS20190606_002.txt success,, 2 occurrences were replaced
Change file name DTS20180506_003.txt to DTS20190606_003.txt success,, 7 occurrences were replaced
D:\dir_temp\DTS20190606_001
D:\dir_temp\DTS20180506_001
Change directory name DTS20180506_001 to DTS20190606_001 success.
请按任意键继续. . .

子目录名和文件名修改成功,结果如下:

d:\dir_temp>tree /f
卷 LENOVO 的文件夹 PATH 列表
卷序列号为 D892-96E6
D:.
└─DTS20190606_001
        DTS20190606_001.txt
        DTS20190606_002.txt
        DTS20190606_003.txt

文件内容修改成功,如下:

d:\dir_temp\DTS20190606_001>type DTS20190606_002.txt
DTS20190606_001_001 DTS20190606_001_001

 Linux系统

1、Linux替换指定目录下(包含子目录)的所有文件中的字符串

find . -name "*.txt" -print0 |xargs -n 0 sed -i 's/DTS20180506/DTS20190606/g'

如果仅替换当前目录下(不包含子目录)的所有文件中的字符串,可以使用如下命令

sed -i 's/DTS20180506/DTS20190606/g' *.txt

2、Linux中替换指定目录下的文件名。使用rename命令。下述命令表示将修改当前目录中以.txt为后缀的文件,将文件名中DTS20180506字符串替换为DTS20190606。

rename DTS20180506 DTS20190606 *.txt

 说明:C语言版本的rename指令中文件名可以使用正则表达式匹配,但是新、旧字符串只能精确匹配,这就意味着如果你需要替换的字符串中有一个字符是变化的,就无法进行一次性批量修改。

posted on 2018-08-05 10:14  锅边糊  阅读(14675)  评论(0编辑  收藏  举报