Hi!

I want to delete an empty directory from FTP Server, however my code works only for files. Let's say the root directory in my FTP Server is called as Documents . I want to delete a directory Director . Then I execute client.deleteFile("\\" + selectedPath); , where selectedPath is equal to Documents\Correspondence\Finance\Director .

Why the directory Director cannot be deleted? Thanks!

private void deleteFolderFromRemoteMachine(String selectedPath) {
        
        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {
            client.connect(this.url);
            client.login(this.login,this.password);
            client.setFileType(FTP.BINARY_FILE_TYPE); // Tried also ASCII_FILE_TYPE

            client.deleteFile("\\" + selectedPath);
       
            client.logout();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Cannot connect to the FTP server!", "Warning", JOptionPane.WARNING_MESSAGE);
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Recommended Answers

All 2 Replies

If you are working with File objects the delete() method should work for empty folders.

Edit: Missed that you used FTPClient, however the method deleteDirectory() should work on directories as well. Are you catching any exceptions when trying to delete? Have you double checked the path?

Edit2: You are calling deleteFile(), not deleteDirectory().

Edit3: You are right, its called removeDirectory(), not deleteDirectory :)

Thanks!

I decided to use client.removeDirectory(strReplaced); Now it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.