Search code examples
sql-server-2008-r2bcpfileinfo

Check file content SQL Server 2008 R2


We have an interface, which creates three files everyday with following name format:

  1. FileAYYYYMMDD (Example: FileA20170925)
  2. FileBYYYYMMDD (Example: FileB20170925)
  3. FileCYYYYMMDD (Example: FileC20170925)

Someone manually needs to check to make sure whether these files are empty or not. I want to automate this step.

What I like to know: Is there a way to determine via SQL query or SQL job whether these files are empty (Yes or No is good enough)?


Solution

  • You should be able to get the filesize with the stored procedure spFileDetails from this site.

    If the filesize is 0 then the file is empty. Here's a copy of the stored procedure. Thanks to "Phil Factor"

    /****** Object:  StoredProcedure [dbo].[spFileDetails]    Script Date: 03/28/2007 15:28:15 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[spFileDetails]
    @Filename VARCHAR(100)
    
    /*
    spFileDetails 'c:\autoexec.bat'
    */
    AS
    DECLARE @hr INT,         --the HRESULT returned from 
           @objFileSystem INT,              --the FileSystem object
           @objFile INT,            --the File object
           @ErrorObject INT,        --the error object
           @ErrorMessage VARCHAR(255),--the potential error message
           @Path VARCHAR(100),--
           @ShortPath VARCHAR(100),
           @Type VARCHAR(100),
           @DateCreated datetime,
           @DateLastAccessed datetime,
           @DateLastModified datetime,
           @Attributes INT,
           @size INT
    
    
    
    SET nocount ON
    
    SELECT @hr=0,@errorMessage='opening the file system object '
    EXEC @hr = sp_OACreate 'Scripting.FileSystemObject',
                                           @objFileSystem OUT
    IF @hr=0 SELECT @errorMessage='accessing the file '''
                                           +@Filename+'''',
           @ErrorObject=@objFileSystem
    IF @hr=0 EXEC @hr = sp_OAMethod @objFileSystem,
             'GetFile',  @objFile out,@Filename
    IF @hr=0 
           SELECT @errorMessage='getting the attributes of '''
                                           +@Filename+'''',
           @ErrorObject=@objFile
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'Path', @path OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'ShortPath', @ShortPath OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'Type', @Type OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'DateCreated', @DateCreated OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'DateLastAccessed', @DateLastAccessed OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'DateLastModified', @DateLastModified OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'Attributes', @Attributes OUT
    IF @hr=0 EXEC @hr = sp_OAGetProperty 
                 @objFile, 'size', @size OUT
    
    
    IF @hr<>0
           BEGIN
           DECLARE 
                   @Source VARCHAR(255),
                   @Description VARCHAR(255),
                   @Helpfile VARCHAR(255),
                   @HelpID INT
    
           EXECUTE sp_OAGetErrorInfo  @errorObject, 
                   @source output,@Description output,
                                   @Helpfile output,@HelpID output
    
           SELECT @ErrorMessage='Error whilst '
                                   +@Errormessage+', '
                                   +@Description
           RAISERROR (@ErrorMessage,16,1)
           END
    EXEC sp_OADestroy @objFileSystem
    EXEC sp_OADestroy @objFile
    SELECT [Path]=  @Path,
           [ShortPath]=    @ShortPath,
           [Type]= @Type,
           [DateCreated]=  @DateCreated ,
           [DateLastAccessed]=     @DateLastAccessed,
           [DateLastModified]=     @DateLastModified,
           [Attributes]=   @Attributes,
           [size]= @size
    RETURN @hr