$ cat a1.sh
#!/bin/bash
for file in /home/user/tmp/*
do
echo $file
done
2)假设目录/home/user/tmp/下面的所有的文件和(子)目录如下:
$ ls
1.txt 2.txt a1.sh a.sh b.sh email_back m1.doc tmp
3)运行脚本:
$ a1.sh (或者./a1.sh)
/home/user/tmp/1.txt
/home/user/tmp/2.txt
/home/user/tmp/a1.sh
/home/user/tmp/a.sh
/home/user/tmp/b.sh
/home/user/tmp/email_back
/home/user/tmp/m1.doc
/home/user/tmp/tmp
4)脚本a1.sh的作用只是显示文件和子目录的列表,要显示文件的内容,脚本继续改造,内容如下,看脚本a2.sh:
$ cat a2.sh
#!/bin/bash
for file in /home/shiqingd/tmp/*
do
echo $file
if [ -f $file ]then
cat $file
fi
done
脚本a2.sh可以达到目的。
Shell获取某目录下所有文件夹的名称, 已知目录D:/temp,获取temp下所有文件夹的名称并输出,Shell怎么写?方法有三,如下:
#!/bin/bash
#方法一
dir=$(ls -l D:/temp/ |awk '/^d/ {print $NF}')
for i in $dir
do
echo $i
done
#######
#方法二
for dir in $(ls D:/tmep/)
do
[ -d $dir ] && echo $dir
done
##方法三
ls -l D:/temp/ |awk '/^d/ {print $NF}'
## 其实同方法一,直接就可以显示不用for循环
php 获取目录下的所有文件夹名称大致思路:
<?php
function traverseDir($dir){
if($dir_handle = @opendir($dir)){
while($filename = readdir($dir_handle)){
if($filename != "." &&$filename != ".."){
$subFile = $dir.DIRECTORY_SEPARATOR.$filename要将源目录及子文件相连
if(is_dir($subFile)){ 若子文件是个目录
echo $filename.'<br>'输出该目录名称
traverseDir($subFile)递归找出下级目录名称
}
}
}
closedir($dir_handle)
}
}
$dirNames = traverseDir("d:/dos")测试某目录
?>
VBS获取当前目录下所有文件夹名字呵呵,原来是团友啊我写了一个!Set ws=WScript.CreateObject("wscript.shell")
w=ws.CurrentDirectory
Set fso=WScript.CreateObject("scripting.filesystemobject")
Set fs=fso.GetFolder(w)
Set f=fs.SubFolders
For Each uu In f
t=t &uu.Path &vbcrlf
Next
MsgBox t
使用VB获得目录下所有文件夹名称的问题用FSO对象模型不是很好么,这种代码看的好痛苦哇!
C# 获取Ftp某个目录下的所有文件(不要文件夹)我在之前做过一个FTP的客户端工具。
drw 文件夹
-rw 文件(有扩展名或无扩展名)
我是根据服务端返回的报文进行分析获取的列表。
给你一些代码片段:
/ <summary>
/ 获取指定目录下的文件和文件夹。
/ </summary>
/ <param name=path>要获取的目录</param>
/ <param name=WRMethods>要发送到FTP服务器的密令。</param>
/ <returns></returns>
public string[] GetFileList(string path, string WRMethods)从ftp服务器上获得文件列表
{
WebResponse response
string[] downloadFiles
int conut = 4
StringBuilder result = new StringBuilder()
Connect(path)
if (FTPVariable.IsUseProxy_ftp)
{
reqFTP.Proxy = FtpProxy.GetFtpSelectProxy(FTPVariable.FtpCommand_transferProxyName)
}
reqFTP.ReadWriteTimeout = 12000
如果不应销毁到服务器的连接,则为 true;否则为 false。默认值为 true。
reqFTP.Method = WRMethods
try
{
response = (FtpWebResponse)reqFTP.GetResponse()
goto Ftp_lbl_03
}
catch (WebException webex)
{
GetReply(webex.Message)
if (ReplyCode == 530) 未登录。
{
goto Ftp_lbl_04
}
else if (ReplyCode == 550)
{
goto Ftp_lbl_04
}
else
{
FtpManage.SetLog("获取列表超时,等候1秒后重试!")
goto Ftp_lbl_01
}
}
Ftp_lbl_01:
try
{
FtpManage.SetLog("正在连接服务器 " + FtpRemoteHost)
response = GetRequest(path, WRMethods)
}
catch (WebException)
{
FtpManage.SetLog("获取列表超时,等候1秒后重试!")
downloadFiles = null
System.Threading.Thread.Sleep(1000)
if (conut == 0)
{
goto Ftp_lbl_02
}
conut--
goto Ftp_lbl_01
}
catch (Exception ex)
{
MSG.Show(ex.Message, Global.GetRS["msgTilteError"], MessageBoxButton.OK, MsgIco.Error)
FtpManage.SetLog("命令执行失败,原因:" + ex.Message)
downloadFiles = null
return downloadFiles
}
Ftp_lbl_03:
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default)中文文件名
string line = reader.ReadLine()
while (line != null)
{
result.Append(line)
result.Append("\n")
line = reader.ReadLine()
}
if (result.Length == 0)
{
return null
}
to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1)
reader.Close()
response.Close()
FtpManage.SetLog("命令已成功执行")
return result.ToString().Split('\n')
Ftp_lbl_04:
FtpManage.SetLog(ReplyInfo)
return null
Ftp_lbl_02:
FtpManage.SetLog("550 获取列表失败,无法连接远程服务器!")
FtpManage.ftpmanage.IsRefurbish = true
return null
}
/ <summary>
/ 获取指定目录下的文件和文件夹。
/ </summary>
/ <param name=path>要获取的目录</param>
/ <returns></returns>
public string[] GetFileList(string path)从ftp服务器上获得文件列表
{
return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectory)
}
/ <summary>
/ 获取指定目录下的文件和文件夹。
/ </summary>
/ <returns></returns>
public string[] GetFileList()从ftp服务器上获得文件列表
{
return GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/", WebRequestMethods.Ftp.ListDirectory)
}
/ <summary>
/ 获取目录和文件名,返回目录表。
/ </summary>
/ <param name=path>要获取的目录</param>
/ <returns></returns>
public string[] GetCatalog_FileList(string path)
{
string[] fountainhead = GetFileList(FTPVariable.FtpURLhead + FtpRemoteHost + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails)
string[] Catalog = null
if (fountainhead == null)
{
return null
}
Catalog = new string[fountainhead.Length]
for (int i = 3i <fountainhead.Lengthi++)
{
Catalog[i - 3] += fountainhead[i].Substring(55, fountainhead[i].Length - 55) + "&"FileName
Catalog[i - 3] += fountainhead[i].Substring(30, 12) + "&"FileSize
Catalog[i - 3] += fountainhead[i].Substring(42, 13) + "&"AmendDate
Catalog[i - 3] += fountainhead[i].Substring(0, 3) + "&"
}
return Catalog
}
请问ASP中如何获取某个目录中所有文件夹的名称?fso.GetFolder(path)
shell编程如何获取指定目录下所有文件的名称,例:目录:/root/dira/下的 001.DAT…009.DAT 9个文件名称?你刚才不是问过了么?
vb 如何读取某目录下所有文件及子文件夹查找某目录下所有 文件 及 子文件夹
试一试不用 FileSystemObject 对象,只用基本控件的代码。
'例子需控件:Command1,List1,List2,File1,Dir1,都采用默认属性。
'例如,查找 C:\ ,带 '** 的语可修改
Dim ctFind As Boolean
Private Sub Form_Load()
Me.Caption = "查找所有文件及文件夹"
Command1.Caption = "查找"
List2.Visible = False: File1.Visible = False: Dir1.Visible = False
Label1.Caption = "就绪"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Form_Resize()
Dim W As Long
On Error Resume Next
W = 720
List1.Move 0, 0, Me.ScaleWidth - W - 120, Me.ScaleHeight - 300
Command1.Move Me.ScaleWidth - W - 60, 300, W
Label1.Move 90, Me.ScaleHeight - 255, Screen.Width, 255
End Sub
Private Sub Command1_Click()
ctFind = Not ctFind
If ctFind Then
Command1.Caption = "取消"
Call FindDirFile("C:") '**查找 C:\ 下的所有文件和目录,或 C:\Windows 等
Command1.Caption = "查找"
Else
Command1.Caption = "查找"
End If
End Sub
Private Sub FindDirFile(ByVal nPath As String)
Dim I As Long, nDir As String, Ci As Long
ctFind = True
List1.Clear: List2.Clear
If Right(nPath, 1) <>"\" Then nPath = nPath &"\"
List1.AddItem "查找 " &nPath: List2.AddItem nPath
File1.Pattern = "*"
File1.System = True: File1.Hidden = True: File1.ReadOnly = True
On Error GoTo Cuo
Dir1.Path = nPath
On Error GoTo 0
Do
If List2.ListCount = 0 Then Exit Do
nPath = List2.List(0)
List2.RemoveItem 0
Dir1.Path = nPath
For I = 0 To Dir1.ListCount - 1
GoSub ShowGe
nDir = Dir1.List(I)
If Right(nDir, 1) <>"\" Then nDir = nDir &"\"
List1.AddItem "■" &nDir
List2.AddItem nDir
Next
File1.Path = nPath
For I = 0 To File1.ListCount - 1
GoSub ShowGe
List1.AddItem " " &nPath &File1.List(I)
Next
Loop
Label1.Caption = "查找完毕,共找到 " &List1.ListCount &" 个条目"
ctFind = False
Exit Sub
Cuo:
List1.AddItem "起始目录不存在:" &nPath
ctFind = False
Exit Sub
ShowGe:
Ci = Ci + 1
If Ci <99 Then Return
Ci = 0
Label1.Caption = "已找到 " &List1.ListCount &" 个:" &nPath
DoEvents
If ctFind Then Return
End Sub
linux shell脚本怎么获取目录下所有txt文件名称执行如下三条命令即可:
(1)、$script myresultfile
(2)、$ls -al *.txt
(3)、$exit
此时,该目录下的所有 txt 文件名称就会以长格式保存在 myresultfile 文件中了。
然后你再使用 SHELL 编程的功能把那些无用的列去掉即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)