找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 400|回复: 10

难题,难题,难题,请帮忙解答!!

[复制链接]

27

主题

92

回帖

41

积分

新手上路

积分
41
发表于 2006-7-5 09:12:03 | 显示全部楼层 |阅读模式
C:\DATAS\D1和C:\DATAS\D2下各有一批可见文件。将这2个子目录下的文件复制到新建的子目录C:\D3中,但C:\DATAS\D1和C:\DATAS\D2中存在相同文件名的那些文件不复制,例如,两个子目录下都有文件ABCD.EFG,此文件就不应复制到C:\D3中。最后把C:\D3子目录连同其中的文件复制到考盘根目录下。提示:可在命令行界面使用FOR、IF等命令。

请问怎么做啊!!谢谢!!
QQ:77119633

27

主题

92

回帖

41

积分

新手上路

积分
41
 楼主| 发表于 2006-7-5 17:54:34 | 显示全部楼层
请问怎么做啊!!谢谢!!
QQ:77119633

676

主题

5002

回帖

601

积分

新手上路

积分
601
QQ
发表于 2006-7-5 20:02:03 | 显示全部楼层
我用JAVA写了一个程序,楼主可以参考

import java.io.*;
class FileTest
{
        /**
         *读出文件夹中的文件
         *返回文件
         */
         public static File[] readFile(String fpath)
         {
                 File f=new File(fpath);                //创建文件夹实例
                 File[] files=f.listFiles();
                 return files;
         }
         
         
         /**
         *读出文件夹中的文件
         *返回文件名
         */
         public static String[] readFileName(String fpath)
         {
                 File f=new File(fpath);
                 String[] filenames=f.list();
                 return filenames;
         }


         /**
         *判断文件是否在文件夹中存在
         *存在返回true,反之返回false
         */
         public static boolean isExist(String file, String filepath)
         {
                 boolean b=false;
                 File fpath=new File(filepath);
                 String f=file;
                 String[] filenames =fpath.list();
                 for (int i=0;i<filenames.length;i++)
                 {
                         if(f.equals(filenames))
                         {
                                 b=true;
                         }
                 }
                 return b;
         }
         
         
        /**  
     *  复制单个文件  
     *  @param  oldPath  String  原文件路径  如:c:/fqf.txt  
     *  @param  newPath  String  复制后路径  如:f:/fqf.txt  
     */  
         public static  void  copyFile(String  oldPath,  String  newPath)  
         {  
       try  {  
           int  bytesum  =  0;  
           int  byteread  =  0;  
           File  oldfile  =  new  File(oldPath);  
           if  (oldfile.exists())  
           {  //文件存在时  
               InputStream  inStream  =  new  FileInputStream(oldPath);  //读入原文件  
               FileOutputStream  fs  =  new  FileOutputStream(newPath);  
               byte[]  buffer  =  new  byte[1444];  
               int  length;  
               while  (  (byteread  =  inStream.read(buffer))  !=  -1)  
               {  
                   bytesum  +=  byteread;  //字节数  文件大小  
                   fs.write(buffer,  0,  byteread);  
               }  
               inStream.close();  
           }  
       }  
       catch  (Exception  e)  
       {  
           System.out.println("复制单个文件操作出错");  
           e.printStackTrace();  
       }  
  }
  
    public static void main(String[] args)
    {
            String filepath1="C:\\DATAS\\D1";
            String filepath2="C:\\DATAS\\D2";
            String filepath3="C:\\DATAS\\D3";
            File[] files1=readFile(filepath1);                //D1中的所有文件
            File[] files2=readFile(filepath2);                //D2中的所有文件
            String[] filenames1=readFileName(filepath1);                //D1中的文件名       
            String[] filenames2=readFileName(filepath2);                //D2中的文件名
            for(int i=0;i<files1.length;i++)               
            {
                    if (isExist(filenames1,filepath2));                //如果文件存在,不复制进D3,反之复制
                    else
                    {
                            copyFile(files1.toString(),filepath3+"\\"+filenames1);
                    }
            }
            for(int i=0;i<files2.length;i++)               
            {
                    if (isExist(filenames2,filepath1));                //如果文件存在,不复制进D3,反之复制
                    else
                    {
                            copyFile(files2.toString(),filepath3+"\\"+filenames2);
                    }
            }
    }  
}

246

主题

1万

回帖

2374

积分

新手上路

守侯冬季的雪

积分
2374
发表于 2006-7-5 20:02:26 | 显示全部楼层
小猴子好强~~~~!!!
我比你帅!

676

主题

5002

回帖

601

积分

新手上路

积分
601
QQ
发表于 2006-7-5 20:05:26 | 显示全部楼层
闲着无聊,写着玩

27

主题

92

回帖

41

积分

新手上路

积分
41
 楼主| 发表于 2006-7-5 20:10:22 | 显示全部楼层
谢谢InTheTerRan
可以用批处理命令写吗
QQ:77119633

676

主题

5002

回帖

601

积分

新手上路

积分
601
QQ
发表于 2006-7-5 20:13:34 | 显示全部楼层
晕,怎么要用这个写?
我看一下吧

27

主题

92

回帖

41

积分

新手上路

积分
41
 楼主| 发表于 2006-7-6 07:28:50 | 显示全部楼层
谢谢InTheTerRan
请问您的联系QQ,MSN
QQ:77119633

1

主题

2

回帖

1

积分

新手上路

积分
1
发表于 2006-7-6 10:18:50 | 显示全部楼层

See what I did

Create one file named test.bat and copy/paste following 3 lines into this file.

copy .\dir1\*.* .\dir3
copy .\dir2\*.* .\dir3
for %%f in (.\dir1\*.*) do for %%g in (.\dir2\*.*) do test2.bat %%~nxf %%~nxg

Create another file named test2.bat and copy/paste following 3 lines into this file.

if not %1==%2 goto end
del .\dir3\%1
:end

Make sure directories of dir1, dir2, dir3 and newly created batch files above are all under the same folder. The basic idea is to dump everything from dir1 and dir2 into dir3 and afterwards remove these duplicated files by 2 times of interating the files under dir1 and dir2.

27

主题

92

回帖

41

积分

新手上路

积分
41
 楼主| 发表于 2006-7-6 16:26:59 | 显示全部楼层
请多指点……
QQ:77119633
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Discuz! X

GMT+8, 2026-12-4 14:56 , Processed in 0.038545 second(s), 21 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表