File Exists check Code Error

  • Hi Folks

    please let me know what i am missing from following code...and when

    i wnat to del this file then I am running following code which is

    not working either...please advice..

    Working Script.....................start here.

    DECLARE @chkfile INT

    DECLARE @File varchar(255)

    DECLARE @dbname VARCHAR(50)

    exec master..xp_fileexist 'C:\db_me.bak', @chkfile output

    IF @chkfile = 1 -- The file exists

    PRINT 'File does exist'

    --print @File

    ELSE

    PRINT 'File does not exist'

    Working Script.....................End here.

    NOT Working Script.....................start here.

    DECLARE @chkfile INT

    DECLARE @File varchar(255)

    DECLARE @dbname VARCHAR(50)

    SET @dbname = 'DB_me'

    SET @File = '''C:\'+@dbname+'.bak'''

    exec master..xp_fileexist @File, @chkfile output

    IF @chkfile = 1 -- The file exists

    PRINT 'File does exist'

    --print @File

    ELSE

    PRINT 'File does not exist'

    NOT Working Script.....................End here.

    Delete File.................

    exec master..xp.cmdshell 'del C:\db_me.bak'

    End Here.......................................

    Thanx in Advance

    SqlIndia

  • Hi folks

    I have solved the file exists code part. The only different was that I was putting quotés around @file variable..(SET @File = '''C:\'+@dbname+'.bak''') which should be like this (SET @File = 'C:\'+@dbname+'.bak')...

    But couldn't solve yet delete the file part.. could any one please look into please..I am getting following error:

    Start Here...............................................................

    DECLARE @chkfile INT

    DECLARE @File varchar(255)

    DECLARE @delFile varchar(255)

    DECLARE @dbname VARCHAR(50)

    SET @dbname = 'DB_2_3'

    SET @File = 'C:\'+@dbname+'.bak'

    SET @delFile = 'del'+@dbname+'.bak'

    print @File

    exec master..xp_fileexist @File, @chkfile output

    IF @chkfile = 1 -- The file exists

    exec master..xp.cmdshell @delFile

    --print @File

    ELSE

    PRINT 'File does not exist'

    End here........................................................................

    ERROR:

    Server: Msg 7202, Level 11, State 2, Line 18

    Could not find server 'master' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.

     

    SqlIndia

  • Hi Folks

    I have solved this..It was my mistake.. It was like this:

    "exec master..xp.cmdshell @delFile"

    should be like this:

    "exec master..xp_cmdshell @delFile, NO_OUTPUT"

     

    SqlIndia

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply