您的位置:首页 > 其它

媒体文件批量改名(递归实现)

2015-02-17 17:24 281 查看
写了一个方法,方便统一批量修改电视剧文件名:

MediaFileUtils:

package net.deniro.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 媒体文件
*
* @author deniro
*         15-2-17上午10:31
*/
public class MediaFileUtils {

/**
* 需要过滤的关键词
*/
private static List<String> filterKeywords = Arrays.asList(new String[]{"www.2tu.cc", "S01"});
private static Logger logger = Logger.getLogger(MediaFileUtils.class);

public static void main(String[] args) {
recursionAndRename("E:\\tv");
}

/**
* 遍历并重命名
*
* @param path
* @return
*/
public static void recursionAndRename(String path) {
if (StringUtils.isEmpty(path)) {
logger.error("路径为空。");
return;
}

File f = new File(path);
new MediaFileUtils().recursionFile(f);
}

/**
* 提取集号
*
* @return
*/
public static String extractNos(String str) {

if (StringUtils.isEmpty(str)) return str;

for (String filterKeyword : filterKeywords) {
if (str.contains(filterKeyword)) {
str = str.replace(filterKeyword, "");
}
}

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
return matcher.group(0);
}

return str;
}

/**
* 获取文件扩展名
*
* @param file
* @return
*/
private static String getFileExtension(File file) {
String fileName = file.getName();
if (fileName.lastIndexOf(".") > 0) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
} else {
return fileName.replace(extractNos(fileName), "");
}
}

/**
* 遍历文件夹(递归)
*
* @param f
*/
public void recursionFile(File f) {
File[] files = f.listFiles();
for (File file : files) {
if (file.isDirectory()) {
recursionFile(file);
} else {
try {
String extension = getFileExtension(file);
File newFile = new File(file.getParent() + "\\" + extractNos(file.getName().replace("." + extension, "")) + "." + extension);
file.renameTo(newFile);
} catch (Exception e) {
logger.error("文件改名", e);
}
}
}
}

}


单元测试:

package utils;

import net.deniro.utils.MediaFileUtils;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Before;
import org.junit.Test;

import java.io.File;

import static junit.framework.Assert.assertEquals;
import static net.deniro.utils.MediaFileUtils.extractNos;

/**
* @author deniro
*         15-2-17上午10:51
*/
public class MediaFileUtilsTest {
@Before
public void start(){
PropertyConfigurator.configure("src/main/resources/log4j.properties");
System.out.println("start");
}

@Test
public void recursionFileTest(){

//        File file=new File("F:\\temp\\recursionFileTest\\Mother-001.rmvb");
//        File newFile=new File("F:\\temp\\recursionFileTest\\001.rmvb");
//        file.renameTo(newFile);

new MediaFileUtils().recursionFile(new File("F:\\temp\\recursionFileTest"));
}

@Test
public void extractNosTest() {
assertEquals(extractNos("Mother-001"), "001");
assertEquals(extractNos("[Hachikuro_Honey_and_Clover][POPGO][Serials][Hachikuro_Honey_and_Clover][GB]02"), "02");
assertEquals(extractNos("[迅雷免费高清下载].浪客剑心 追忆篇__第1集__480P标清.xv"), "1");
assertEquals(extractNos("老爸老妈浪漫史第三季-015"), "015");
assertEquals(extractNos("破产姐妹.第一季EP04V2"), "04");
assertEquals(extractNos("[迅雷下载www.2tu.cc]打工姐妹花.第二季EP01"), "01");
assertEquals(extractNos("生死线-第2集"), "2");
assertEquals(extractNos("生死线-19"), "19");
assertEquals(extractNos("十诫之八~心灵之罪"), "十诫之八~心灵之罪");
assertEquals(extractNos("我的团长我的团-001"), "001");
assertEquals(extractNos("无耻家庭.Shameless.US.S01E02.Chi_Eng.BDrip.720X400-YYeTs人人影视"), "02");
assertEquals(extractNos("[迅雷下载www.2tu.cc]无耻之徒.第二季EP08"), "08");
assertEquals(extractNos("[迅雷下载www.2tu.cc]无耻之徒.第三季EP04"), "04");
assertEquals(extractNos("生死线-19"), "19");
assertEquals(extractNos("08"), "08");
assertEquals(extractNos("雍正王朝01"), "01");

}
}


pom文件:

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>

<dependency>
<groupId>apache-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>


log4j.properties

#设置日志级别
log4j.rootLogger=debug,stdout

# %d 输出日志时间点的日期或时间
# %p 输出优先级,DEBUG,INFO,WARN,ERROR,FATAL
# %c 输出所属类全名
# %l 输出日志发生的位置,包括类名、线程以及代码中的所在行数
# %m 输出代码中指定输出的信息
# %n 输出一个回车换行符

#输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%m ----- %d{yyyy-MM-dd HH:mm:ss.SSS} %p:%l%n


批量结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: