Agent Job Check for file existence

  • I am trying to have my first step in a job check for file existence. I was trying to explore using cmdexec in step 1 and doing something similar below, but the if exists is not working. I have tried fully qualifying the path too. Should I try something else to check for file existence?

    T:\ER>if exists test.txt exit

    test.txt was unexpected at this time.

  • I use xp_fileexist It is probably undocumented (which will freak out some purists) but it works well and is probably used by a number of underlying SQL Server processes so I doubt it will really ever go away.

    Of course you could write your own little CLR function if you wanted to but why bother.

    The probability of survival is inversely proportional to the angle of arrival.

  • I actually found a good solution using powershell. You can use powershell as a type in the job properties and use this code.

    if ( (Test-Path -Path "C:\temp\testdb.bak") -eq $false)

    { Write-error -Message "Error: testdb.bak does not exist."}

    http://blogs.solidq.com/EnriquePuigNouselles/Post.aspx?ID=5&title=Checking%20existing%20files%20with%20Powershell%20and%20SQL%20Server%20Agent]

  • sturner (6/17/2011)


    I use xp_fileexist It is probably undocumented (which will freak out some purists) ...

    Not sure if I'm a purist but your post freaked me out a little.

    PowerShell +10,000

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

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

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