1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
*QUESTION: How can I detect the existence of a file by syntax?

*ANSWER: posted to SPSS newsgroup by rlevesque@videotron.ca on 2001/05/15.

* First save the following file as "c:\\temp\\isfile.sps".
*------------ beginning of "c:\\temp\\isfile.sps".
GET FILE='d:\\temp\\mydatafile.sav'.
* replace the above with the path and name of the file you wish to check the existence.
DEFINE !isfile()'yes'!ENDDEFINE.
*------------ end of "c:\\temp\\isfile.sps".


********************.
* The first time the following syntax is ran, the file mydata.sav does not exists.
********************.
DATA LIST LIST /dummy.
BEGIN DATA
1
END DATA.

DEFINE !isfile()'no'!ENDDEFINE.

INCLUDE 'c:\\temp\\isfile.sps'.

STRING mydata(A14).
DO IF !isfile='yes'.
* File exists.
COMPUTE mydata="exists".
ELSE.
* File does not exists.
COMPUTE mydata="does not exist".
END IF.
EXECUTE.

********************.
* Now create the file before to prove the syntax detects its existence.
********************.
DATA LIST LIST /dummy.
BEGIN DATA
1
END DATA.

SAVE OUTFILE='d:\\temp\\mydatafile.sav'.
DEFINE !isfile()'no'!ENDDEFINE.

INCLUDE 'c:\\temp\\isfile.sps'.

STRING mydata(A14).

DO IF !isfile='yes'.
* File exists.
COMPUTE mydata="exists".
ELSE.
* File does not exists.
COMPUTE mydata="does not exist".
END IF.
EXECUTE.