當前位置:首頁 » 文件傳輸 » 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