Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Get file size Expand / Collapse
Author
Message
Posted Wednesday, December 20, 2006 7:16 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 17, 2010 3:19 AM
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

Post #331794
Posted Wednesday, December 20, 2006 6:57 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 36,995, Visits: 31,514
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #332008
Posted Thursday, December 21, 2006 12:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 17, 2010 3:19 AM
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.

Post #332040
Posted Thursday, December 21, 2006 6:59 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 36,995, Visits: 31,514

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #332145
Posted Thursday, December 21, 2006 10:51 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 36,995, Visits: 31,514

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #332418
Posted Thursday, December 28, 2006 4:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 28, 2006 4:33 AM
Points: 1, Visits: 1

That's running for local files only.

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

 

Post #333121
Posted Thursday, December 28, 2006 7:33 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 36,995, Visits: 31,514
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #333145
Posted Thursday, February 19, 2009 8:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 16, 2009 6:37 AM
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!
Post #660420
Posted Thursday, February 19, 2009 8:49 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:13 PM
Points: 36,995, Visits: 31,514
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #660455
Posted Friday, February 20, 2009 1:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 16, 2009 6:37 AM
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:'

)

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

)

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

)

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

insert into
#commandLineOutput
(
row

)
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

)
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]

)
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
Post #661714
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse