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

文件遍歷編程

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

熱點內容
小米4清除緩存 發布:2025-07-16 03:03:17 瀏覽:562
如何緩解壓力英語作文 發布:2025-07-16 03:03:15 瀏覽:14
手機視頻怎麼緩存 發布:2025-07-16 02:59:05 瀏覽:932
安卓手機設備在哪裡找 發布:2025-07-16 02:49:28 瀏覽:356
php建立數組 發布:2025-07-16 02:34:30 瀏覽:284
oracle存儲過程同步 發布:2025-07-16 02:29:18 瀏覽:941
歐諾s買哪個配置的好 發布:2025-07-16 02:26:22 瀏覽:559
熱點可以建立ftp嗎 發布:2025-07-16 02:26:21 瀏覽:304
如何選擇最佳配置 發布:2025-07-16 01:56:44 瀏覽:604
mad加密 發布:2025-07-16 01:52:12 瀏覽:425