Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get file size


Get file size

Author
Message
Rajesh-388763
Rajesh-388763
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 10

Hi,

 

          I need to get the file size in bytes for any given file name inside a stored procedure, without using master..xp_filesize. Can somebody please help me on this.

The path and the file name is known to me.

 

Regards,

Rajesh Khubchandani


Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44871 Visits: 39851
Sure... right after you tell us why you can't use xp_filesize so we don't recommend something similar...

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Rajesh-388763
Rajesh-388763
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 10

I cannot use xp_filesize because the i cannot make use of xpFileUtil.dll file into the sql server version which I am using. this is the restriction which has been put on me for the development. The reason which i can think of is that you need to pay some $ to access that dll.

 

This is the only reason which i can think of for restriction of the using xpFileUtil.dll.

so now can you please help me for the same.


Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44871 Visits: 39851

Ok, thanks... isn't that the way?  Company buys something and that won't let you use it which forces you to revert to undocumented features in SQL Server...

This will work in SQL Server 2000 and SQL Server 2005 and you don't need SA privs to use it... Master.dbo.xp_GetFileDetails comes with SQL Server...

EXEC Master.dbo.xp_GetFileDetails 'pathandfilenamehere'

...or...

EXEC Master.dbo.xp_GetFileDetails 'uncpathandfilenamehere'

 



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44871 Visits: 39851

Rajesh!

Did you try it or what?



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
HEUZE Henri
HEUZE Henri
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1

That's running for local files only.

When you try using UNC Path, access is denied or the authentication failed.

 


Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44871 Visits: 39851
Yes, it will fail if your server isn't setup to "see" the path.  The SQL Server "service" must be started as a power user that can "see" the UNC path you give it.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
tsuga
tsuga
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
I see this hasn't been active for awhile, but I wanted to just thank you Jeff for the insight on that. It sure helped me!
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44871 Visits: 39851
Outstanding... thank you very much for taking the time to stop by and make a comment. All of us do this stuff for free and our only reward is comments such as yours. Thanks.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
tsuga
tsuga
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
Jeff,

What I was trying to do was find a means of getting a file listing so I could scedule a job to delete files that were say two weeks or more old. So because of your assistance, I was able to come up with this stored procedure that basically recreates the "dir" command to a table.

Now that's acting stable, I can now move on to making a procedure that references this one to delete files based on date, size, name, or whatever.

Thanks again and have a great weekend!

Tony

create procedure [dbo].[sproc_utility_Dir]

      (
            @path                     varChar ( 1000 ) = 'c:'

      Wink

as

begin

declare         
            @isDirectory               bit,
            @name                     varChar ( 0255 ),
            @pathFile                  varChar ( 1255 ),
            @command                  nvarChar ( 4000 ),
            @result                     int            

set nocount on

create table   
            #commandLineOutput
      (
            row               varChar ( 0400 ),
            flag            bit
         
      Wink

create table #detail
      (
            [isDirectory]      bit,
            [path]            varChar ( 1000 ),
            [name]            varChar ( 0255 ),
            [pathFile]         varChar ( 1255 ),
            [Alternate Name]   varChar ( 0255 ),
            [Size]            int,
            [Creation Date]      varChar ( 0010 ),
            [Creation Time]      varChar ( 0010 ),
            [Last Written Date]   varChar ( 0010 ),
            [Last Written Time]   varChar ( 0010 ),
            [Last Accessed Date]varChar ( 0010 ),
            [Last Accessed Time]varChar ( 0010 ),
            [Attributes]      int

      Wink

select      
            @command = 'dir "' + @path + '\*.*" /b /a:d /o:n',
            @result = 0

insert into
            #commandLineOutput
      (
            row
      
      Wink
execute         @result = master.dbo.xp_cmdshell
                  @command


if (@result <> 0)
   begin
   select      
            [isDirectory],
            [path],
            [name],
            [pathFile],
            [Alternate Name],
            [Size],
            [Creation Date],
            [Creation Time],
            [Last Written Date],
            [Last Written Time],
            [Last Accessed Date],
            [Last Accessed Time],
            [Attributes]
      
   from      
            #detail   
   where
            0 > 1

   drop table   #commandLineOutput
   drop table   #detail
            
   return      @result
   
   end
               
update      
            #commandLineOutput
set         
            flag = 1
where      
            flag is null            

select      
            @command = 'dir "' + @path + '\*.*" /b /a:-d /o:n',
            @result = 0

insert into
            #commandLineOutput
      (
            row
      
      Wink
execute         @result = master.dbo.xp_cmdshell
               @command
               
if (@result <> 0)
   begin
   select      
            [isDirectory],
            [path],
            [name],
            [pathFile],
            [Alternate Name],
            [Size],
            [Creation Date],
            [Creation Time],
            [Last Written Date],
            [Last Written Time],
            [Last Accessed Date],
            [Last Accessed Time],
            [Attributes]
   
   from      
            #detail   
   where
            0 > 1

   drop table   #commandLineOutput
   drop table   #detail
            
   return      @result
   
   end
   
update      
            #commandLineOutput
set         
            flag = 0
where      
            flag is null            

delete
from      
            #commandLineOutput
where      
            row is null
   
declare         looping            cursor
   for   select   [flag],
            [row]
   from      #commandLineOutput

open         looping
fetch next from   looping
   into       @isDirectory,
            @name

while (@@fetch_status = 0)
   begin   
   select      @pathFile = @path + '\' + @name
   
   insert into #detail
         (
            [Alternate Name],
            [Size],
            [Creation Date],
            [Creation Time],
            [Last Written Date],
            [Last Written Time],
            [Last Accessed Date],
            [Last Accessed Time],
            [Attributes]
               
         Wink         
   execute @result = master.dbo.xp_GetFileDetails
                  @pathFile
   
   if (@result = 0)
      begin
      if (@isDirectory = 0)
         begin
         update
            #detail
         set
            [isDirectory] = @isDirectory,
            [path] = @path,
            [name] = @name,
            [pathFile] = @pathFile,
            [Creation Time] = replicate('0', 6 - len([Creation Time])) + [Creation Time],
            [Last Written Time] = replicate('0', 6 - len([Last Written Time])) + [Last Written Time],
            [Last Accessed Time] = replicate('0', 6 - len([Last Accessed Time])) + [Last Accessed Time]
         where
            [isDirectory] is null
         and   [path] is null
         and   [name] is null
         and   [pathFile] is null

         end
         
      else if (@isDirectory = 1)
         begin
         update
            #detail
         set
            [isDirectory] = @isDirectory,
            [path] = @path,
            [name] = @name,
            [pathFile] = @pathFile + '\',
            [Creation Time] = replicate('0', 6 - len([Creation Time])) + [Creation Time],
            [Last Written Time] = replicate('0', 6 - len([Last Written Time])) + [Last Written Time],
            [Last Accessed Time] = replicate('0', 6 - len([Last Accessed Time])) + [Last Accessed Time]
         where
            [isDirectory] is null
         and   [path] is null
         and   [name] is null
         and   [pathFile] is null
         
         end
         
      end
         
   fetch next from   looping
   into       @isDirectory,
            @name
      
   end

close         looping
deallocate      looping
      
select      
            [isDirectory],
            [path],
            [name],
            [pathFile],
            [Alternate Name],
            [Size],
            convert(varChar ( 10 ), cast([Creation Date] as dateTime), 101) as [Creation Date],
            subString([Creation Time], 1, 2) + ':' + subString([Creation Time], 3, 2) + ':' + subString([Creation Time], 5, 2) as [Creation Time],
            convert(varChar ( 10 ), cast([Last Written Date] as dateTime), 101) as [Last Written Date],
            subString([Last Written Time], 1, 2) + ':' + subString([Last Written Time], 3, 2) + ':' + subString([Last Written Time], 5, 2) as [Last Written Time],
            convert(varChar ( 10 ), cast([Last Accessed Date] as dateTime), 101) as [Last Accessed Date],
            subString([Last Accessed Time], 1, 2) + ':' + subString([Last Accessed Time], 3, 2) + ':' + subString([Last Accessed Time], 5, 2) as [Last Accessed Time],
            [Attributes]
   
from      
            #detail

order by
            [isDirectory] desc,
            name asc
            
drop table      #commandLineOutput
drop table      #detail

end
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search