Check if there are files in Folder

Hi,

I'm wondering if its possible to return the number of files in a folder suing something like DIR command.

I'm wanting to do something like,

If count(DIR) >0 then do something

Else End

exec master.dbo.xp_cmdshell 'dir C:\Test\'

July 20th, 2015 7:56pm

This article might help:

http://sqlmag.com/t-sql/file-operations-made-easy

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2015 10:27pm

Hi,

I'm wondering if its possible to return the number of files in a folder suing something like DIR command.

I'm wanting to do something like,

If count(DIR) >0 then do something

Else End

exec master.dbo.xp_cmdshell 'dir C:\Test\'

I think that the xp_cmdshell will definitely work. I've used it before to get the oldest backup and delete it after the new backup completed and was verified. The trick here is:

 
DIR *.BAK /O /B

The /O switch orders the files and /B reports the file and folder names ONLY (no fluff!)

You could also try:

EXEC master..xp_dirtree 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup', 10, 1

Or CLR as suggested by aucj...

July 20th, 2015 11:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics