Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi All,
I want to rename Some Folders present in a Directory. Is it possible to achieve in anyway?
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)); }
Have you tried using the tSystem component?
https://help.talend.com/reader/hfF27SBEP9Avc0ofq7IDjw/mdw6s1fJbAlR4WwQQv9PCw
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
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.
Hi @rhall,
My New Folder Name will be OldSubFolderName_Old_Date. Plus the last line of code is giving compilation error.
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
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.
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
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)); }