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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
* QUESTION: I have a file which contains the name of about 300 files. Most of these files are in 20 folders. How can I process all these files?

* ANSWER by rlevesque@videotron.ca.


*Replace path in next line by your path.
DEFINE !path()"D:\\data\\"!ENDDEFINE.


*************.
*Prepare dummy files to illustrate the solution.
*************.

DATA LIST LIST /fdrname(A12).
BEGIN DATA
"folder1"
"folderb"
"last folder"
END DATA.
LIST.

*The next file would contain the names of the 20 folders.
SAVE OUTFILE=!path + "Folder names.sav".

DATA LIST LIST /fname(A12).
BEGIN DATA
"file1"
"file2"
"another file"
"last file"
END DATA.
LIST.

* This file would countain the list of the 300 files names.
SAVE OUTFILE=!path + "File names.sav".

* each of the 300 files is in most (but not necessarily all) of the 20 folders.

* Make dummy data files.
DATA LIST FREE /dummy.
BEGIN DATA
1 2 3 4 5
END DATA.

*NOTE: each file must exist in FIRST folder whose name appears in the folder file.
* the dummy files may be made up of the variable names with no records.
SAVE OUTFILE=!path + "folder1\\file1.sav".
SAVE OUTFILE=!path + "folderb\\file1.sav".
SAVE OUTFILE=!path + "folder1\\file2.sav".
SAVE OUTFILE=!path + "folderb\\file2.sav".
SAVE OUTFILE=!path + "last folder\\file2.sav".
SAVE OUTFILE=!path + "folder1\\another file.sav".
SAVE OUTFILE=!path + "folder1\\last file.sav".
SAVE OUTFILE=!path + "folderb\\last file.sav".


********************.
**** Start the job.
********************.

* Prepare the Folder name file.
GET FILE=!path + "Folder names.sav".
COMPUTE id=$CASENUM.
SAVE OUTFILE=!path + "folders.sav".

* Prepare the files names file.
GET FILE=!path + "File names.sav".
* replace 350 by a number at least as large as the number of files.
LOOP id=1 TO 350.
XSAVE OUTFILE=!path + "files.sav" /KEEP id fname.
END LOOP.
EXECUTE.

GET FILE=!path + "Files.sav".
SORT CASES BY id.
MATCH FILES /FILE=*
 /TABLE='D:\\data\\folders.sav'
 /BY id.

SELECT IF LEN(RTRIM(fdrname))>0.
*Write syntax which will combine the files.
SORT CASES BY fname.

MATCH FILES FILE=* /BY fname /FIRST=first /LAST=last.
STRING fullname(A80), savename(A20), quote(A1).
COMPUTE quote="'".
COMPUTE fullname=CONCAT(quote,RTRIM(fdrname),"\\",RTRIM(fname),".sav",quote).
COMPUTE savename=CONCAT(quote,RTRIM(fname),".sav",quote).

DO IF first.

WRITE OUTFILE=!path + "syntax to combine files.sps"
/ 'GET FILE="' !path '" + ' fullname '.'.
ELSE.
WRITE OUTFILE=!path + "syntax to combine files.sps"
 /'ADD FILES FILE=* /FILE="' !path '" + ' fullname '.'.
END IF.

DO IF last.
WRITE OUTFILE=!path + "syntax to combine files.sps"
 /'SAVE OUTFILE="' !path '" + ' savename '.'.
END IF.

Execute.

*Then load the "syntax to combine files.sps"  and run it.
* NOTE that using the INCLUDE command to run the syntax 
* will NOT work because of the missing files.