Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Folder Exits or not

Author  Topic 

Madhu_sms
Starting Member

2 Posts

Posted - 2015-05-04 : 10:00:18
Hi friends,
Can you please help me to give the logic for finding folders in specified drive. As an example below:
Declare @NewDrive varchar(1)
Set @NewDrive = 'C'
DECLARE @StringValue varchar(2000)
SET @StringValue = 'C:\Drive1\Folder1\db3.ndf'

I need to find whether the folders in @StringValue exits in @NewDrive or not. If not should create it.

@StringValue can have many folders.

Please help me as soon as possible and i am having less time to do it.
Regards
Madhu




Madhusudhan

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-05-04 : 12:33:50
This can be completed by using Powershell - use Get-ChildItem , This post shows some examples http://www.sqlserver-dba.com/2012/12/sql-server-powershell-get-childitem-with-examples.html

The issue you may face with using Powershell - is it may not be suitable with how you intend to manage the overall job.

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

Madhu_sms
Starting Member

2 Posts

Posted - 2015-05-05 : 01:18:01
Thanks a Lot for replying. I need to achive this by SQL server queries. Can you please help me if you any more information.



quote:
Originally posted by jackv

This can be completed by using Powershell - use Get-ChildItem , This post shows some examples http://www.sqlserver-dba.com/2012/12/sql-server-powershell-get-childitem-with-examples.html

The issue you may face with using Powershell - is it may not be suitable with how you intend to manage the overall job.

Jack Vamvas
--------------------
http://www.sqlserver-dba.com




Madhusudhan
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-05-05 : 01:47:24
If you need to achieve by t-sql , then you'll need to execute some sort of script from t-sql , using xp_cmdshell. It will require SQL Server to interact with the OS.
But before you enable xp_cmdshell consider the security risks of using xp_cmdshell on the SQL Server. For example , if the server is a Production server there are risks which may not be acceptable. http://www.sqlserver-dba.com/2015/02/xp_cmdshell-the-most-dangerous-extended-stored-procedure-on-the-planet.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2015-05-05 : 02:27:57
[code]exec xp_cmdshell 'IF EXIST C:\folder\NUL ECHO folder exists'[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-05-06 : 01:14:24
To check if it doesn't exist and then create the folder

IF not exist C:\folder (mkdir C:\folder)


Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
   

- Advertisement -