#
造
5
个
目录,每个目录下,造
3
个
文件和两个子目录如下:
cd
$home/tmp
for
i
in
d1
d2
d3
d4
d5
do
mkdir
-p
$i
touch
$i/1.txt
$i/2.txt
$i/3.txt
mkdir
-p
$i/tmp1
$i/tmp2
done
#
检验测试环境:
$
ls
-lr
d1
total
0
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
2.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
3.txt
drwxr-sr-x
2
wenlee
comm
256
dec
22
10:35
tmp1/
drwxr-sr-x
2
wenlee
comm
256
dec
22
10:35
tmp2/
#
利用下列脚本来实现你要做的:
cd
$home/tmp
for
i
in
*/1.txt
do
echo
"found
$i,
save
$i
and
remove
everything
else
under
$(dirname
$i)/"
save_this_file=$(basename
$i)
curr_dir=$(dirname
$i)
#
把这个1.txt暂时存到/tmp里面去,为了避免已经有同样的档案名称在/tmp,加上$$
(i.e.
pid)
mv
$i
/tmp/${save_this_file}.$$
rm
-rf
$curr_dir
mkdir
-p
$curr_dir
mv
/tmp/${save_this_file}.$$
$curr_dir
done
#
屏幕执行输出如下:
found
d1/1.txt,
save
d1/1.txt
and
remove
everything
else
under
d1/
found
d2/1.txt,
save
d2/1.txt
and
remove
everything
else
under
d2/
found
d3/1.txt,
save
d3/1.txt
and
remove
everything
else
under
d3/
found
d4/1.txt,
save
d4/1.txt
and
remove
everything
else
under
d4/
found
d5/1.txt,
save
d5/1.txt
and
remove
everything
else
under
d5/
#
复验实验环境:
$
ls
-l
d?/*
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d1/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d2/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d3/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d4/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d5/1.txt
ok?
thanks!
为了避免目录列举消耗时间过长,请指定一个目录来模拟,命令行参数:代表路径的字符串.如果认可代码,请加分50,谢谢
----
import javax.swing.*
import javax.swing.tree.*
import java.awt.*
import java.io.*
final public class FileTree extends JFrame {
public FileTree(File dir) throws HeadlessException {
super("File Tree")
JTree tree
add(new JScrollPane(tree =new JTree(buildTreeModel(dir))))
tree.setCellRenderer(new FileTreeRenderer())
setSize(400,600)
setVisible(true)
}
private TreeModel buildTreeModel(File dir){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(dir)
walkthrough(dir,root)
return new DefaultTreeModel(root)
}
private static void walkthrough(File f,DefaultMutableTreeNode node){
for (File fle : f.listFiles()) {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(fle)
node.add(n)
if (fle.isDirectory()){
walkthrough(fle, n)
}
}
}
private class FileTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JLabel cmp = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus)
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode)value
Object obj = n.getUserObject()
if (obj instanceof File) {
File f = (File)obj
cmp.setText(f.getName())
cmp.setForeground(f.isDirectory()?Color.BLUE:Color.BLACK)
}
}
return cmp
}
}
public static void main(String[] args) {
new FileTree(new File(args[0]))
}
}
#include <Windows.h>#include <stdio.h>
#include <stdlib.h>
typedef void (*Callback)(const char *path, const WIN32_FIND_DATA *info, void *arg)
void PrintPath(const char *path, const WIN32_FIND_DATA *info, void *arg)
{
printf("%s\n", info->cFileName)
}
void RecursiveDirectory(const char *path, Callback callback, void *arg)
{
WIN32_FIND_DATA fd
HANDLE hd = INVALID_HANDLE_VALUE
hd = FindFirstFile(path, &fd)
if(hd == INVALID_HANDLE_VALUE)
{
return
}
do
{
callback(path, &fd, arg)
if((fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
{
char subpath[MAX_PATH] = {'\0'}
sprintf(subpath, "%s\\%s\\*", path, fd.cFileName)
RecursiveDirectory(subpath, callback, arg)
}
} while(FindNextFile(hd, &fd))
FindClose(hd)
}
int main(int argc, char **argv)
{
if(argc > 1)
{
RecursiveDirectory(argv[1], PrintPath, NULL)
}
return 0
}
很久之前的一个框,有用就看吧。
*nix的*dir系列函数比win的Find*系列函数好用多了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)