8

I am preparing a Jenkins pipeline script in Groovy language. I would like to move all files and folders to another location. As Groovy supports Java so I used below java code to perform the operation.

pipeline{
 agent any    
    
 stages{    
     stage('Organise Files'){                         
         steps{  
                script{                        
                    File sourceFolder = new File("C:\\My-Source");
                    File  destinationFolder = new File("C:\\My-Destination");                                                   
                    File[] listOfFiles = sourceFolder.listFiles();
                    echo "Files Total: " + listOfFiles.length;  

                    for (File file : listOfFiles) {
                        if (file.isFile()) {
                            echo file.getName()                                                                
                            Files.copy(Paths.get(file.path), Paths.get("C:\\My-Destination"));                                   
                        }
                    }                  
                }                                
            }                           
        } 
    }
}

This code throws the bellow exception:

groovy.lang.MissingPropertyException: No such property: Files for class: WorkflowScript

I tried with below code too, but it's not working either.

FileUtils.copyFile(file.path, "C:\\My-Destination");

Finally, I did try with java I/O Stream to perform the operation and the code is bellow:

def srcStream = new File("C:\\My-Source\\**\\*").newDataInputStream()
def dstStream = new File("C:\\My-Destination").newDataOutputStream()
dstStream << srcStream
srcStream.close()
dstStream.close()

But it's not working either and throws the below exception:

java.io.FileNotFoundException: C:\My-Source (Access is denied)

Can anyone suggest me how to solve the problem and please also let me know how can I delete the files from the source location after copy or move it? One more thing, during the copy can I filter some folder and files using wildcard? Please also let me know that.

Gerhard
  • 6,850
  • 8
  • 51
  • 81
mnu-nasir
  • 1,642
  • 5
  • 30
  • 62

2 Answers2

21

Don't execute these I/O functions using plain Java/Groovy. Even if you get this running, this will always be executed on the master and not the build agents. Use pipeline steps also for this, for example:

bat("xcopy C:\\My-Source C:\\My-Destination /O /X /E /H /K")

or using the File Operations Plugin

fileOperations([fileCopyOperation(
  excludes: '',
  flattenFiles: false,
  includes: 'C:\\My-Source\\**',
  targetLocation: "C:\\My-Destination"
)]).

I assume I didn't hit the very right syntax for Windows paths here in my examples, but I hope you get the point.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • thanks for your answer. but how can I move the files to the destination folder excluding 2 specific files and one folder? it means I have 10 DLL files, 2 JSON files, and few other folders. Now I want to move all the DLLs and all the folders except one and JSON files to the destination folder. the rest of the JSON file and folders will remain in the same location and after moving the files, it will be deleted from the source location.How can I do this? I don't see any move operation. – mnu-nasir Nov 19 '18 at 07:07
  • I think in the above answer includes should have C:\\My-Source\\** . isn't it? – merlachandra Aug 08 '19 at 12:33
1

you can add simply power shall plugin and create a job and add a build step choose power shall option

Copy-Item -Path “D:\repo\test-Jenkin*” -Destination “E:\new copy\repo1” -Recurse