当前位置:首页 » 编程软件 » 文件遍历编程

文件遍历编程

发布时间: 2024-10-22 20:37:47

❶ C#遍历所有文件和子目录

using System;

using System IO;

class ListAllFilesDemo

{

//遍历所有文件和文件夹 查找指定文件 并返回该文件的完整路径

public static void ListFiles(FileSystemInfo info)

{

if (!info Exists) return;

DirectoryInfo dir = info as DirectoryInfo;

//不是目录

if (dir == null) return;

FileSystemInfo[] files = dir GetFileSystemInfos();

for (int i = ; i < files Length; i++)

{

FileInfo file = files[i] as FileInfo;

//是文件

if (file != null)

{

if (file Name Contains( config inc php ))

{

Console WriteLine(file FullName);

Console ReadLine();

}

}

//对于子目录 进行递归调用

else

ListFiles(files[i]);

}

}

public static void Main()

{

Console Write( 请输入要查询的目录: );

string dir = Console ReadLine();

try

{

ListFiles(new DirectoryInfo(dir));

Console ReadLine();

}

catch (IOException e)

{

Console WriteLine(e Message);

}

}

}

网页形式

string thePath = / /Upload/Star/ ;

thePath = Server MapPath(thePath);//得到文件绝对路径

System IO DirectoryInfo d = new System IO DirectoryInfo(thePath);

System IO DirectoryInfo[] ds = d GetDirectories( * * System IO SearchOption TopDirectoryOnly);

foreach (System IO DirectoryInfo var in ds) {

//路径全称

Response Write(var FullName + <br/> );//遍历文件夹下面的文件夹

//仅文件名称

Response Write(var Name + <br/> );

lishixin/Article/program/net/201311/12136

❷ 如何用vba遍历文件夹里面的子文件并且复制指定数据形成一张新的表格,ps:子文件的数据格式一直

尝试用下边代码试试:

Sub OpenAndClose()

Dim MyFile As String

Dim s As String

Dim count As Integer

MyFile = Dir(文件夹目录 & "*.xlsx")

'读入文件夹中的第一个.xlsx文件

count = count + 1 '记录文件的个数

s = s & count & "、" & MyFile

Do While MyFile <> ""

MyFile = Dir '第二次读入的时候不用写参数

If MyFile = "" Then

Exit Do '当MyFile为空的时候就说明已经遍历完了,这时退出Do,否则还要运行一遍

End If

count = count + 1

If count Mod 2 <> 1 Then

s = s & vbTab & count & "、" & MyFile

Else

s = s & vbCrLf & count & "、" & MyFile

End If

Loop

Debug.Print s

End Sub


另外,可以考虑用python试试

❸ bash脚本遍历目录指定后缀的文件,并执行操作

可以使用ls或者find来完成对某个文件夹下所有文件的遍历
比如使用ls
可以简单地使用一个通配符来完成
ls 某个目录/*
也可以使用find来完成
比如
find 某个目录
自然的也可以写一个shell脚本来进行遍历
首先进行一个要遍历的文件夹
然后循环查看每个文件
如果该文件是一个文件夹的话则进入该文件夹做和上面相同的事件
这样就可以该整个文件夹内的所有文件进行遍历了
一个简单的代码如下
#!/bin/bash

function show()
{
cd $1

for i in `ls`
do
if [ -d "$i" ]
then
show "$i"
else
echo "$i"
fi
done

cd ..
}

show $1

exit 0
该程序不能遍历以.开头的隐藏文件
可以使用ls -a来进行遍历隐藏文件
遍历时需要注意.和..这两个特殊文件

下面是一个简单的代码
#!/bin/bash

function show()
{
cd $1

for i in `ls -a`
do
if [ "$i" == "." ] || [ "$i" == ".." ]
then
continue;
fi

if [ -d "$i" ]
then
show "$i"
else
echo "$i"
fi
done

cd ..
}

show $1

exit 0

热点内容
kvm编译原理 发布:2024-10-22 22:57:41 浏览:440
qq密码情侣改成什么最好 发布:2024-10-22 22:55:48 浏览:808
linux安装cuda 发布:2024-10-22 22:32:07 浏览:486
编译和链接的键 发布:2024-10-22 22:21:01 浏览:114
java数组的实现 发布:2024-10-22 22:18:15 浏览:329
python定义字符串数组 发布:2024-10-22 22:14:26 浏览:604
eclipse编译项目路径 发布:2024-10-22 22:08:24 浏览:826
如何看懂联想电脑配置 发布:2024-10-22 22:07:15 浏览:513
贝贝韵脚本 发布:2024-10-22 22:06:29 浏览:861
存储器启动配置 发布:2024-10-22 22:05:18 浏览:809