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
' Demo: this script creates as many sav files as there are txt files (with a given mask eg I*.txt) in a folder
' The txt files must have the same format
' rlevesque@videotron.ca 2003/2/8
' Visit my SPSS site http://pages.infinit.net/rlevesqu/index.htm
Option Explicit

Sub Main
	Dim strPath As String
	Dim strFilemask As String
'	Modify the next 2 lines to fit your requirements
	strPath		="c:\\temp\\"
	strFilemask	="I*.txt"
	Call CreateSavFiles(strPath, strFilemask)

	strFilemask="I*.sav"
	'Comment next line if you do not need a single sav file including all the sav files
	Call CombineDataFiles(strPath, strFilemask)
End Sub


Sub CreateSavFiles (strPath As String, strFilemask As String)
' Create one sav file for each txt file
Dim strFname As String
Dim strCmd As String
Dim intFileNb

	If InStr(strPath, "\\") = 0 Then		'no path given, use current folder
		strPath = objSpssApp.GetSPSSPath
	End If
	If Len(strFilemask) = 0 Then 		'no file Mask given, combine all files
		strFilemask = "*.sav"
	End If

    'Get the first file name
    strFname = Dir$(strPath & strFilemask)

	While strFname <> ""
		strCmd = "GET FILE='" & strPath & strFname & "'." & vbCr
		strCmd = "DATA LIST FILE='" & strPath & strFname & "' LIST (TAB) SKIP=1 /event (F5.2) scl (F9.5)." & vbCr
		strCmd = strCmd & "STRING idstat(A6)." & vbCr
		strCmd = strCmd & "COMPUTE idstat='" & Mid(strFname,2,6) & "'." & vbCr
		strCmd = strCmd & "SAVE OUTFILE='" & strPath & Mid(strFname,1,InStr(strFname,".")-1) & ".sav'." & vbCr
		strCmd = strCmd & "EXECUTE."

		'Debug.Print strCmd
		objSpssApp.ExecuteCommands strCmd , True
		'Get next file name
		strFname = Dir$()
	Wend

End Sub


Sub CombineDataFiles (strPath As String, strFilemask As String)
' Demo: this script combines all sav files (meeting a given mask eg *.sav) located in a given folder
' The sav files will normally have the same variables (but it works even if they don't)
' The label of the variable source contains the path of the data files
' The value labels of the variable source contain the name of the source data files
' rlevesque@videotron.ca 2001/10/2
' Visit my SPSS site http://pages.infinit.net/rlevesqu/index.htm
Dim strFname As String
Dim strCmd As String
Dim intFileNb

	If InStr(strPath, "\\") = 0 Then		'no path given, use current folder
		strPath = objSpssApp.GetSPSSPath
	End If
	If Len(strFilemask) = 0 Then 		'no file Mask given, combine all files
		strFilemask = "*.sav"
	End If

    'Get the first file name and load file
    strFname = Dir$(strPath & strFilemask)
	strCmd = "GET FILE='" & strPath & strFname & "'." & vbCr
	strCmd = strCmd & "COMPUTE source=1." & vbCr
	strCmd = strCmd & "VALUE LABEL source 1 " & "'" & strFname & "'." & vbCr
	strCmd = strCmd & "VARIABLE LABEL source 'path=" & strPath & "'." & vbCr
	strCmd = strCmd & "EXECUTE."
	objSpssApp.ExecuteCommands strCmd , True

	'Combine the other files
	intFileNb = 2
    While strFname <> ""
		strFname = Dir$()
		If strFname <> "" Then
			strCmd = "ADD FILES /FILE=* /FILE='" & strPath & strFname & "'." & vbCr
			strCmd = strCmd & "IF MISSING(source) source=" & intFileNb & "." & vbCr
			strCmd = strCmd & "ADD VALUE LABEL source " & intFileNb & " '" & strFname & "'." & vbCr
			strCmd = strCmd & "EXECUTE."
			Debug.Print strCmd
			objSpssApp.ExecuteCommands strCmd , True
			intFileNb = intFileNb + 1
		End If
    Wend

    ' Save the combined file
	strCmd = "SAVE OUTFILE='" & strPath & "combined file.sav'." & vbCr
	strCmd = strCmd & "EXECUTE."
	objSpssApp.ExecuteCommands strCmd , True

End Sub