Extract file writing and directory creation from file_loader.js