SD card write very random, mostly not working

Have you tried the SD without all those other devices connected to the Arduino? Have you considered the possibility it may be a power problem? Or one of those other devices is interfering with the SD in some way?

Did you bother to try each device separately before throwing that sketch together? I recommend starting with the ethernet shield. Get the w5100 and the SD working, then add one device at a time, insuring each device works with your current setup before adding another.