Announcement

Collapse
No announcement yet.

Folder does exist in ftp

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Folder does exist in ftp

    Hello to everyone,
    I am using the ftp plugin to upload some files to my host. The files are uploaded if directories depending of the upload date.
    The issue is here: I create this structure to upload the files.
    rootpath/year/month/date
    When the structure is generated for the first time everything works well, but than it gives and error because the directories exist and can't be created twice.

    The code that I use is this
    Code:
    FTP.MakeDir(System.GetDate(DATE_FMT_YEAR));
    err = Application.GetLastError();
    if err ~= FTP.OK then
                Dialog.Message("Error", _tblErrorMessages[err]);
                Application.ExitScript();
           else
    			FTP.ChangeDir(System.GetDate(DATE_FMT_YEAR));
    			err = Application.GetLastError();
    			if err ~= FTP.OK then
    				    Dialog.Message("Error", _tblErrorMessages[err]);
    				    Application.ExitScript();
    			    else
    					FTP.MakeDir(System.GetDate(DATE_FMT_MONTH));
    					err = Application.GetLastError();
    					if err ~= FTP.OK then
    						    Dialog.Message("Error", _tblErrorMessages[err]);
    						    Application.ExitScript();					
    					    else
    							FTP.ChangeDir(System.GetDate(DATE_FMT_MONTH));
    							err = Application.GetLastError();
    							if err ~= FTP.OK then
    								    Dialog.Message("Error", _tblErrorMessages[err]);
    								    Application.ExitScript();							
    							   else
    									FTP.MakeDir(System.GetDate(DATE_FMT_DAY));
    									err = Application.GetLastError();
    									if err ~= FTP.OK then
    											   Dialog.Message("Error", _tblErrorMessages[err]);
    										       Application.ExitScript();
    									    else
    											   FTP.ChangeDir(System.GetDate(DATE_FMT_DAY));
    									           err = Application.GetLastError();
    									end						   
    							end					    
    					end		    
    			end
    end
    How can I check if a directory exist before I create a new one?!

  • #2
    I dont know if there is a specific does exist function but creating one as you say does give an error so you know it does exist.

    Comment


    • #3
      Originally posted by claus707 View Post
      How can I check if a directory exist before I create a new one?!
      Originally posted by Shrek View Post
      I dont know if there is a specific does exist function but creating one as you say does give an error so you know it does exist.
      Reading the FTP plugin help you can easily find that the FTP.List function allows you to get a complete list of files and folders given an appropriate path.

      Then you can know if a specified folder does exist and, if not, create it, otherwise proceeding with the file transfer without creating the folder.
      We are slowly invading your planet to teach lazy humans to read the user manual.
      But don't be scared: we are here to help.

      Comment


      • #4
        Thank you for the reply Cybergraph!
        Maybe with FTP.List it can be an optimized solution, but I made a trick with FTP.ChangeDir(). In the beginning I test for an error if I can go to the directory, if I get an error it means that the directory isn't created so I create the directories.
        The code is here and for the moment it works perfectly.
        Code:
        folder_path = System.GetDate(DATE_FMT_YEAR) .. "/" .. System.GetDate(DATE_FMT_MONTH) .. "/" .. System.GetDate(DATE_FMT_DAY);
        
        FTP.ChangeDir(folder_path);
        err = Application.GetLastError();
        if err ~= FTP.OK then
        			FTP.MakeDir(System.GetDate(DATE_FMT_YEAR));
        			err = Application.GetLastError();
        			if err ~= FTP.OK then
        			            Dialog.Message("Error", _tblErrorMessages[err]);
        			            Application.ExitScript();
        			       else
        						FTP.ChangeDir(System.GetDate(DATE_FMT_YEAR));
        						err = Application.GetLastError();
        						if err ~= FTP.OK then
        							    Dialog.Message("Error", _tblErrorMessages[err]);
        							    Application.ExitScript();
        						    else
        								FTP.MakeDir(System.GetDate(DATE_FMT_MONTH));
        								err = Application.GetLastError();
        								if err ~= FTP.OK then
        									    Dialog.Message("Error", _tblErrorMessages[err]);
        									    Application.ExitScript();					
        								    else
        										FTP.ChangeDir(System.GetDate(DATE_FMT_MONTH));
        										err = Application.GetLastError();
        										if err ~= FTP.OK then
        											    Dialog.Message("Error", _tblErrorMessages[err]);
        											    Application.ExitScript();							
        										   else
        												FTP.MakeDir(System.GetDate(DATE_FMT_DAY));
        												err = Application.GetLastError();
        												if err ~= FTP.OK then
        														   Dialog.Message("Error", _tblErrorMessages[err]);
        													       Application.ExitScript();
        												    else
        														   FTP.ChangeDir(System.GetDate(DATE_FMT_DAY));
        												           err = Application.GetLastError();
        												end						   
        										end					    
        								end		    
        						end
        			end
        end

        Comment

        Working...
        X