Four Stars

Delete empty folders and sub forlders

Hi,

 

I'm trying to delete the empty folders/directories recursively. I tried using the tFileList to look for empty directories and then tFileDelete components, but the problem was that it was deleting the non empty folders as, it deleted the parent folder if there is no file in that, regardless of the subfolders within that main folder having files in them.

So if my directory structure is like this:

a

a>b

a>c>file1.txt

a>c>d

I want 'b' and 'd' to be delete, since they contains no file, but 'a' and 'c' remains untouched!

 

Any suggestion?

  • Data Integration
4 REPLIES
Five Stars

Re: Delete empty folders and sub forlders

Hi,

 

You can use tSystem component and in that you can add the command to delete the empty folders recursively.

 

Thanks

Four Stars

Re: Delete empty folders and sub forlders

Thank you Abhishek, The problem is that I don't want to use shell script, since tSystem is not good at handling pipe "|", also the talend code that I'm working on is to replace a shell script.

 

So is there a way to handle this solely in Talend?

Employee

Re: Delete empty folders and sub forlders

@fatemehbehfar , I can frame the below solution , ca you try once and let us know if this solves your problem ?

 

1. tfilelist (list all folders subfolderscheck box) -> iterate -> titeratetoflow (change schema one field as 'file_folder_name type string 255) -> use tfile list property field called 'file_path' --> tfileoutputdelimited (append mode)

 

This file should give you top down list of all folder , files etc

on subjob ok

 

2. tfileinputdelimited -> tmap (use logic to find last index of "/" and use fucntions like 'contains' *.txt* , flag field like Y or N -> give all 'Y' records into tfiledelete

 

so my approach is to classify the records having .txt file and the one which don't have (N) will be deleted

 

hope this helps. yu can twist and turn this approach to make it work.

 

Regards

Nayak

Eleven Stars

Re: Delete empty folders and sub forlders

I don't think that will quite fit the requirement @snayak. For example, the following folder structure requires you to think about the content of nested folders before deciding whether to delete a folder which does not contain files BUT does contain folders which contain files.

C:\Test\Folder1\Folder2 <--Keep 

C:\Test\Folder1\MyTest.txt <---Keep

C:\Test\Folder1\Folder2\Folder3\MyTest1.txt <---Keep

C:\Test\Folder1\Folder2\Folder3\Folder4 <----Delete

 

I decided to take a look at this @fatemehbehfar as it intrigued me. First of all I would say that the easiest and most straight forward way of achieving this requirement is to use a bit of Java. There are lots of examples online and it really isn't that hard. There are lots of arguments for not using pure Java when using Talend, but there are also some quite convincing arguments for using it when you have to go around the houses in order to achieve something with Talend components, when all it takes is a few lines of Java. This I would argue is such a case. However, I wanted to see if it could be done and believe I have done it. 

 

Since it is late here I have included the job (v6.2.1) and will let you figure out how it works. It is a little messy, but it works. Just replace the source folder in the first tFileList. I'm happy to answer questions if you get stuck. This job will not actually delete folders, but it will print out your folder structure and indicate whether each folder should be deleted or not. You can test this and then modify the job to actually carry out the deletes if it suits your requirement. If the "delete" boolean that is printed says "true", then the folder should be deleted. 

 

I hope this helps.

 

Rilhia Solutions