Create Text File From Stored Procedure
You could use SQL CLR. This allows (more-or-less) any C# code to be executed from within SQL Server as if it were a stored procedure. This includes OS and file system manipulation. The code can accept values from and return values to T-SQL. Permissions can be controlled just like any other stored procedure, sidestepping many of the concerns with xp_cmdshell.