Aurora_Gaimon

CA PPM: How to move files with GEL

Blog Post created by Aurora_Gaimon Employee on May 11, 2017

I believe this is another typical question any developer may face when writing GEL script. How do I move files from one location to another?

 

There are two ways to achieve it:

  • Copy file in new location and delete old one.
  • Use renameTo function of the java.IO.File class.

 

Let's see in more details those options.

 

 

Option A: Copy file in new location and delete old one

 

Please, review related post on how to copy and delete a file:

CA PPM: How to copy files with GEL 

CA PPM: How to delete files with GEL 

 

 

Option B: Use renameTo function of the java.IO.File class

 

For demo purposing, we will move one file testDemo.txt from Fodler1 to Folder2:

 

  • Folders structure:

 

File is located under Folder1:

 

Folder2 is empty:

 

 

  • Execute the following GEL script:
<gel:script  xmlns:core="jelly:core"  
xmlns:util="jelly:util" 
xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary">
 

     <core:set var="oldFile" value="c:\Folder1\testDemo.txt"/> 
     <core:set var="newFile" value="c:\Folder2\testDemo.txt"/>

     <core:new className="java.io.File" var="v_oldFile">
          <core:arg type="java.lang.String" value="${oldFile}"/>
     </core:new>
     <core:new className="java.io.File" var="v_newFile">
          <core:arg type="java.lang.String" value="${newFile}"/>
     </core:new>

     <core:invoke method="renameTo" on="${v_oldFile}" var="fileMoved">
          <core:arg type="java.io.File" value="${v_newFile}"/>
     </core:invoke>

</gel:script>

 

 

 

.

 

  • Verify file is moved properly. Folder1  is now empty and  Folder2 contains the file:

 

 

** Note: You can also rename file to something else, but for this demo I just used the same file name to show it can be moved.

 

 

When to use option A or B?

From my point of view,  it does not matter if the created date time stamp of the file is not important. Otherwise, bear in mind:

 

Option A: Copy and Delete - It will create a new time stamp created date.

 

Option B: RenameTo - It will keep the original created date time stamp as per my demo screenshots.

  • File created: 5/11/2017 at 4:24PM
  • File moved via process: 5/11/2017 at 4:34PM
  • File moved still has 5/11/2017 at  4:24PM as created date.

 

 

 

 

Related links:

Rename file using GEL script? 

 

 

 

If you need help with GEL scripting, engage CA Services or ask questions in CA Communities.

 

That’s all. Thanks for reading until here. Did you like it? Please, don’t be shy and share it.

Outcomes