Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 29th, 2005, 11:25 AM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default File System Object question

Hello All,

I have a script that checks a database for records and saves each record set as an asp file. The script uses the "FileSystemObject" to do this and is run once a month, so I can create an archive. (I have been asked to do it this way for SEO reasons)

In my database there is a field for date, and this date is entered as dd/mm/yyyy. My question is if its possible to have the FileSystemObject create a folder for each month and save the asp files into it?

Heres what my code looks like:

Code:
<%

Dim Connection, Recordset
Dim sSQL, sConnString
Dim sFSO
Dim sThisPage
Dim TempPath
Dim f

'declare SQL statement that will query the database
sSQL = "SELECT * FROM DeHavillandNews"

'define the connection string, specify database 
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ 
"Data Source=" & Server.MapPath("casinonewsxml.mdb") 


'create an ADO connection and recordset
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
connection.Open sConnString

'Open the recordset object, execute the SQL statement
recordset.Open sSQL, connection

'create an instance of the FileSystemObject
SET sFSO = Server.CreateObject("Scripting.FileSystemObject")

'Loop through all your records in the database
DO WHILE NOT recordset.EOF

'Map a path to you unique file name based on the id in the database
sThisPage = Server.Mappath("htmlpages/"& recordset("ItemID") &".asp")

'Check if the file exists already and delete it
TempPath=sThisPage 
IF sFSO.FileExists(TempPath) = TRUE THEN
sFSO.DeleteFile(TempPath)
END IF

'Create a new file with the same name and fill it with content
Set f = sFSO.CreateTextFile(sThisPage, true)
f.write "<p class='newshead'>"& vbcrlf
f.write recordset("Heading") & vbcrlf
f.write "</p><p class='newstext'>"& vbcrlf
f.write recordset("Contents") & vbcrlf
f.write "</p>"& vbcrlf

'Begin the loop
recordset.MoveNext
LOOP

%>
Thanks guys - I really appreciate any input or help.

Reply With Quote
  #2 (permalink)  
Old December 8th, 2005, 04:14 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Absolutely! (In the following, you might have to make some adjustments for trailing back slashes, etc.)
I tried to make all the changes I made boldface, but read carefully...
I substituted .WriteLine for .Write in locations where it was clear that the contents of what was being written would not have any linefeeds of carriage returns. So I left ("Heading") and ("Contents") as .Write.
Code:
    Dim cnctn, rs             ' » Both Connection and Recordset are words
    Dim sSQL, sConnString     '   naming specific items in the data access
    Dim sThisPage             '   object models;  it’s good idea to stay
    Dim TempPath              '   away from such names...
    Dim fso
    Dim Mnth
    Dim f
    Dim s   ' Temp string
    Dim x   ' Throw away variable to catch the created folder
            ' when using sFSO.CreateFolder() As Folder

    ' Declare SQL stmnt that will query the db
    sSQL = "SELECT * FROM DeHavillandNews"

    ' Define the connection string, specify db driver
    ' and the location of db
    sConnString  = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ 
                   "Data Source=" & Server.MapPath("casinonewsxml.mdb") 


    ' Create an ADO connection & recordset
    Set cnctn = Server.CreateObject("ADODB.Connection")
    Set rs    = Server.CreateObject("ADODB.Recordset")

    ' Open the connection to the db
    cnctn.Open sConnString

    ' Open the recordset, execute the SQL stmnt
    rs.Open sSQL, cnctn

    ' Create an instance of the FileSystemObject
    Set fso = Server.CreateObject("Scripting.FileSystemObject")

    ' Loop through all the records in the db
    Do While Not rs.EOF

        ' Get the month from the recordset
        Mnth = Mid(rs("DtFld"), 4, 2)   ' (I’m assuming a field name...)
        ' If DtFld is a true date field, use VB’s date manipulation
        ' Mnth = CStr(Month(rs("DtFld")))

        ' Check for the existence of the folder.
        ' Create it if non-existent.
        s = Server.MapPath("htmlpages/a.txt"      ' Generate file path & name
        s = Left(s, Len(s) - 5)                   ' Strip the file name
        If Not fso.FolderExists(s) Then           ' Look for it
            Set x = fso.CreateFolder("s" & Mnth)  ' Create it
            Set x = Nothing                       ' Release the reference
        End If

        ' Map a path to a unique file name based on the id in the db
        sThisPage = Server.MapPath("htmlpages/" & Mnth & "/" & rs("ItemID") & ".asp")

        ' Check whether the file exists already;
        ' delete it if so.
     '  TempPath = sThisPage    ' Doesn’t appear that TempPage differs from sThisPage . . .
        If fso.FileExists(sThisPage) Then
            fso.DeleteFile(sThisPage)
        End If

        ' Create a new file & fill it with content
        Set f = fso.CreateTextFile(sThisPage, True)

        f.WriteLine "<p class='newshead'>"
        f.Write     rs("Heading") & vbCrLf
        f.WriteLine "</p><p class='newstext'>"
        f.Write     rs("Contents") & vbCrLf
        f.WriteLine "</p>"
        f.Close
        Set f = Nothing

        rs.MoveNext

    Loop
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File System Object kumar_rajeshk Pro VB 6 7 April 28th, 2004 01:35 AM
File System Object Permissions kilika Classic ASP Basics 2 August 6th, 2003 09:23 AM



All times are GMT -4. The time now is 10:01 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.