You can use the FolderReferences property of a document to do this. This code will put all documents that have not been foldered into a new "Lost Documents" folder:
Sub Initialize Dim sess As New NotesSession Dim db As NotesDatabase Set db = sess.CurrentDatabase db.FolderReferencesEnabled = True Dim coll As NotesDocumentCollection Set coll = db.AllDocuments Set doc = coll.GetFirstDocument While Not(doc Is Nothing) i = 0 If doc.FolderReferences(0) <> "" Then 'doc is in at least one folder, so pull it from this collection Call coll.DeleteDocument(doc) 'just pulls it from the folder) End If Set doc = coll.GetNextDocument(doc) Wend Call coll.PutAllInFolder("Lost Documents") 'all those that remain in the collection End Sub