An elegent way to handle OS specific slashes?


(Ken) #1

I’m using the ‘Exec’ build-in tasks, and I’m wondering if there is a cleaner way to handle OS specific slashes/backslashes. This task need to be able to run on both Windows and Linux:

task myTask(type: Exec) {

    String command = "command ${projectDir}/${solution}/*.exe"

    if  (operationSystem == 'windows' {
        command = command.replace("/", "\\")
        commandLine('cmd /c ' + command)
    } else {
        commandLine(command)
   }  
}

I’m already not too happy about the if/else for the ‘cmd /c’ (it’s a shell command), is there any way to avoid the slash replacement?


(Stefan Oehme) #2

You could use File.separator instead of /.