當前位置:首頁 » 編程軟體 » 文件遍歷編程

文件遍歷編程

發布時間: 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

熱點內容
怎樣在我的世界重建伺服器 發布:2024-10-22 23:37:10 瀏覽:355
安卓手機如何使用手寫功能 發布:2024-10-22 23:19:16 瀏覽:351
手機壁紙上傳 發布:2024-10-22 23:13:51 瀏覽:772
oracle資料庫遷移方案 發布:2024-10-22 23:10:53 瀏覽:384
七牛雲存儲java上傳 發布:2024-10-22 23:10:49 瀏覽:236
kvm編譯原理 發布:2024-10-22 22:57:41 瀏覽:441
qq密碼情侶改成什麼最好 發布:2024-10-22 22:55:48 瀏覽:809
linux安裝cuda 發布:2024-10-22 22:32:07 瀏覽:487
編譯和鏈接的鍵 發布:2024-10-22 22:21:01 瀏覽:115
java數組的實現 發布:2024-10-22 22:18:15 瀏覽:331