' 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