6

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)

Torbilicious
  • 467
  • 1
  • 4
  • 17
  • This answer for Java (https://stackoverflow.com/questions/3548775/platform-independent-paths-in-java) should work for Groovy too. – BalRog Jul 10 '17 at 16:44

1 Answers1

2

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)
Bron Juyan
  • 21
  • 2