Delete a file on ftp server with use of .net

JavaidJavaid Member Posts: 23
Hi

Can someone tell me how to delete a file on ftp server using .net.

FTPUpload (Working fine)
FileRec.SETRANGE(Path,FTPSetup."Path to Files");
FileRec.SETRANGE("Is a file",TRUE);
IF FileRec.FINDSET THEN
REPEAT
FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Server" + FTPSetup."Path on FTP Server" + FileManagement.GetFileName(FTPSetup."Path to Files" + FileRec.Name));
FTPWebRequest.Credentials := NetworkCredentials.NetworkCredential(FTPSetup."User ID",FTPSetup.Password);

FTPWebRequest.KeepAlive := TRUE;
FTPWebRequest.UseBinary := TRUE;
FTPWebRequest.UsePassive := TRUE;
FTPWebRequest.Method := 'STOR';

FileStream := FileToUpload.OpenRead(FTPSetup."Path to Files" + FileRec.Name);
Stream := FTPWebRequest.GetRequestStream;
FileStream.CopyTo(Stream);

FileStream.Close;
Stream.Close;

FTPWebResponse := FTPWebRequest.GetResponse;
FTPWebResponse.Close;

FileName := COPYSTR(FileRec.Name,1,STRLEN(FileRec.Name)-4);

COPY(FTPSetup."Path to Files" + FileRec.Name,STRSUBSTNO(FTPSetup."Path to Archive Folder" + FileName + '_' + DELCHR(FORMAT(TODAY),'=','-') + '.csv'));
ERASE(FTPSetup."Path to Files" + FileRec.Name);
UNTIL FileRec.NEXT = 0;

FTPDownloadToList (Working fine)
FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Server" + FTPSetup."Path on FTP Server");
FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."User ID",FTPSetup.Password);

FTPWebRequest.KeepAlive := TRUE;
FTPWebRequest.UseBinary := TRUE;
FTPWebRequest.UsePassive := TRUE;
FTPWebRequest.Method := 'NLST';

FTPWebResponse := FTPWebRequest.GetResponse();
StreamReader := StreamReader.StreamReader(FTPWebResponse.GetResponseStream());
List := List.List();
FileName := StreamReader.ReadLine();

WHILE FileName <> '' DO BEGIN
List.Add(FileName);
FileName := StreamReader.ReadLine();
END;

FOR i:=1 TO List.Count DO BEGIN
IF FORMAT(List.Item(i-1)) <> 'backup' THEN BEGIN
FTPDownload(FORMAT(List.Item(i-1)));
END;
END;

FTPDownload (Working fine)
FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Server" + FTPSetup."Path on FTP Server" + FileToDownload);
FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."User ID",FTPSetup.Password);

FTPWebRequest.UseBinary := TRUE;
FTPWebRequest.UsePassive := TRUE;
FTPWebRequest.KeepAlive := TRUE;
FTPWebRequest.Method := 'RETR';

FTPWebResponse := FTPWebRequest.GetResponse();
ResponseStream := FTPWebResponse.GetResponseStream();

TempBlob.Blob.CREATEOUTSTREAM(OutStream);
COPYSTREAM(OutStream,ResponseStream);

//FileName := FTPSetup."FTP Server" + FTPSetup."Path on FTP Server" + FileToDownload;
FileName := FTPSetup."Path to Files" + FileToDownload;

//COPY(FTPSetup."FTP Server" + FTPSetup."Path on FTP Server" + FileToDownload,'\\sin-dk-file03\Navision\Integration\POS_Import\'+ FileToDownload);

TempBlob.Blob.EXPORT(FileName);


FTP DeleteDownloadFile Not working
??????
FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Server" + FTPSetup."Path on FTP Server" + FileToDownload);
FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."User ID",FTPSetup.Password);

//FTPWebRequest.Proxy := Null;
FTPWebRequest.UseBinary := FALSE;
FTPWebRequest.UsePassive := TRUE;
FTPWebRequest.KeepAlive := FALSE;
FTPWebRequest.Method := WebRequestMethods.Ftp.DeleteFile;

FTPWebResponse := FTPWebRequest.GetResponse();
ResponseStream := FTPWebResponse.GetResponseStream();








Answers

  • ResolusResolus Member Posts: 40
    Did you debug and check if your path is correct? Not too many /?
    In my opinion it's sometimes easier to read/debug by separating it into a variable.

    FileToRemove := FTPSetup."FTP Server" + FTPSetup."Path on FTP Server" + FileToDownload;
    FTPWebRequest := FTPWebRequest.Create(FileToRemove);
Sign In or Register to comment.