文件批量处理

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class fileBP implements string_Processing
{
    public static String ENCODING="UTF-8";
    public static void main(String args[])
    {
        //Scanner sc = new Scanner(System.in,"GBK"); 
        Scanner sc = new Scanner(System.in,ENCODING); 
        System.out.println("请输入预计访问的路径");
        String path=(sc.nextLine()).trim();
        System.out.println(showMessage.getInfo());
         while(true)
         {
            new fileBP().sP( (sc.nextLine()).trim(),path);
         }
    }

    @Override
    public  void sP(String parm, String path) 
    {
        String s[];//临时变量,用于分割参数
        File file;//临时变量
        String name;//临时变量
        List<File> list=pP(path);   
        Scanner sc = new Scanner(System.in,ENCODING); 
        if(list.size()>0)
        {
            file = list.get(0);
            name =file.getName();
            StringBuilder sb;
            if(parm.contains("-"))
            {
                s=parm.split("-");   
                //-结果
                if(Pattern.compile("^\\-((?!\\-).)+").matcher(parm).matches())
                {
                    sb = new StringBuilder();
                    sb.append("原文件名:").append(name);
                    sb.append(",修改后文件名:").append(name.substring(0,name.indexOf(s[1])+s[1].length()));
                    sb.append(",确认是否继续,输入Y表示继续:");
                    System.out.println(sb);
                    if( "Y".equals((sc.nextLine()).trim()))
                    {
                        for(int i=0;i<list.size();i++)
                        {
                            sb = new StringBuilder();
                            file = list.get(i);
                            name =file.getName();
                            if(!file.renameTo(new File(file.getParent()+File.separator+name.substring(0,name.indexOf(s[1])+s[1].length()))))   
                            {   
                                System.out.println("更改失败!");   
                            }  
                            sb.append("原文件名:").append(name);
                            sb.append(",修改后文件名:").append(name.substring(0,name.indexOf(s[1])+s[1].length()));
                            sb.append(",原路径:").append( file.getParent()).append(File.separator).append(name);
                            sb.append(",修改后:").append( file.getParent()).append(File.separator).append(name.substring(0,name.indexOf(s[1])+s[1].length()));
                            System.out.println(sb);
                        }
                    }
                }
                 //结果-
                if(Pattern.compile("((?!\\-).)+\\-$").matcher(parm).matches())
                {
                    sb = new StringBuilder();
                    sb.append("原文件名:").append(name);
                    sb.append(",修改后文件名:").append( name.substring(name.indexOf(s[0]),name.length()));
                    sb.append(",确认是否继续,输入Y表示继续:");
                    System.out.println(sb);
                    if( "Y".equals((sc.nextLine()).trim()))
                    {
                        for(int i=0;i<list.size();i++)
                        {
                            file = list.get(i);
                            name =file.getName();
                            if(!file.renameTo(new File(file.getParent()+File.separator+name.substring(name.indexOf(s[0]),name.length()))))   
                            {   
                                System.out.println("更改失败!");   
                            }  
                            sb = new StringBuilder();
                            sb.append("原文件名:").append(name);
                            sb.append(",修改后文件名:").append(name.substring(name.indexOf(s[0]),name.length()));
                            sb.append(",原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append(name.substring(name.indexOf(s[0]),name.length()));
                            System.out.println(sb);
                        }
                    }      
                }
                //4-打印
                if(Pattern.compile("^[0-9]+\\-((?!\\-).)+").matcher(parm).matches())
                {
                    sb = new StringBuilder();
                    sb.append("原文件名:").append(name);
                    sb.append("修改后文件名:").append( name.substring(0,Integer.parseInt(s[0]))).append(s[1]).append(name.substring(Integer.parseInt(s[0]),name.length()));
                    sb.append("确认是否继续,输入Y表示继续:");
                    System.out.println(sb);
                    if( "Y".equals((sc.nextLine()).trim()))
                    {
                        for(int i=0;i<list.size();i++)
                        {
                            file = list.get(i);
                            name =file.getName();
                            if(!file.renameTo(new File(file.getParent()+File.separator+ name.substring(0,Integer.parseInt(s[0]))+s[1]+name.substring(Integer.parseInt(s[0]),name.length()))))
                            {   
                                System.out.println("更改失败!");   
                            }  
                            sb = new StringBuilder();
                            sb.append("原文件名:").append(name);
                            sb.append("修改后文件名:").append( name.substring(0,Integer.parseInt(s[0]))).append(s[1]).append(name.substring(Integer.parseInt(s[0]),name.length()));
                            sb.append("原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.substring(0,Integer.parseInt(s[0]))).append(s[1]).append(name.substring(Integer.parseInt(s[0]),name.length()));
                            System.out.println(sb);
                        }
                    }
                }
                //1-2-a
                if(Pattern.compile("^[0-9]+\\-[0-9]+\\-((?!\\-).)+").matcher(parm).matches())
                {
                    sb = new StringBuilder();
                    sb.append("原文件名:").append(name);
                    sb.append(",修改后文件名:").append( name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]));
                    sb.append(",确认是否继续,输入Y表示继续:");
                    System.out.println(sb);
                    if( "Y".equals((sc.nextLine()).trim()))
                    {
                        for(int i=0;i<list.size();i++)
                        {
                            file = list.get(i);
                            name =file.getName();
                            if(!file.renameTo(new File(file.getParent()+File.separator+ name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]))))
                            {   
                                System.out.println("更改失败!");   
                            }  
                            sb = new StringBuilder();
                            sb.append("原文件名:").append(name);
                            sb.append(",修改后文件名:").append( name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]));
                            sb.append(",原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]));
                            System.out.println(sb);
                        }
                    }
                }
            }
            if(parm.contains("--"))
             {
                s=parm.split("--");   
                 //a--b
                if(Pattern.compile("((?!\\-).)+\\-\\-((?!\\-).)+").matcher(parm).matches())
                {
                    sb = new StringBuilder();
                    sb.append("原文件名:").append(name);
                    sb.append("修改后文件名:").append(name.replace(s[0], s[1]));
                    sb.append("确认是否继续,输入Y表示继续:");
                    System.out.println(sb);
                    if( "Y".equals((sc.nextLine()).trim()))
                    {
                        for(int i=0;i<list.size();i++)
                        {
                            file = list.get(i);
                            name =file.getName();
                            if(!file.renameTo(new File(file.getParent()+File.separator+ name.replace(s[0], s[1]))))
                            {   
                                System.out.println("更改失败!");   
                            }  
                            sb = new StringBuilder();
                            sb.append("原文件名:").append(name);
                            sb.append("修改后文件名:").append( name.replace(s[0], s[1]));
                            sb.append("原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.replace(s[0], s[1]));
                            System.out.println(sb);
                        }
                    }
                }
                //1-5-000
                if(Pattern.compile("^[0-9]+\\-\\-[0-9]+\\-\\-[0]*").matcher(parm).matches())
                {
                    System.out.println("请输入预计排序的依据,格式为2-4,程序会排除其中的非数字部分:");
                    String s1[]=(sc.nextLine()).trim().split("-");//排序
                    int k=name.length();
                    String str;
                    if(s1.length>1)
                        k=Integer.parseInt(s1[1]);

                    if(s.length>2)
                        str=new DecimalFormat(s[2]).format(Integer.parseInt(s[1]));
                    else
                        str=s[1];
                    sb = new StringBuilder();
                    sb.append("原文件名:").append(name);
                    sb.append("修改后文件名:").append(name.substring(0,Integer.parseInt(s[0]))).append(str).append(name.substring(Integer.parseInt(s[0]),name.length()));
                    sb.append("排序数:").append(Pattern.compile("[^0-9]").matcher(name.substring(Integer.parseInt(s1[0]),k)).replaceAll(" ").trim());    
                    sb.append("确认是否继续,输入Y表示继续:");
                    System.out.println(sb);
                    if( "Y".equals((sc.nextLine()).trim()))
                    {
                        List <duplicateRemoval>list1=xupp(list,s1);

                        for(int i=0;i<list1.size();i++)
                        {
                            file = list1.get(i).getpath();
                            name =file.getName();     
                            k=name.length();

                            if(s1.length>1)
                                k=Integer.parseInt(s1[1]);     

                            if(s.length>2)
                                str=new DecimalFormat(s[2]).format(Integer.parseInt(s[1])+i);
                            else
                                str=(Integer.parseInt(s[1])+i)+"";
                            
                            if(!file.renameTo(new File(file.getParent()+File.separator+  name.substring(0,Integer.parseInt(s[0]))+str+name.substring(Integer.parseInt(s[0]),name.length())  )))
                            {   
                                System.out.println("更改失败!");   
                            }  
                            sb = new StringBuilder();
                            sb.append("原文件名:").append(name);
                            sb.append("排序数:").append(Pattern.compile("[^0-9]").matcher(name.substring(Integer.parseInt(s1[0]),k)).replaceAll(" ").trim());    
                            sb.append("原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.substring(0,Integer.parseInt(s[0]))).append(str).append(name.substring(Integer.parseInt(s[0]),name.length()));
                            System.out.println(sb);
                        }
                    }
                }
            }
            if("show".equals(parm.trim()))
            {
                System.out.println("请输入预计排序的依据,格式为2-4,程序会排除其中的非数字部分:");
                String s1[]=(sc.nextLine()).trim().split("-");//排序
                List <duplicateRemoval>list1=xupp(list,s1);
                for(int i=0;i<list1.size();i++)
                {
                    System.out.println(list1.get(i).getpath());
                }
            }
            if("copy".equals(parm.trim()))
            {
                System.out.println("请输入预计排序的依据,格式为2-4,程序会排除其中的非数字部分:");
                String s1[]=(sc.nextLine()).trim().split("-");//排序
                List <duplicateRemoval>list1=xupp(list,s1);
                
                System.out.println("拷贝的路径:");
                String ph=sc.nextLine().trim();
                File f0;
                for(int i=0;i<list1.size();i++)
                {
                    f0=list1.get(i).getpath();   
                    fileChannelCopy(f0,new File(ph+File.separator+f0.getName()));
                    System.out.println("预计拷贝文件数为:"+list.size()+",当前拷贝到第"+(i+1)+"个,文件路径及文件名为:"+f0);
                }
            }
        }
        if("exit".equals(parm.trim()))
        {
            System.exit(0);
        }
        System.out.println(showMessage.getInfo());
    }

    @Override
    public List<File> pP(String path) 
    {
        File file[]=new File(path).listFiles();
        List <File>list = new ArrayList<>();
        for (File file1 : file) 
            if (file1.isFile()) 
                list.add(file1);
        return list;
    }

    @Override
    public List xupp(List <File>list,String s1[]) 
    {
        File file;
        String name;
        int k;
        List <duplicateRemoval>list1 = new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            file = list.get(i);
            name =file.getName();      
            k=name.length();
            if(s1.length>1)
                    k=Integer.parseInt(s1[1]);      
            list1.add(new duplicateRemoval(Pattern.compile("[^0-9]").matcher(name.substring(Integer.parseInt(s1[0]),k)).replaceAll(" ").trim(),file));
        }
        Collections.sort(list1);
        for(int i=0;i<list1.size();i++)
        {
            System.out.println("排序结果:"+list1.get(i).getpath());
        }
        return list1;
    }
    @Override
    public  void fileChannelCopy(File s, File t) 
    {
        FileChannel in = null;
        try {
            in = new FileInputStream(s).getChannel();//得到对应的文件通道
            in.transferTo(0, in.size(), new FileOutputStream(t).getChannel());//连接两个通道,并且从in通道读取,然后写入out通道
        }   catch (FileNotFoundException ex) {
            Logger.getLogger(fileBP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(fileBP.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            try {
                if(in!=null)
                    in.close();
            }
            catch (IOException ex) {
                Logger.getLogger(fileBP.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
interface string_Processing //方法申明
{
    void sP(String parm,String path);//根据参数,文件路径返回文件路径
    List pP(String path);//获取文件
    List xupp(List <File>list,String s1[]);
    void fileChannelCopy(File s, File t) ;
    
}
class showMessage//提示信息显示
{
    private static  StringBuilder Info;
    public static String getInfo()
    {
        Info = new StringBuilder();//提示语句拼接
        Info.append("请按提示格式输入参数,参考文件名为:“测试输入结果看下.mp4”\r\n");
        Info.append("       1.文件名称右截取,输入参数“-结果”,得到文件名称:“测试输入结果”\r\n");
        Info.append("       2.文件名称左截取,输入参数“结果-”,得到文件名称:“结果看下.mp4”\r\n");
        Info.append("       3.指定位置插入字符串,输入参数“4-打印”,得到文件名称:“测试输入打印结果看下.mp4”\r\n");
        Info.append("       4.指定位置字符串替换,输入参数“2-7-看”,得到文件名称:“测试看下.mp4”\r\n");
        Info.append("       5.字符串替换(中间双横杠),输入参数“输入结果看--看”,得到文件名称:“测试看下.mp4”\r\n");
        Info.append("       6.指定位置插入指定格式的数字(中间双横杠),输入参数”1--10--000“,文件名第1个字后面插入从10开始的数字,并且把数字格式化为000”\r\n");
        Info.append("ps:\r\n");
        Info.append("       1.输入show,表示显示当前文件的排列顺序\r\n");
        Info.append("       1.输入copy,表示需要拷贝文件\r\n");
        Info.append("       3.输入exit表示退出程序\r\n");
        return Info.toString();
    }
}
class duplicateRemoval implements Comparable<duplicateRemoval>//去重复
{
    private final  String  str;//比较值
    private final  File  path;//路径
    public duplicateRemoval(String str,File path)
    {
        this.str=str;
        this.path=path;
    }
    public  String getstr()//只去字符串中的数字部分
    {
        return str;
    }
    public  File getpath()//只去字符串中的数字部分
    {
        return path;
    }
    @Override
    public int compareTo(duplicateRemoval o) 
    {
        return new java.math.BigDecimal(this.str).subtract(new java.math.BigDecimal(o.getstr())).intValue();
    }
}
时间: 2024-08-05 11:09:50

文件批量处理的相关文章

wwwscan网站目录文件批量扫描工具

准备一个比赛样题里面给的一个扫描的工具: 不知道怎么用就上网百度了一下果然有关于这个软件的两篇介绍(感觉写的很好),第一篇介绍的应该和我的工具一样,也给了例子(现在Google不能访问了)和参数介绍,第二篇作者可能自己"升级"过软件也提供了下载地址.但是有个问题是:我机器上跑感觉那些多进程.端口和超时等参数都是虚设的,我试验的结果是我的软件只要输入ip或域名就可以运行扫描. ps:直接copy 文章来源:http://blog.chinaunix.net/uid-26726420-id

linux 目录下文件批量植入和删除

linux目录下文件批量植入 [[email protected] http2]# find /usr/local/http2/htdocs/ -type f|xargs sed -i "1  i <script>alert(1)</script>" 其中上面的1 为文件的第一行 i为植入 批量删除: [[email protected] ~]# find /usr/local/http2/htdocs/ -type f|xargs sed -i '/<s

jquery文件批量上传控件Uploadify3.2(java springMVC)

人比較懒  有用为主 不怎么排版了 先放上Uploadify的官网链接:http://www.uploadify.com/  -->里面能够看到PHP的演示样例,属性说明,以及控件下载地址.分flash版(免费)和html5版(收费). 官网上能够看到效果演示. 另外在网上找到了一份Uploadify3.2的中文API文档.能够在我上传的资源里面下载. 以下就是一个基于springMVC的文件上传演示样例,看起来可能比文档更直观一些··· 1.下载插件放到项目中 2.在jsp页面中引入下面JS和

Batch File Rename Utility(文件批量改名软件) 1.1.4231

软件名称: Batch File Rename Utility(文件批量改名软件) 1.1.4231.23098 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP / Win2008 软件大小: 2.2MB 图片预览: 软件简介: Batch File Rename Utility是一个易于使用的文件和文件夹改名工具.提供灵活的选择配置的规则,可以组合和堆叠.它允许你替换字符,删除/添加号码,删除/添加文本,变更,分裂名称等等.该计

【分享】Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件

[分享]Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件 在Andoird Studio下编译java文件时,经常会出现像下面的错误: Error:(29, 43) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 在这里,分享一个工具:ANSI文件批量另存为无BOM的UTF-8文件: 把下面代码用记事本存为AndroidStudioJava编码.vbs,双击即可使用: on error resume next Set WshShell=WScrip

带进度条的文件批量上传插件uploadify

有时项目中需要一个文件批量上传功能时,个人认为uploadify是快速简便的解决方案. 先上效果图: 一. 下载uploadify 从官网下载uploadify的Flash版本(Flash版本免费,另一版本HTML5版本需要付费) 下载地址: http://www.uploadify.com/download/ 下载后直接把文件解压,然后放在项目中 二. 在项目中使用 在页面中引入: <!--引入Jquery--> <script src="js/jquery-1.11.3.m

R语言之RCurl实现文件批量下载

R语言之RCurl实现文件批量下载 前言: RCurl工具包的作者是由Duncan Temple Lang现任加州大学 U.C. Davis分校副教授.他曾致力于借助统计整合进行信息技术的探索.使用者通过RCurl可以轻易访问网页,进行相关数据的抓取以及下载,为数据分析提供原始素材.近年RCurl在数据分析业界中使用也越来越流行. Step1:安装RCurl install.packages('RCurl') Step2:代码实现 =========================== 1 #利

wps文件批量转换到 microsoft office平台 软件

wps文件批量转换到 microsoft office平台 软件,如果文件少的话,用WPS的另存为功能 就可以了, 如果文件多的话,试试如下的软件吧,也是金山官方放出来的. 下载地址: http://www.400gb.com/file/81380250 或者这个地址也可以下载到:http://chicxp.7958.com.cn/down_19487111.html

asp.net 文件批量选取,批量上传,带进度条,uploadify3.2 TOP

http://www.16aspx.com/Article/3444 asp.net 文件批量选取,批量上传,带进度条,uploadify3.2 TOP,布布扣,bubuko.com

C#实现文件批量重命名源码下载

本文要实现的功能是 文件批量重命名,当选择一个文件夹时,通过操作可以把文件夹下面所有文件进行重命名.建立了HoverTreeBatch项目. 然后 定义文件夹信息: DirectoryInfo _TheFolder; //这里是选择文件夹并实例化_TheFolder FileInfo[] _files=null; _files = _TheFolder.GetFiles(); 这样就得到了文件夹下所有文件信息. //加后缀 foreach (FileInfo fi in _files) { Ho