Content Management with the FSO
August 14, 2000
By now, you should have a good taste of the FSO and its methods and
properties. Let's dig a little deeper and do some engineering design to
tackle a more difficult problem.
Content management is being able to keep track of, manipulate, and
generally perform tasks with content, typically documents in a web
environment. Smart content management does all that in an easy, painless
fashion. (Check out
this article
to study up on content management.) Behind the scenes
of such an application is heavy duty file manipulation. Once again,
enter the FSO. We need to be able to move, delete, rename, and create
files, and our friend the FSO steps in nicely. In the article above, we
discuss a submission system for writers to
publish their content. What we don't mention is exactly what to do with
the file once it's up.
First of all, you'll most likely want to rename the file, since the writer
probably named it something only he would understand or care about.
To be able to keep track of all your documents, you'll want to rename it
to something unique, that is easily identifiable by your system. Unfortunately,
the FSO doesn't allow for an easy file renaming, so we'll have to engineer
a bit.
<%
' create the fso object
set fso = Server.Createobject("Scripting.FileSystemObject")
path = "c:\temp\test.txt"
strDate = Replace(Date(), "/", "")
strDir = "c:\inetpub\wwwroot\articles\" & strDate
strNewFileName = Hour(Now) & "_" & Minute(Now) & "_" & second(Now) & ".html"
' open the old file
set file = fso.opentextfile(path, 1) <-- For reading
strText = file.readall
set file = nothing
' check for and/or create folder
if not fso.folderexists(Server.MapPath(strDir)) then
set f = fso.CreateFolder(Server.MapPath(strDir))
else
set f = fso.GetFolder(Server.MapPath(strDir))
end if
' create and write new file
set file = fso.Createtextfile(f.path & "\" & strNewFileName)
file.write(strText)
set f = nothing
file.close
set file = nothing
' delete the old file
fso.DeleteFile(path & "\" & rst("FileName") & i)
' clean up
set fso = nothing
%>
The lack of the FSO's abilities however can be used to our advantage here;
we can perform two steps in one. First, we open and read the file's contents.
I'll assume that we'll want to create a unique folder, as well as a unique
filename to store the article in. However, since this folder's path will
change every day, we'll have to first check to see if the folder
already exists, and if it doesn't, create it. This is done by the
if not fso.folderexists section. We then get that path and use
it to create the new file (you'll recall that the FSO needs the full path
name to manipulate files, and the GetFolder and
CreateFolder
methods return just that - a full path). After we're done with the new file,
we'll want to get rid of the old one,
so it doesn't clutter up our file system. This is accomplished with
fso.DeleteFile.
So the two steps we accomplished here were renaming the file, and moving
it to a more apropos directory. Note that we could also have done some
more manipulation here, such as editing the text before we wrote it to
the new file.
Searching with the FSO
The wonders of the File System Object
What you can't do
|