Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPSS Get working directory

Tags:

spss

I am using SPSS 19 und would like to get the current working directory to use the INSERT command, to call additional syntax files. Does somebody know how to do it? There seems to be a Python command (SpssClient.GetCurrentDirectory()) but that returns a gibberish error code (I love this pile of crap called SPSS....)

like image 363
Christian Sauer Avatar asked Dec 20 '22 11:12

Christian Sauer


2 Answers

Rather than using the scripting API's, you can use the programmabilty API's like this.

begin program.  
import spss, spssaux  
workingdir = spssaux.getShow("DIRECTORY")  
spss.Submit("""FILE HANDLE cwd /NAME="%s".""" % workingdir)  
end program.  

This defines a file handle named cwd.

Note also that INSERT has a CD keyword that changes the backend working directory to whatever location is specified in FILE.

HTH, Jon Peck

like image 152
JKP Avatar answered Feb 08 '23 15:02

JKP


After some googling I found a working method:

BEGIN PROGRAM.
import spss
import SpssClient
CONST_FHandleName = 'CurrentDir'

#SpssClient.StartClient() /stop nötig, sonst gibt es Probleme mit dem zugriff auf SPSS
try:
    SpssClient.StartClient()
    syntaxpath =SpssClient.GetDesignatedSyntaxDoc()
                currentdir = os.path.dirname(syntaxpath.GetDocumentPath())
    FHandle="File handle " + CONST_FHandleName + " /name='" + currentdir + "'"
finally:
    SpssClient.StopClient()
print "The FHandle Dir is now: " + currentdir 
print "The FHandle Dir is now: " + FHandle

spss.Submit(FHandle)
END PROGRAM.

This program will get the directory of the current Syntax-File and set the directory as a file handle. Of course, this is not the real working directory, but a approximation that works for me.

Caution:

spss.getworkingdirectory returns apparently the first startup folder - NOT the folder of the active dataset (great if you startet SPSS first and then loaded the dataset - it will still point to its home directory, where it cannot write. SPSS 19 behaviour...)

like image 41
Christian Sauer Avatar answered Feb 08 '23 14:02

Christian Sauer