Eight Stars

Rename FolderNames present in Same Directory

Hi All,

I want to rename Some Folders present in a Directory. Is it possible to achieve in anyway?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Fifteen Stars

Re: Rename FolderNames present in Same Directory

I've made an assumption that your folders will only have a "_" character in them if they have already been renamed. There are better ways of doing this using the date stamp, but this will do. A better way of dealing with this would be to move the folder. Your job will end up taking longer and longer with the more folders you build up in this source folder.

 

Anyway, this code will stop your previously renamed folders from being renamed again. As I said, you may want to think of better logic than just looking for a "_" character....

 

System.out.println(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")));

if(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")).indexOf('_')==-1){

	java.io.File dir = new java.io.File(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")));

	String newFolderName = 	((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))+"_"+routines.TalendDate.getDate("yyyyMMddHHmmss");

	dir.renameTo(new java.io.File(newFolderName));
}
Rilhia Solutions
17 REPLIES
Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @TRF@rhall_2_0@manodwhb can u help please

Fifteen Stars

Re: Rename FolderNames present in Same Directory

Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @rhall_2_0,

 

What to give command over there? Basically I have to rename all the folder names in that directory to foldername_Sysdate. Sydate I am getting in the Jobflow. Using Global variable of tflowtoiterate I can get that. But How to rename all the folder names? is it possible?By Java code? @xdshi

Fifteen Stars

Re: Rename FolderNames present in Same Directory

You could always use a tFileList and set it to look for directories, then connect a tJava component and use this code....

 

java.io.File dir = new java.io.File(((String)globalMap.get("tFileList_1_CURRENT_FILEDIRECTORY")));

String newFolderName = "my_new_folder";

dir.renameTo(newFolderName);

You may need to tweak the code (written off the top of my head), but it should kind of work.

Rilhia Solutions
Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @rhall_2_0,

My New Folder Name will be OldSubFolderName_Old_Date. Plus the last line of code is giving compilation error. 

 

Eight Stars

Re: Rename FolderNames present in Same Directory

One More thing is happening,

When I am printing the tfilelist Current File directory in tjava I am seeing it is printing only the Parent Directory Name. But not taking inside Subdirectory Names

Fifteen Stars

Re: Rename FolderNames present in Same Directory

Sorry, this was written quickly without access to try it. I've just tested it and tweaked it for you. This code will work.....

 

System.out.println(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")));

java.io.File dir = new java.io.File(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")));

String newFolderName = ((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))+"_"+routines.TalendDate.getDate("yyyyMMddHHmmss");

dir.renameTo(new java.io.File(newFolderName));

Keep in mind that I had no idea what you meant by "old date" so I have just added some code to add the current date to the end of the folder names. You will need to adjust that.

Rilhia Solutions
Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @rhall_2_0,

Your solution is working. But only Problem is My Folder is being renamed yesterday is also renaming with today's timestamp. But I dont want to touch earlier folders. I want to rename only folders which created in last Run. I have attached the snapshot how folders are creating. Can You please help

Fifteen Stars

Re: Rename FolderNames present in Same Directory

I've made an assumption that your folders will only have a "_" character in them if they have already been renamed. There are better ways of doing this using the date stamp, but this will do. A better way of dealing with this would be to move the folder. Your job will end up taking longer and longer with the more folders you build up in this source folder.

 

Anyway, this code will stop your previously renamed folders from being renamed again. As I said, you may want to think of better logic than just looking for a "_" character....

 

System.out.println(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")));

if(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")).indexOf('_')==-1){

	java.io.File dir = new java.io.File(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")));

	String newFolderName = 	((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))+"_"+routines.TalendDate.getDate("yyyyMMddHHmmss");

	dir.renameTo(new java.io.File(newFolderName));
}
Rilhia Solutions
Eight Stars

Re: Rename FolderNames present in Same Directory

Not working @rhall_2_0 Smiley Sad 

Fifteen Stars

Re: Rename FolderNames present in Same Directory

I tested it and it worked. That is exactly the code I tested. What is not working exactly?
Rilhia Solutions
Eight Stars

Re: Rename FolderNames present in Same Directory

RENAME part

Fifteen Stars

Re: Rename FolderNames present in Same Directory

It will rename any flder without a "_" character in it. I've tested that this is the case. Can you give an example of what is not being renamed?

Rilhia Solutions
Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @rhall_2_0,
Yeah none of the folders are being renamed with the new code. Not even the folders with date timestamp.
Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @rhall,
With folders being present there still it is unable to rename my folders without timestamp. 😔😔😔😔. It is iterating perfectly from tFilelist. Printing folderNames. But dont know rename part not happening
Fifteen Stars

Re: Rename FolderNames present in Same Directory

Do the folder paths have "_" in them anywhere? 

Rilhia Solutions
Eight Stars

Re: Rename FolderNames present in Same Directory

Hi @rhall,
Dont know how to thanku Sir. 😊 Thanks a lot. Your solution worked like charm. 😊