Jenkins pipeline script to copy or move file to another destination
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.
mnu-nasir
Innovative software developer with 6+ years of experience in designing and developing software systems for various business domains. Well-versed in technology with a comprehensive understanding of business needs for building relevant and solution-oriented applications. Proven record in full life-cycle of the software development process including requirements gathering, design, coding, testing, debugging and maintenance. Possess advanced skills and knowledge of leading programming tools. Effective problem solver with a proactive approach to identifying and resolving issues.
Updated on April 16, 2020Comments
-
mnu-nasir about 4 years
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.
-
mnu-nasir over 5 yearsthanks 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.
-
merlachandra almost 5 yearsI think in the above answer includes should have C:\\My-Source\** . isn't it?