4

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....)

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85

4 Answers4

4

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

buhtz
  • 10,774
  • 18
  • 76
  • 149
JKP
  • 5,419
  • 13
  • 5
  • I know the CD-command, but my problem was to find a useful place to point the file handle. I did not want to hardcode this path... – Christian Sauer Jan 08 '13 at 15:21
3

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...)

eli-k
  • 10,898
  • 11
  • 40
  • 44
Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
2

i have been able to get SPSS 18 to set the directory it has been started in as the default directory. Select Edit > Options > File Locations. In the data files and other files boxes enter a single period. Whenever you start SPSS by clicking on a data or syntax file, it will default to the current directory, i.e. the directory it was started in. Works for SPSS18, ymmv with other versions.

Incidentally, the same trick works for Excel but I haven't found any other applications that is works for. In my opinion this is the way programs should work rather than forcing you into some directory you don't want.

Tim Rosnau
  • 21
  • 1
2

In SPSS V23 Try

CWD.
SHOW DIRECTORY.

If the command CWD is not recognized, download/install the CWD ('Current Working Directory') extension. Select Utilities > Extension Bundles > Download and Install Extension Bundles, and search for CWD.

munozedg
  • 21
  • 2