當前位置:首頁 » 文件管理 » linuxc遍歷文件夾

linuxc遍歷文件夾

發布時間: 2025-05-05 04:07:42

linux C 中的文件夾遍歷dirent d_type表明該文件的類型 跪求~~~

enum

{

DT_UNKNOWN = 0,

# define DT_UNKNOWN DT_UNKNOWN

DT_FIFO = 1,

# define DT_FIFO DT_FIFO

DT_CHR = 2,

# define DT_CHR DT_CHR

DT_DIR = 4,

# define DT_DIR DT_DIR

DT_BLK = 6,

# define DT_BLK DT_BLK

DT_REG = 8,

# define DT_REG DT_REG

DT_LNK = 10,

# define DT_LNK DT_LNK

DT_SOCK = 12,

# define DT_SOCK DT_SOCK

DT_WHT = 14

# define DT_WHT DT_WHT

};

這是d_type的枚舉類型..........每個值表示一個類型..........4是目錄,0是未知,1是管道,2是字元設備,8表示文件...............6是塊設備..........其他的都如字面表示..........很容易區分.........

㈡ linux平台:使用lua語言遍歷某一文件夾下所有文件

你可以參考如下實例代碼:

functiongetFile(file_name)
localf=assert(io.open(file_name,'r'))
localstring=f:read("*all")
f:close()
returnstring
endfunctionwriteFile(file_name,string)
localf=assert(io.open(file_name,'w'))
f:write(string)
f:close()
end--從命令行獲取參數,如果有參數則遍歷指定目錄,沒有參數遍歷當前目錄ifarg[1]~=nilthen
cmd="ls"..arg[1]
else
cmd="ls"endprint("cmd",cmd)
--io.popen返回的是一個FILE,跟c裡面的popen一樣locals=io.popen(cmd)
localfileLists=s:read("*all")
print(fileLists)
whiletruedo--從文件列表裡一行一行的獲取文件名_,end_pos,line=string.find(fileLists,"([^ ]+.txt)",start_pos)
ifnotend_posthenbreakend--print("wld",line)localstr=getFile(line)
--把每一行的末尾1,替換為0,localnew=string.gsub(str,"1, ","0, ");
--替換後的字元串寫入到文件。以前的內容會清空writeFile(line,new)
start_pos=end_pos+1end

㈢ linux shell 遍歷文件夾 並將結果保存 到變數

#!/bin/bash
(($#<1))&&echo"paramiszero!"&&exit1
[!-d$1]&&echo"$1notpath"&&exit1
dir=$1
dir_p="$dirDirectory:"
cd$dir
dir=`pwd`
foriin`ls$dir`
do
if[-d$i];then
/tmp/sh/dir_file$i#我的腳本文件在/tmp/sh中,需要改一下這里
else
dir_p="$dir_pFile$i"
fi
done
cd..
echo$dir_p


實驗結果:

[root@localhost sh]# ./dir_file /tmp/python/

python_2 Directory : File 1.log File 2.log

python_3 Directory : File 3.log

/tmp/python/ Directory : File p File t.py File y.py


這樣應該可以吧,試試看

熱點內容
安卓手機怎麼敲兩下就出來 發布:2025-05-05 08:19:46 瀏覽:137
手機怎麼zip解壓 發布:2025-05-05 08:12:11 瀏覽:350
php聊天開源 發布:2025-05-05 08:09:15 瀏覽:619
泰坦之旅安卓哪個不閃退 發布:2025-05-05 08:08:39 瀏覽:955
9愛源碼網 發布:2025-05-05 08:04:56 瀏覽:926
戰艦少女腳本加速 發布:2025-05-05 08:02:48 瀏覽:985
新款crv哪個配置性價比 發布:2025-05-05 08:02:12 瀏覽:959
如何黑游戲伺服器 發布:2025-05-05 08:02:12 瀏覽:999
obs流媒體伺服器直播點播搭建 發布:2025-05-05 07:57:13 瀏覽:398
大眾波羅08款mp3的密碼是多少 發布:2025-05-05 07:44:09 瀏覽:106