Groovy split path into name and parent

Viewed 3973

I am trying to split a path into parent and the name.

When trying

String path = "/root/file"
File file = new File(path)

println("Name: " + file.name)
println("Parent: " + file.parent)

We get

Name: file
Parent: /root

With the Windows path C:\\root\\file.exe we get

Name: C:\root\file.exe
Parent: null

Is this the intended behaviour? And if it is how do I get the same result for a Windows path? (If possible please without using regular expressions)

1 Answers

use .replace to change the "\" to "/

String path = "C:\\root\\file.exe"
path = path.replace("\\","/")
File file = new File(path)

println("Name: " + file.name)
println("Parent: " + file.parent)
Related