难题,难题,难题,请帮忙解答!!
发表于 2006-7-5 09:12 · 作者 conroe · 400 次查看
conroe 发表于 2006-7-5 09:12
C:\DATAS\D1和C:\DATAS\D2下各有一批可见文件。将这2个子目录下的文件复制到新建的子目录C:\D3中,但C:\DATAS\D1和C:\DATAS\D2中存在相同文件名的那些文件不复制,例如,两个子目录下都有文件ABCD.EFG,此文件就不应复制到C:\D3中。最后把C:\D3子目录连同其中的文件复制到考盘根目录下。提示:可在命令行界面使用FOR、IF等命令。
请问怎么做啊!!谢谢!!
conroe 发表于 2006-7-5 17:54
请问怎么做啊!!谢谢!!
InTheTerRan 发表于 2006-7-5 20:02
我用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);
}
}
}
}
小马过河 发表于 2006-7-5 20:02
小猴子好强~~~~!!!
InTheTerRan 发表于 2006-7-5 20:05
闲着无聊,写着玩
conroe 发表于 2006-7-5 20:10
谢谢InTheTerRan
可以用批处理命令写吗
InTheTerRan 发表于 2006-7-5 20:13
晕,怎么要用这个写?
我看一下吧
conroe 发表于 2006-7-6 07:28
谢谢InTheTerRan
请问您的联系QQ,MSN
youkaihou 发表于 2006-7-6 10:18
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.
conroe 发表于 2006-7-6 16:26
请多指点……
阿达 发表于 2006-7-6 16:28
………………………………无语了= =
编程对我来说是天书