You don't need any scripting to sort the files. Use the Switch variables. An image will contain exif metadata. It will tell you the exif:PixelXDimension and exif:PixelYDimension. If exif:PixelYDimension is greater than exif:PixelXDimension the image is landscape. If it's the opposite it's portrait and if they are equal it's square.