My preference is to use File (or Path) object whenever dealing with files – and 
then ONLY using forward slashes.  So for your request:
def scriptDir = /C:\Folder1\My Documents\Folder2\Version\etc\/
becomes
def scriptDir = new File("C:/Folder1/My Documents/Folder2/Version/etc")
or (if you really wanted the path as a string)
def scriptDir = new File("C:/Folder1/My 
Documents/Folder2/Version/etc").absolutePath

I agree that copy-pasting file paths from File Explorer and then twiddling the 
slash direction is a bit tedious – but better than figuring out all that 
escaping!  I dare say that using a File object has overheads – but at least you 
can test if the file or directory exists. Iterate over the files etc. etc.

Adding pieces of a path or path and file name together, again use the File 
class:

def myScript = new File("C:/Folder1/My Scripts", "Script1.groovy")
def myScript = new File(scriptDir, "Script1.groovy")

It saves getting the System.properties['file.separator'] – and you can even 
ignore the drive letter (if it is on the current disk).
def myScript = new File("/Folder1/My Scripts", "Script1.groovy")

And yes – I use groovy rather a lot on Windows.

Merlin Beedell
Cryoserver: A focused, flexible email archive delivered by experts

From: Joe Wolf [mailto:joew...@gmail.com]
Sent: 14 June 2017 14:53
To: users@groovy.apache.org; Paul King <pa...@asert.com.au>
Subject: Re: Slashy strings in GroovyConsole

Also be careful when using slashy Strings with path names that start with a 
lowercase "u"

They'll get interpreted as a unicode character escape, e.g. /c:\users/ --> 
TokenStreamIOException: Did not find four digit hex character code.

(BTW, I'm glad to see there are other GroovyConsole users on Windows out there)

-Joe

On Wed, Jun 14, 2017 at 6:32 AM, Paul King 
<pa...@asert.com.au<mailto:pa...@asert.com.au>> wrote:
What bo zhang said or use dollar slashy string.

Cheers, Paul.

On Wed, Jun 14, 2017 at 7:37 PM, bo zhang 
<zhangbo...@gmail.com<mailto:zhangbo...@gmail.com>> wrote:
def scriptDir = /C:\Folder1\My Documents\Folder2\Version\etc\//

note the last slash. \/ is escaping /

On Wed, Jun 14, 2017 at 4:44 PM, Dmytro Shyshchenko 
<shysche...@gmail.com<mailto:shysche...@gmail.com>> wrote:
Long time ago I was using slashy strings in my groovy scripts for the paths on 
Windows. Somehting like this:
def scriptDir = /C:\Folder1\My Documents\Folder2\Version\etc\/

However in the recent versions, like 2.3.6 or 2.4.11, I can no longer execute 
those scripts via a GroovyConsole. It always gave me an error pointing to the 
last charecter of the script. Like this:
unexpected char: 0xFFFF at line: 19, column: 13
which was a bit misleading...

When I change the paths to a "normal" srtings, everything works fine, as it was 
before.
def scriptDir = "C:\\Folder1\\My Documents\\Folder2\\Version\\etc\\"

Is it a bug? Shall I report it?





[https://www.cryoserver.com/wp-content/uploads/2017/03/ISO-logo-signature.jpg]  
[https://www.cryoserver.com/wp-content/uploads/2017/03/softech-signature.png]   
[https://www.cryoserver.com/wp-content/uploads/2017/03/ITE-signature.png]

FCS (UK) Limited is registered in England & Wales, Registration Number: 5940018 
- Registered Address: Wigglesworth House, 69 Southwark Bridge Road, London SE1 
9HH Disclaimer: This e-mail contains proprietary information, some or all of 
which may be legally privileged and is for the intended recipient only and the 
information in it are provided in confidence and may not be disclosed to any 
third party or used for any other purpose without the express permission of FCS 
(UK) Ltd. If an addressing or transmission error has misdirected this e-mail, 
please notify the author by replying. If you are not the intended recipient you 
should not use, disclose, distribute, copy, print or relay on this e-mail. The 
views expressed in this communication are not necessarily the views held by FCS 
(UK) Limited.

Reply via email to