The Scripting.FileSystemObject object is very handy when dealing with files or folders on the server. Imagine to have an asp page displaying the content of a folder, the user will be able to manage the folder and its content quite easily.
The FileSystemObject allow to access all information regarding a file (creation date, size, name and so on) and to execute simple operations on the file itself.
Ok, in this post I will concentrate on move, copy and delete operations, but in the future I plan to explore other methods.
First of all we initialize the object:
Then we set the file we need to manipulate:
Set FSO= Server.CreateObject("Scripting.FileSystemObject")
The yourfilepath and youdestinationfilepath should be something like /fileupload/uploads/763/document1.doc
filepath = yourfilepath
destinationfilepath = youdestinationfilepath
file = Server.MapPath(filepath)
destinationfile = Server.MapPath(destinationfilepath)
Now we actually can manipulate the file the way we want.
In order to copy the file, we use:
The last parameter is set to false if you don't want to overwrite an existing file in destination and true if you want to.
FSO.CopyFile file, destinationfile, False
The CopyFolder method will copy the folder and the CreateFolder method will create a new folder.
The MoveFile and MoveFolder methods work the same way:
folder = Server.MapPath("/fileupload/oldfolder/")
newfolder = Server.MapPath("/fileupload/newfolder/")
FSO.CopyFolder folder, newfolder, False
The easiest methods (but the most dangerous!) are the DeleteFile and DeleteFolder:
FSO.MoveFile file, destinationfile
FSO.MoveFolder folder, newfolder
In the end of our little piece of code, we set the object to nothing:
And that's it.
Set FSO = Nothing
Just as reminder, in this post we saw:
the Server.CreateObject("Scripting.FileSystemObject") object,
the CopyFile, CopyFolder, MoveFile, MoveFolder, DeleteFile and DeleteFolder methods.
Now start planning your file management system web page.