Home All Groups Group Topic Archive Search About

Check if file exists then delete it

Author
14 Jul 2006 2:25 PM
mdscorp
I am almost done with my HUGE stored procedure and iowe it mostly to
contributions from all of you guys, THANKS
The last part of the T-SQL coding is I need to check for a file in the
server , delete it before I attempt to re-create it with new data for
the next day, any code for this ?

thanks all

Author
14 Jul 2006 2:32 PM
Tav
mdscorp wrote:
> The last part of the T-SQL coding is I need to check for a file in the
> server , delete it before I attempt to re-create it with new data for
> the next day, any code for this ?

mdscorp,

You said, 'check for a file in the server'.  What sort of file?  Or do
you mean table.  If you mean table then you need this script in your
stored procedure:

if exists (select * from sysobjects where id =
object_id(N'[dbo].[<table name>]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[<table name>]

CREATE TABLE dbo.[<table name>]
    ...


-Tav.-

Tavis Pitt
Author
14 Jul 2006 2:35 PM
mdscorp
Tav wrote:
Show quote
> mdscorp wrote:
> > The last part of the T-SQL coding is I need to check for a file in the
> > server , delete it before I attempt to re-create it with new data for
> > the next day, any code for this ?
>
> mdscorp,
>
> You said, 'check for a file in the server'.  What sort of file?  Or do
> you mean table.  If you mean table then you need this script in your
> stored procedure:
>
> if exists (select * from sysobjects where id =
> object_id(N'[dbo].[<table name>]') and OBJECTPROPERTY(id,
> N'IsUserTable') = 1)
> drop table [dbo].[<table name>]
>
> CREATE TABLE dbo.[<table name>]
>     ...
>
>
> -Tav.-
>
> Tavis Pitt

Sorry, this store procedure creates a flat ascii file, before it
creates it I must check if already exists, delete it then create it
with new data.
Author
14 Jul 2006 2:36 PM
SQL Menace
take a look at xp_fileexist

master..xp_fileexist 'C:\Program Files\Microsoft SQL
Server\MSSQL\readme.txt'

This is undocumented so be careful

Denis the SQL Menace
http://sqlservercode.blogspot.com/



mdscorp wrote:
Show quote
> I am almost done with my HUGE stored procedure and iowe it mostly to
> contributions from all of you guys, THANKS
> The last part of the T-SQL coding is I need to check for a file in the
> server , delete it before I attempt to re-create it with new data for
> the next day, any code for this ?
>
> thanks all
Author
14 Jul 2006 2:51 PM
mdscorp
SQL Menace wrote:
Show quote
> take a look at xp_fileexist
>
> master..xp_fileexist 'C:\Program Files\Microsoft SQL
> Server\MSSQL\readme.txt'
>
> This is undocumented so be careful
>
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
>
>
> mdscorp wrote:
> > I am almost done with my HUGE stored procedure and iowe it mostly to
> > contributions from all of you guys, THANKS
> > The last part of the T-SQL coding is I need to check for a file in the
> > server , delete it before I attempt to re-create it with new data for
> > the next day, any code for this ?
> >
> > thanks all

ok, this works to let me know if the file exists, how do i kill it or
delete it or remove it ?
Author
14 Jul 2006 2:58 PM
SQL Menace
assuming you have permissions

master..xp_cmdshell 'del c:\deletethis.txt'

where c:\deletethis.txt' would be the path + filename

Denis the SQL Menace
http://sqlservercode.blogspot.com/



mdscorp wrote:
Show quote
> SQL Menace wrote:
> > take a look at xp_fileexist
> >
> > master..xp_fileexist 'C:\Program Files\Microsoft SQL
> > Server\MSSQL\readme.txt'
> >
> > This is undocumented so be careful
> >
> > Denis the SQL Menace
> > http://sqlservercode.blogspot.com/
> >
> >
> >
> > mdscorp wrote:
> > > I am almost done with my HUGE stored procedure and iowe it mostly to
> > > contributions from all of you guys, THANKS
> > > The last part of the T-SQL coding is I need to check for a file in the
> > > server , delete it before I attempt to re-create it with new data for
> > > the next day, any code for this ?
> > >
> > > thanks all
>
> ok, this works to let me know if the file exists, how do i kill it or
> delete it or remove it ?
Author
14 Jul 2006 3:46 PM
mdscorp
SQL Menace wrote:
Show quote
> assuming you have permissions
>
> master..xp_cmdshell 'del c:\deletethis.txt'
>
> where c:\deletethis.txt' would be the path + filename
>
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
>
>
> mdscorp wrote:
> > SQL Menace wrote:
> > > take a look at xp_fileexist
> > >
> > > master..xp_fileexist 'C:\Program Files\Microsoft SQL
> > > Server\MSSQL\readme.txt'
> > >
> > > This is undocumented so be careful
> > >
> > > Denis the SQL Menace
> > > http://sqlservercode.blogspot.com/
> > >
> > >
> > >
> > > mdscorp wrote:
> > > > I am almost done with my HUGE stored procedure and iowe it mostly to
> > > > contributions from all of you guys, THANKS
> > > > The last part of the T-SQL coding is I need to check for a file in the
> > > > server , delete it before I attempt to re-create it with new data for
> > > > the next day, any code for this ?
> > > >
> > > > thanks all
> >
> > ok, this works to let me know if the file exists, how do i kill it or
> > delete it or remove it ?

yes this wirks but i find the following problem.

the file created has a name that changes on a daily basis basically is
a name plus the date looking something like this:

"TextFile_07-14-06.txt"

so I do
SET @FileName =
REPLACE('\\server\upload\TextFile_'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-')

and carry on but when ty to use the xp_cmdshell to delete it as in

EXEC master..xp_FileExist @FileName, @FileExists out
IF @FileExists =1
    EXEC master..xp_cmdShell 'del ' + @FileName

i get an error because the command for the xp_cmdshell to work must be
embedded into single quotes all together...

any workaround this ?
Author
14 Jul 2006 3:50 PM
Aaron Bertrand [SQL Server MVP]
> SET @FileName =
> REPLACE('\\server\upload\TextFile_'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-')
>
> and carry on but when ty to use the xp_cmdshell to delete it as in
>
> EXEC master..xp_FileExist @FileName, @FileExists out
> IF @FileExists =1
> EXEC master..xp_cmdShell 'del ' + @FileName
>
> i get an error because the command for the xp_cmdshell to work must be
> embedded into single quotes all together...

How about,

DECLARE @cmd VARCHAR(255);
SET @cmd = 'del '+@FileName;
....
IF @FileExists = 1
    EXEC master..xp_cmdShell @cmd;
Author
14 Jul 2006 2:56 PM
Arnie Rowland
If you need to verify a file system file, you could use osql, or you could
attempt to bcp the file into a #Temp table and check the error -bit klutzy,
but could work 'down and dirty'.

--
Arnie Rowland*
"To be successful, your heart must accompany your knowledge."



Show quote
"mdscorp" <l***@mds-corp.com> wrote in message
news:1152887151.104212.266790@b28g2000cwb.googlegroups.com...
>I am almost done with my HUGE stored procedure and iowe it mostly to
> contributions from all of you guys, THANKS
> The last part of the T-SQL coding is I need to check for a file in the
> server , delete it before I attempt to re-create it with new data for
> the next day, any code for this ?
>
> thanks all
>

AddThis Social Bookmark Button