Home Forums SQL Server 2005 Development Script to create datestamp folder causing issue RE: Script to create datestamp folder causing issue

  • I think its due to the fact that each call to xp_cmdshell spawns a new shell process and so the second call (2nd process) can't see the variable. Which explains why its works in the cmd shell as one statement.

    You can try this, it works on my machine. Alter the path as required

    declare @setdate varchar (100)

    declare @CreateDir varchar (100)

    declare @Filemov varchar (100)

    --select @setdate='set folderdate=%date:~0,2%-%date:~3,2%-%date:~6,4%'

    select @setdate = convert(varchar(11),getdate(),105)

    print @setdate

    select @CreateDir='mkdir C:\Test\' + @setdate

    print @CreateDir

    select @Filemov='move /Y C:\Test\*.txt C:\Test\' + @setdate

    print @Filemov

    exec master..xp_cmdshell @CreateDir

    exec master..xp_cmdshell @Filemov

    Once you're happy you may want to specify with no output if you don't want it to interfere with a batch?