-
XP_CMDSHELL from DOS Prompt and SQL Mail!
I have a problem like this.
I have a Folder on our NT4.0 server gets filled with different files with extensions(For ex. .bak, .new, .old, etc). These files with those extensions gets build up on a daily basis.
What I am looking for is,
a)I would like to do a count on those files with those extenstion
b)Send an email to the user and their boss if I have letz say more than 15 files of .bak or 15 files of .new files in that folder.
c) Also like to schedule this program to to run every day.
We have SQL Server6.5. Can some one assist me in coding this problem. Thanks
for your time. Roja96@hotmail.com
-
XP_CMDSHELL from DOS Prompt and SQL Mail! (reply)
something like this:
set nocount on
declare @cmd varchar(255)
,@directory varchar(255)
/* get list of files in directory */
select @directory = 'c: emp'
select @cmd = 'dir ' + @directory + ' /b'
/* insert into list of files into temp working table */
create table #tmpFileList (FileName varchar(255) not null, FileExtension varchar(255) null)
insert into #tmpFileList
(FileName)
exec master..xp_cmdshell @cmd
if @@rowcount =0
print 'no files today!'
else
begin
declare @FileName varchar(255)
,@FileExtension varchar(255)
,@pos int
,@temp varchar(255)
declare files_cursor cursor for
select FileName from #tmpFileList
open files_cursor
fetch next from files_cursor into @FileName
while (@@fetch_status=0)
begin
/* get file extension */
select @temp=@FileName
select @pos = charindex('.', @temp)
if @pos =0
select @temp = null
else
begin
while @pos<>0
begin
select @temp=substring(@temp, @pos+1, len(@temp))
select @pos = charindex('.', @temp)
end
end
update #tmpFileList set FileExtension = @temp
where current of files_cursor
fetch next from files_cursor into @FileName
end
close files_cursor
deallocate files_cursor
print 'list of files in directory:'
print '---------------------------'
select FileExtension
,FileName
from #tmpFileList
print 'file groups:'
print '---------------------------'
select FileExtension, count(*) FileCount from #tmpFileList
group by FileExtension
end
drop table #tmpFileList
set nocount off
let me know if you need anything else
------------
roja at 12/3/99 8:14:38 PM
I have a problem like this.
I have a Folder on our NT4.0 server gets filled with different files with extensions(For ex. .bak, .new, .old, etc). These files with those extensions gets build up on a daily basis.
What I am looking for is,
a)I would like to do a count on those files with those extenstion
b)Send an email to the user and their boss if I have letz say more than 15 files of .bak or 15 files of .new files in that folder.
c) Also like to schedule this program to to run every day.
We have SQL Server6.5. Can some one assist me in coding this problem. Thanks
for your time. Roja96@hotmail.com
-
XP_CMDSHELL from DOS Prompt and SQL Mail! (reply)
you can put that script into a stored proc in the master (or user) database on your SQL Server:
create procedure sp_FileInfoNotify
( @directory varchar(255)
,@recipient_list varchar(255)
)
as
set nocount on
declare @cmd varchar(255)
/* get list of files in directory */
select @directory = 'c: emp'
select @cmd = 'dir ' + @directory + ' /b'
/* insert into list of files into temp working table */
create table #tmpFileList (FileName varchar(255) not null, FileExtension varchar(255) null)
insert into #tmpFileList
(FileName)
exec master..xp_cmdshell @cmd
if @@rowcount =0
print 'no files today!'
else
begin
declare @FileName varchar(255)
,@FileExtension varchar(255)
,@pos int
,@temp varchar(255)
declare files_cursor cursor for
select FileName from #tmpFileList
open files_cursor
fetch next from files_cursor into @FileName
while (@@fetch_status=0)
begin
/* get file extension */
select @temp=@FileName
select @pos = charindex('.', @temp)
if @pos =0
select @temp = null
else
begin
while @pos<>0
begin
select @temp=substring(@temp, @pos+1, len(@temp))
select @pos = charindex('.', @temp)
end
end
update #tmpFileList set FileExtension = @temp
where current of files_cursor
fetch next from files_cursor into @FileName
end
close files_cursor
deallocate files_cursor
print 'list of files in directory:'
print '---------------------------'
select FileExtension
,FileName
from #tmpFileList
print 'file groups:'
print '---------------------------'
select FileExtension, count(*) FileCount from #tmpFileList
group by FileExtension
end
/* ---------------------------------------- */
/* notify user(s) */
exec master.dbo.xp_sendmail @recipient_list,
@subject = 'File Information',
@message = 'tooooo many files!'
drop table #tmpFileList
set nocount off
go
for information on how to use xp_sendmail stored proc - look in the SQL Server Books Online
to schedule this proc to run very day - you'll need to create a task -->
in SQL Server Enterprise Manager, under SQL Executive - Open 'Manage Scheuled Tasks' --> click on 'New Task' --> under type select 'TSQL' --> in command box enter sp_FileInfoNotify '<you folder name>', '<e-mail separated by ;>'
------------
Gregory at 12/9/99 10:25:51 AM
something like this:
set nocount on
declare @cmd varchar(255)
,@directory varchar(255)
/* get list of files in directory */
select @directory = 'c: emp'
select @cmd = 'dir ' + @directory + ' /b'
/* insert into list of files into temp working table */
create table #tmpFileList (FileName varchar(255) not null, FileExtension varchar(255) null)
insert into #tmpFileList
(FileName)
exec master..xp_cmdshell @cmd
if @@rowcount =0
print 'no files today!'
else
begin
declare @FileName varchar(255)
,@FileExtension varchar(255)
,@pos int
,@temp varchar(255)
declare files_cursor cursor for
select FileName from #tmpFileList
open files_cursor
fetch next from files_cursor into @FileName
while (@@fetch_status=0)
begin
/* get file extension */
select @temp=@FileName
select @pos = charindex('.', @temp)
if @pos =0
select @temp = null
else
begin
while @pos<>0
begin
select @temp=substring(@temp, @pos+1, len(@temp))
select @pos = charindex('.', @temp)
end
end
update #tmpFileList set FileExtension = @temp
where current of files_cursor
fetch next from files_cursor into @FileName
end
close files_cursor
deallocate files_cursor
print 'list of files in directory:'
print '---------------------------'
select FileExtension
,FileName
from #tmpFileList
print 'file groups:'
print '---------------------------'
select FileExtension, count(*) FileCount from #tmpFileList
group by FileExtension
end
drop table #tmpFileList
set nocount off
let me know if you need anything else
------------
roja at 12/3/99 8:14:38 PM
I have a problem like this.
I have a Folder on our NT4.0 server gets filled with different files with extensions(For ex. .bak, .new, .old, etc). These files with those extensions gets build up on a daily basis.
What I am looking for is,
a)I would like to do a count on those files with those extenstion
b)Send an email to the user and their boss if I have letz say more than 15 files of .bak or 15 files of .new files in that folder.
c) Also like to schedule this program to to run every day.
We have SQL Server6.5. Can some one assist me in coding this problem. Thanks
for your time. Roja96@hotmail.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|