The Magic function of the FSO in ASP-using FSO for Content Management _fso topic

Source: Internet
Author: User
Author: Gan Ganping


To this, you may already have a good understanding of the FSO. Let's go into the next step to solve more complex problems.

First, you may want to rename the file. To keep track of all the documents, you will rename them so that they are unique so that the system can be easily area
Don't. Unfortunately, the FSO does not allow simple file renaming operations, so we have to modify it.

<%
' Create ' the FSO object
Set fso = Server.CreateObject ("Scripting.FileSystemObject")
Path = "C:emp est.txt"
Strdate = Replace (Date (), "/", "")
Strdir = "C:inetpubwwwrootarticles" & Strdate
Strnewfilename = Hour (now) & "_" & Minute (now) & "_" &
Second (now) & ". html"

' Open ' 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
%>

FSO's lack of capacity here has become an advantage, we can perform 2 steps at a time. First, open the file and read the contents of the file. Suppose you want to create a
Unique folder and a unique file to store the article. However, because the path to the folder changes every day, you must first check whether the folder has
exists, if it does not exist, it is created. This is done in the If not fso.folderexists code snippet. Then, get that path and create a new file. New
After the file is established, delete the old file, this through the FSO. DeleteFile to complete.

These 2 steps are: Rename the file and move to a more appropriate directory. Note that you can also do more with files here, such as writing
Edit the content before entering the new file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.