Tuesday, July 24, 2007

Samba + Groovy

If you have an application where you need to connect with a Samba server I suggest you use JCIFS. This is a nice library for Java that allows file interaction on a remote machine via Samba. The nice thing about this library is that it uses classes that function just like the file classes you normally use in Java. Here is a nice little groovy script JCIFS:


smbAuth = new NtlmPasswordAuthentication("bob.com", "joe", "smoe")

fullPath = "smb://bob.com/media$/blah.txt"

smbFile = new SmbFile(fullPath, smbAuth)

if(smbFile.exists()) {
    smbDate = new Date(smbFile.getLastModified())
    println "Date on SMB file: ${smbDate}"
}

The above example connects to the samba machine, gets the file we want, and then gets and prints the modified date on that file. You can use this library to write quick scripts to sync files from a non samba machine to another machine (Windows?).