1

I'm trying to write some code for my Jenkins library. I've written the following lines:

steps.println 'Destination Path = ' + target_path

def file = new File(target_path)
steps.println 'target file name: ' + file.getName()

def folder = file.getAbsoluteFile().getParentFile()
steps.println 'target file path: ' + folder.getName()
folder.mkdirs()

But when this code gets executed, I am getting these results

Destination Path = files/docs/My File Name.pdf
[Pipeline] echo 
target file name: My File Name.pdf
[Pipeline] echo 
target file path: docs

Everything I've found says that getParentFile() is supposed to return ALL the path except the last part (the file name), but it is clearly not doing it in this case. Its just returning the next file up.

I'm also seeing, but is not represented in this code sample, that the getAbsoluteFile() method is simply tacking a slash in front of the whole path. Since this is running under Jenkins, I know that there should at least be a workspace path in front of it, yet I'm not getting any of that.

I thought maybe the spaces in the filename were a problem, but even when I tried without spaces I got the same result.

Can someone please help me with what I'm doing wrong or what I've missed?

Kevin Milner
  • 821
  • 2
  • 14
  • 30
  • Read about File.getName() - it returns a name without a path. Use folder.toString() instead of folder.getName() to see path+name... – daggett Oct 12 '18 at 06:44
  • And about workspace check this: https://stackoverflow.com/questions/36934028/get-absolute-path-to-workspace-directory-in-jenkins-pipeline-plugin – daggett Oct 12 '18 at 06:49
  • Both of these suggestions helped me out and resolved this issue (now on to the next!) If you want to put this as an answer I'll accept it. – Kevin Milner Oct 12 '18 at 13:42

0 Answers0