Advertisement

07.27.2008 at 04:22PM PDT, ID: 23599427
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Using SQL Query to assess size of a folder, is it doable?

Asked by vision_staff in SQL Query Syntax, SQL Server 2005

Tags:

Hi everyone,

I have developed a SQL script to check the integrity of a particular software.
The software basically backs up some files, delete it from the server and set the database entry to 'Archive'.

Sometimes when the program is acting weird, it will back up the file, delete the file from the server, but failed to set it as archived. Therefore, when another program call for the files to be retrieved, it would try to look for the files on the server and would obviously fail to retrieve the file.

The script that I developed read from the SQL Server database for the status of the files, if it is on the server, check if the file is really there. If not, show a list of these entries and we would check manually if they have been properly archived. The script just checks for the existence of the directory file called ps.dir as the name of the files will vary, but they will be contained in this directory file.

I would like to extend the capability of the script to be able to check the size of a folder.
My questions would be whether this is doable and how do I do it using just SQL query.
I know it might be easier to create a VBScript to do this, but the management preferred using SQL Agent to automate the running of the query, rather than scheduling a task to run scripts.
However, if it proves to be too difficult, I'll probably use a script anyway.

I'd like to start a fresh script with the following algorithm:

Connect to Database
Read an Entry
if this entry status is on server (field called [Media Type] is "Image Server")
           grab the path of the file  (supplied in a field called image_path)
           Check the size of the folder
           if this folder is smaller than x (minimum size of the files)
                     List this entry in a table
           end if
end if
Display list of entries that has smaller files.
           
I've also attached my script in the code snippet section.
Apology if it is badly written.

Thank you very much for your attention.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
declare @Rows int
declare @exam_id int
declare @patient_id int
declare @image_path varchar(1000)
declare @media_type varchar(25)
declare @tab table(i int identity, exid int, patid int, impa varchar(1000), medty varchar(25))
 
/***************************************************/
/** GET NUMBER OF ROWS *****************************/
/***************************************************/
Insert into @Tab
SELECT exam_id, pat_id, image_path, [media type] from Exam where pat_id > 0 order by exam_id desc
select @Rows = @@ROWCOUNT
/*print @Rows*/
 
/***************************************************/
/** CHECK FOR FILE EXISTENCE ***********************/
/***************************************************/
 
declare @returnBit bit
select @returnBit = 0
 
/***************************************************/
/* while there are rows:                           */
/* check if media type is image server             */
/* if it is, check the existence of file1          */
/* if it exist, set returnBit as 1 else 0          */
/***************************************************/
while @Rows > 0
Begin
create table #a (s varchar(1000))
declare @Path varchar(128) ,
@FileName varchar(128)
 
select @exam_id = exid, @patient_id = patid, @image_path = impa, @media_type = medty
from @tab where i = @Rows
 
/*DEBUG*/
/*print @exam_id*/
/*print @patient_id*/
/*print @image_path*/
/*print @media_type*/
 
if @media_type = 'Image Server' 
begin 
select @Path = 'C:\images\' + replace(@image_path,'/', '\'),
@FileName = 'ps.dir'
 
declare @cmd varchar(1000)
 
select @cmd = 'dir /B ' +'"'+ @Path + @FileName+'"'
print @cmd
insert #a exec master..xp_cmdshell @cmd
/*exec master..xp_cmdshell @cmd*/
select @returnBit = CASE when exists (select * from #a where s = @FileName) then 1 else 0 End
print @returnBit
 
/***************************************************/
/** SET MEDIA TYPE TO ARCHIVE IF FILE NOT FOUND   **/
/***************************************************/
/* if returnBit is 0 (if file1 is not found then)  */
/* insert into table @tab2       */
/***************************************************/
declare @tab2 table(i int identity, exam_id int, pat_id int, image_path varchar(1000), media_type varchar(25), fullPath varchar
 
(1000), found bit)
if @returnBit = 0
begin
insert into @tab2 (exam_id, pat_id, image_path, media_type, fullPath, found) values (@exam_id, @patient_id, @image_path, @media_type, 
 
@cmd, @returnBit)
/*set [Media Type] = 'Archive'*/
end /*end if returnBit == 0*/
end /*end if media type == image server*/
 
/* decrement counter */
SET @Rows = @Rows - 1
/* drop temporary table */
drop table #a 
END /* End while there are still rows to process */
 
select * from @tab2
[+][-]07.27.2008 at 06:07PM PDT, ID: 22100215

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, SQL Server 2005
Tags: SQL Server Query
Sign Up Now!
Solution Provided By: EugeneZ
Participating Experts: 2
Solution Grade: A
 
 
[+][-]07.27.2008 at 06:16PM PDT, ID: 22100239

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.27.2008 at 09:32PM PDT, ID: 22100701

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07.27.2008 at 09:57PM PDT, ID: 22100749

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.28.2008 at 03:30AM PDT, ID: 22101901

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.17.2008 at 02:54PM PDT, ID: 22745663

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]10.25.2008 at 07:13AM PDT, ID: 22803334

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628