ASP File System Object Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vdown
    New Member
    • Feb 2007
    • 3

    ASP File System Object Question

    Im no ASP expert but have inherited a site that scans a load of folders for PDFs using the file system object.

    All the PDFs are held in a time based heriarchical folder structure i.e. Year\Month\Date (in format 150207, thats UK date).

    The first page lists the current month with all the days that have folders. In the format (15 February 2007) a function converts the folder name to this format. All the folders are then put in an array and sorted in date order,

    I want to be able to exclude any days in the future. That is its the 15th of Feb today so if there was a folder for the 16th I dont want it to appear until the 16th.

    This would be easy with a SQL statement but I cant find any relavent reference to do it with a File System Array. As the only way of identifying the date of the PDFs is from their parent folder name.
  • jhardman
    Recognized Expert Specialist
    • Jan 2007
    • 3405

    #2
    it may be easiest to pull up all of the files with the FSO, then only display the files you want. i.e.:
    Code:
    if datediff("d", docDate, currentDate) >= 0 then
       'code to display the file
    else
       'ignore future files
    end if
    I can never remember date function syntax, but it is pretty close to that.
    The "else" clause can obviously be left out.
    If you post your FSO code I might be able to give other suggestions , maybe filter it out as you load

    Comment

    • vdown
      New Member
      • Feb 2007
      • 3

      #3
      Here the code of the first page, It automatically goes into a specific folder based on the query string in the address bar.

      There is nothing too relavent in the include file either.

      Thx for any help.
      Code:
      <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
      <!--#include file="shared_functions.asp" -->
      <%
      	On Error Resume Next
      	
      	' Now to the Runtime code:
      	Dim strPath   'Path of directory to show
      	Dim strCurrentFolderPath
      	Dim objFSO    				'FileSystemObject variable
      	Dim objArchiveFolder 		'Folder variable
      	Dim objCurrentFolder		'Folder for the current month
      	Dim objItem   				'Variable used to loop through the contents of the folder
      	Dim NoFiles	  				'The number of files in a folder
      	Dim varChain				'Hold the Chain
      	Dim SortedFiles				'Sorted files
      	Dim DisplayName				'
      	Dim TargetMonth 
      	Dim TargetYear
      	Dim FolderDate
      	Dim CorrectedDate2
      				'
      	' Create our FSO
      	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
      	
      	'FolderDate=Date()
      
      	'Done indirectly to prevent hacks
      	Function getChain()
      	
      	(not relavent)
      			End Function
      	
      	Function AutoCorrectDate(FOLDERNAME)
      		temp = FOLDERNAME
      		'response.Write(temp)
      		'take the days from the beginning
      		part1 = mid(temp,1,2)
      		'Add the / in between to make 101006 to 10/
      		part1 =  part1 & "/"
      		
      		'add a slash between the month and year
      		part2 = mid(temp,3,2)
      		part2 =  part2 & "/20"
      		
      		'Finally add a the rest of the year on the end
      		part3= mid(temp,5,2)
      		
      		AutoCorrectDate = part1 & part2 & part3
      	'response.write(AutoCorrectDate)
      	End Function
      	' NOTE: As currently implemented, this needs to end with the \
      	
      	
      	If Request.QueryString("archive") <> "" then
      		
      		TargetMonth  = Request.QueryString("archive")
      		
      		TargetYear   = Request.QueryString("year")
      		
      		strPath = ".\" & getChain & "\" '& year(now) & "\"
      			
      			if TargetYear <> "" then
      				'If it's a Year
      				
      				strCurrentFolderPath = ".\" & getChain &"\" & TargetYear 
      			
      			Elseif targetmonth <> "" and TargetYear <> "" then
      				'If it's a month
      				
      				strCurrentFolderPath = ".\" & getChain &"\" & targetYear & "\" & TargetMonth & "\"
      				
      			Else 
      				
      				strCurrentFolderPath = ".\" & getChain &"\" & year(now) & "\" & Monthname(month(now)) & "\"
      				
      			End if
      				
      	Else
      		TargetMonth = MonthName(Month(now))
      		
      		strPath = ".\" & getChain '& "\" & year(now) & "\"
      	
      		strCurrentFolderPath = ".\" & getChain &"\" & year(now) & "\" & TargetMonth & "\"
      	
      	End if
      	
      	
      	
      	'Sub HandleError
      		'cDate("10/10/06")
      		'Response.Write("A runtime Error has occured")
      	'End Sub
      	
      	
      	
      	'Make sure a folder exists, if it doesn't Use last months
      	'if objFSO.FolderExists(strCurrentFolderPath) = False then
      		'strCurrentFolderPath = ".\(removed)\" & year(now) & "\" & MonthName(Month(now)-1) & "\"
      	'end if	
      	' Get a handle on our folder
      	Set objArchiveFolder = objFSO.GetFolder(Server.MapPath(strPath))
      	Set objCurrentFolder = objFSO.GetFolder(Server.MapPath(strCurrentFolderPath))
      	
      	'if objFSO.GetFolder(Server.MapPath(strCurrentFolderPath)) then
      	'	newPath = ".\" & getChain &"\" & year(now) & "\" & MonthName(7) & "\"
      	'	
      	'	set objCurrentFolder = objFSO.GetFolder(Server.MapPath(newPath))
      	'end if
      	
      	set SortedFiles	= server.CreateObject("ADODB.recordset")
      	
      	With SortedFiles.fields
      		.Append "Name", 200, 200
      		.Append "DateLastModified", 7
      		.Append "DateCreated", 7
      		.Append "Size", 3
      		.Append "FilesCount", 3
      		.Append "FoldersCount", 3
      	End With
      	
      	SortedFiles.open()
      	'SortedFiles.filter = "size < 50000"
      	For Each objItem in objCurrentFolder.SubFolders
      		SortedFiles.addnew
      		
      		SortedFiles("name") = objItem.name
      		SortedFiles("DateLastModified") = objItem.DateLastModified
      		SortedFiles("DateCreated") = objItem.DateCreated
      		SortedFiles("Size") = objitem.size
      		SortedFiles("FilesCount") = objItem.files.count
      
      		SortedFiles.update
      	next
      	
      	'response.write(sortedfiles)
      	'response.Write(folderdate)
      	SortedFiles.Sort = "Name DESC"
      %>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Click: UK &amp; Ireland Press adverts</title>
      <link href="press.css" rel="stylesheet" type="text/css" />
      </head>
      
      <body>
      <div id="wrapper">
      <!--	<div id="header">
      		<h1>Press Ads</h1>
      	</div> -->
      	<div id="main_content">
      		<img src="banner/banner.gif" height="80" width="776" alt="Press Adverts" />
      		<!--Menu begin -->
      		<!--#include file="menu.html" -->
      		<!--Menu end -->
      		<!-- Cut and paste this section as needed, multiple "listing"s go in a listings
      		Cut and paste date, week divs as required.
      		<div class="listings">
      			<div class="listing">
      				<div class="bullet"></div>
      				<div class="paper">The Sun</div>
      				<div class="categories">Sony Camcorders</div>
      				<div class="link">Save Millions </div>
      			</div>
      		</div> -->
      <!--Don not replace/paste/edit any content above this line-->
      		<!--Week and year begin -->
      		<!--<h2>Wk 49 <span class="little">2005/2006</span></h2> -->
      		<!--Week and year end -->
      		<!--Date begin -->
      		<div class="date">
      			<b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b>
      			<%= TargetMonth & " " & Year(now)%><!--Above & below b elements provide the rounded corners-->
      			<b class="rtop"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b>		
      		</div>
      		<!--Date end -->
      		<!--Listings begin -->
      		<div class="listings">
      			<table class="listing">
      				<tr>
      					<th>File Name:</th>
      					<th>Number of Ads</th>
      					<th>Date Added:</th>
      					<th>Total Size of PDF's</th>
      			  </tr>
      				<%
      				
      				
      				' Now that I've done the SubFolders, do the files!
      				
      				Do While not SortedFiles.EOF 
      				'and correcteddate2 >= folderdate
      				'response.write(SortedFiles.Fields("name").value)
      				'and AutoCorrectDate <= FolderDate
      
      				'Check for non numeric file names
      				If isnumeric(SortedFiles.Fields("name").value) then
      				correctedDate = AutoCorrectDate(SortedFiles.Fields("name").value)
      				
      				%>
      				<tr>
      					<td class="paper">
      						<img src="images/folder.gif" alt="folder icon" />
      						<%= "<a href=" & chr(34) & "Fresh_press.asp?date=" & SortedFiles.Fields("name").value & "&chain=" & varChain & chr(34)& ">"  & FormatDateTime(correctedDate,1) & "</a>"%>
      					</td>
      				  	<td><%= SortedFiles.Fields("FilesCount").value %></td>
      					<td><%= FormatDateTime(SortedFiles.Fields("DateCreated").value,2) %></td>
      					<td><%=  BtyeConversion(SortedFiles.Fields("size").value) %></td>
      			  </tr>
      				<%
      				End if
      				SortedFiles.MoveNext
      				Loop 'objCurrentFolder 
      			%>
      		  </table>
      		</div>
      		
      		<!----------------------------------------- Archive Starts here ---------------------------------------------->
      		<hr />
      		<div class="date">
      			<b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b>
      			Press Ads Archive
      			<b class="rtop"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b>		
      		</div>
      		<div class="listings">
      			<table class="listing">
      				<tr>
      					<th>File Name:</th>
      					<th>Number of Days:</th>
      					<th>Date Created:</th>
      					<th>File Type:</th>
      			  </tr>
      				<%
      					' Now that I've done the SubFolders, do the files!
      					For Each objItem In objArchiveFolder.SubFolders
      					
      					'Test to see if it's a year
      						If isNumeric(objItem.Name) then					
      				%>
      				<tr>
      					<td class="paper"><img src="images/folder.gif" alt="folder" /><%= "<a href=" & chr(34) & "Fresh_press_archive.asp?chain=" & varchain & "&amp;" &  "year=" & objItem.Name & "&amp;" & chr(34) & ">" & objItem.Name %></a></td>
      				  	<td><%= objItem.SubFolders.count %></td>
      					<td><%= FormatDateTime(objItem.DateCreated,2) %></td>
      					<td><%= objItem.Type %></td>
      			  </tr>
      				<%
      						End if
      					
      					Next 'objItem
      					
      					' All done!  Kill off our object variables.
      					Set objItem = Nothing
      					Set objArchiveFolder = Nothing
      					Set objFSO = Nothing
      				%>
      		  </table>
      		</div>
      	</div>
      </div>
      </body>
      </html>
      %>

      Comment

      • jhardman
        Recognized Expert Specialist
        • Jan 2007
        • 3405

        #4
        Ok, I would insert 2 lines. First:
        Code:
        	'Check for non numeric file names
        	If isnumeric(SortedFiles.Fields("name").value) then
        	correctedDate = AutoCorrectDate(SortedFiles.Fields("name").value)
        		'next line is the one I insert
        		if dateDiff("d", correctedDate, now) < 0 then
        and then add an extra end if:
        Code:
        				<%
        	End if 		'this is the line I added
        	End if
        	SortedFiles.MoveNext
        	Loop 'objCurrentFolder
        the dateDiff function is the one that checks, and it just subtracts one date from the other. The ony problem is, I never remember which way it works, so if this does the opposite of what you want, you know you need to switch it to " > 0"

        Comment

        • vdown
          New Member
          • Feb 2007
          • 3

          #5
          Thats it, thanks very much.

          You were right switched the less than to the greater than and it worked.

          Comment

          Working...