SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Script to create datestamp folder causing issue


Script to create datestamp folder causing issue

Author
Message
Arpan Shinde
Arpan Shinde
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 271
I am trying to create date stamp folder and later move files to that folder through following script.
But when i execute script it create folder but as %folderdate% and not as datestamp.
where as when i execute three of the commands through command prompt it works fine. So can anyone help me to sort out this and tell where iam going wrong.

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%'
print @setdate
select @CreateDir='mkdir G:\Testing\test1\%folderdate%'
print @CreateDir
select @Filemov='move /Y G:\Testing\*.csv G:\Testing\test1\%folderdate%'
print @Filemov
exec master..xp_cmdshell @setdate
exec master..xp_cmdshell @CreateDir
exec master..xp_cmdshell @Filemov
NickDBA
NickDBA
SSC-Addicted
SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)SSC-Addicted (471 reputation)

Group: General Forum Members
Points: 471 Visits: 938
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search