linuxc遍歷文件夾
㈠ 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
這樣應該可以吧,試試看