当前位置:首页 » 文件传输 » c语言FTP获取目录下列表
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言FTP获取目录下列表

发布时间: 2022-12-29 04:39:46

① 怎么获得FTP服务器上目录下的所有文件名和子目录

不要使用tidftp,长时间搜索会出现死锁现象,使用WinInet单元
procere TFtpScan.Execute;
var
FFTPHandle: HINTERNET;
FInetHandle: HINTERNET;
Enum: HINTERNET;
FFindFileData: WIN32_FIND_DATA;
R: Boolean;
FFileName, DataStr: string;
TempErrorCode: Cardinal;
begin

② FTP 怎么查看本地的文件列表

一个简单的问题,ftp到远程服务器之后需要上传一个文件,但是我忘记了本地需要上传的文件名,因此需要查看本地目录。
解决方法:
一、cd到本地目录
二、ls命令查看本地目录的内容。

③ 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 = 3; i < fountainhead.Length; i++)
{
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;
}

④ 在FTP命令当中查看本地文件列表命令是

查看本地文件列表命令是:ls

其它常用的FTP命令及含义:

1、dir:显示服务器目录和文件列表

2、cd:进入服务器指定的目录(dir命令可以使用通配符“”和“?”,比如,显示当前目录中所有扩展名为jpg的文件,可使用命令 dir .jpg。)

3、put:上传指定文件put filename [newname]

4、、send:上传指定文件send filename [newname]

(filename为上传的本地文件名,newname为上传至FTP服务器上时使用的名字,如果不指定newname,文件将以原名上传。)

(4)c语言FTP获取目录下列表扩展阅读

ftp命令行格式及开关含义:

ftp [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:windowsize] [computer]

-v - 禁止显示远程服务器相应信息

-n - 禁止自动登录

-i - 多文件传输过程中关闭交互提示

-d - 启用调试,显示所有客户端与服务器端传递的命令

-g - 禁用文件名通配符,允许在本地文件和路径名中使用

-s:filename - 指定包含 FTP 命令的文本文件;命令在FTP启动后自动运行。此参数中没有空格。可替代重定向符(>)使用。

-a - 在绑字数据连接时使用所有本地接口

-w:windowsize - 覆盖默认的传输缓冲区大小 65535。

computer - 指定远程电脑计算机名或IP地址。此参数必须放到最后。

⑤ C语言怎么读取某一文件夹下的所有文件夹和文件

读取的代码方式如下:

int main()

{

long file;

struct _finddata_t find;

_chdir("d:\");

if((file=_findfirst("*.*", &find))==-1L)

{

printf("空白! ");

exit(0);

}

printf("%s ", find.name);

while(_findnext(file, &find)==0)

{

printf("%s ", find.name);

}

_findclose(file);

return 0;

}

⑥ 如何用C语言获取目录下的文件和目录列表

#include <stdio.h>
#include <stdlib.h>

void main()
{
system("DIR /D C:\\ /s /B > a.log");
}

C:\下的所有文件夹,子文件夹里所有文件,转向到 文本文件 a.log 里。
格式:
C:\aaa\bbb\ccc\...

只要文件夹命令:
dir /d c: /B /ad
只要文件夹命令,含子文件夹:
dir /d c: /B /ad /s

⑦ C语言如何连接FTP,连接成功后遍历FTP下目录,包括子目录,要源代码,而且是成功案例,自以为是的靠边站!

用libcurl实现,具体代码看
http://curl.haxx.se/libcurl/c/ftpget.html