Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in NYC Sept 4th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
abhi90
Creator II
Creator II

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?

 

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable

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));
}

View solution in original post

17 Replies
abhi90
Creator II
Creator II
Author

Hi @TRF@rhall@manodwhb can u help please

Anonymous
Not applicable

abhi90
Creator II
Creator II
Author

Hi @rhall,

 

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

Anonymous
Not applicable

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.

abhi90
Creator II
Creator II
Author

Hi @rhall,

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

 

abhi90
Creator II
Creator II
Author

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

Anonymous
Not applicable

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.

abhi90
Creator II
Creator II
Author

Hi @rhall,

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


RENAMING.JPG
tfilelist.JPG
Anonymous
Not applicable

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));
}