ASP MagicFile Object  

The MagicFile object allows you to manipulate files and directories from an Active Server Page. You can create directories, remove them, search for specific files, and copy files from one destination to another. Directories and files can be searched recursively or non-recursively, depending upon your needs.

 

Methods

  CreateNewDirectory Method
CopyDirectory Method
DirScanRecursive Method
DirScanSimple Method
DirList Method
SortDirList Method
RemoveFromDirList Method
CleanDirList Method
FileScanRecursive Method
FileScanSimple Method
FileList Method
SortFileList Method
RemoveFromFileList Method
CleanFileList Method
IsExists Method
CopyFile Method
GetLastErrorNbr Method
GetLastErrorDscr Method
 

Properties

  Count Property
DirCount Property

 

Working with MagicFile

To call the MagicFile Object from an Active Server Page, you will need to create the object using the following code:

<%MyObj=Server.CreateObject("MagicBundle.MagicFile")%>

Remember to enclose the code within <% %> brackets.
CreateNewDirectory Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.CreateNewDirectory(strDir)

Example: The following example creates a new directory called "shazam"

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
IsOK = MyObj.CreateNewDirectory("c:\shazam")
If IsOK then
   response.write("New directory was created successfully.")
else
   response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
   response.write(MyObj.GetLastErrorDscr)
End if
Set MyObj = nothing
%>
CopyDirectory Method
Copies an existing directory with all subdirectories and files to a new directory.
Return Value:Nonzero if successful; otherwise 0.
Syntax: object.CopyDirectory(strSrc, strDest)
  strSrc - name of an existing directory
  strDst - directory name to copy to
Example: The following example copies the directory c:\inetpub\wwwroot(the source directory) to c:\My_wwwRoot(the destination directory).
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
IsOk = MyObj.CopyDirectory("c:\inetpub\wwwroot", "c:\My_wwwRoot")
if IsOk then
  Response.write("Directory was copied successfully.")
else   response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
end if
set MyObj=Nothing
%>
DirScanRecursive Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.DirScanRecursive(strDir)

Example: The following example searches an entire directory tree and outputs a list of subfolders:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
Dim i
If (MyObj.DirScanRecursive("c:\"))=0 then
 response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
 response.write(MyObj.GetLastErrorDscr)
else
 for i=0 to MyObj.DirCount-1
   response.write(MyObj.DirList(i))
   response.write("<br>")
 next
end if
call MyObj.CleanDirList
set MyObj = nothing
%>

DirScanSimple Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.DirScanSimple(strDir)

Example: The following example shows a non-recursive search of the C: drive:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
Dim i
If (MyObj.DirScanSimple("c:\"))=0 then
 response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
 response.write(MyObj.GetLastErrorDscr)
else
 for i=0 to MyObj.DirCount-1
   response.write(MyObj.DirList(i))
   response.write("<br>")
  next
end if  
call MyObj.CleanDirList
set MyObj = nothing
%>
DirList Method
Returns an item from directories collection by DirNo index.
Syntax: object.DirList(DirNo)
DirNo - value from 0 to (object.DirCount-1)
SortDirList Method
Sets a value that determines whether DirList are sorted in ascending or descending order.
Syntax: object.SortDirList(varDirection)
varDirection<>0 - directory list will be sorted in ascending order
varDirection=0 - directory list will be sorted in descending order
RemoveFromDirList Method
Syntax: object.RemoveFromDirList(dirStr)
Example: The following example shows how to remove a directory's name from the directory list collection. Note that this method does not actually remove the directory from the drive, but simply removes the directory from the list.
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
Dim i
If (MyObj.DirScanSimple("c:\inetpub\wwwroot"))=0 then
 response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
 response.write(MyObj.GetLastErrorDscr)
else
'remove "c:\inetpub\wwwroot\test" from the list
 MyObj.RemoveFromDirList("c:\inetpub\wwwroot\test")
 for i=0 to MyObj.DirCount-1
  response.write(MyObj.DirList(i))
  response.write("<br>")
 next
end if
call MyObj.CleanDirList
set MyObj = nothing
%>
CleanDirList Method
Syntax: object.CleanDirList
Example: The following example creates a directory list, then clears it:
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
call MyObj.DirScanSimple("c:\inetpub\wwwroot\")
response.write (MyObj.DirCount & "<br>")
call MyObj.CleanDirList
response.write (MyObj.DirCount)
set MyObj = nothing
%>
FileScanRecursive Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.FileScanRecursive(strDir, strMask)
Example: The following example searches for all PDF files in the wwwroot directory:
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
Dim i
If (MyObj.FileScanRecursive("c:\inetpub\wwwroot", "*.pdf"))=0 then
 response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
 response.write(MyObj.GetLastErrorDscr)
Else
  for i=0 to MyObj.Count-1
    response.write(MyObj.FileList(i))
    response.write("<br>")
  next
end if
call MyObj.CleanFileList
set MyObj = nothing
%>
FileScanSimple Method
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.FileScanSimple(strDir, strMask)
Example: You can perform search in a non-recursive manner by using the FileScanSimple method, as shown below:
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
Dim i
If (MyObj.FileScanSimple("c:\inetpub\wwwroot", "*.pdf"))=0 then
 response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
 response.write(MyObj.GetLastErrorDscr)
Else
  for i=0 to MyObj.Count-1
    response.write(MyObj.FileList(i))
    response.write("<br>")
  next
end if
call MyObj.CleanFileList
set MyObj = nothing
%>
FileList Method
Returns an item from files collection by FileNo index.
Syntax: object.FileList(FileNo)
FileNo - value from 0 to (object.Count-1)
SortFileList Method
Sets a value that determines whether FileList are sorted in ascending or descending order.
Syntax: object.SortFileList(varDirection)
varDirection<>0 - FileList will be sorted in ascending order
varDirection=0 - FileList will be sorted in descending order
RemoveFromFileList Method
Syntax: object.RemoveFromFileList(fileStr )
Example: The following example shows how to remove a file's name from the file list collection. Note that this method does not actually remove the file from the drive, but simply removes the file from the list.
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
Dim i
If (MyObj.FileScanRecursive("c:\inetpub\wwwroot", "*.pdf"))=0 then
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
else
'remove the file called manual.pdf from the list
  MyObj.RemoveFromFileList("c:\inetpub\wwwroot\manual.pdf")
  for i=0 to MyObj.Count-1
    response.write(MyObj.FileList(i))
    response.write("<br>")
  next
end if
call MyObj.CleanFileList
set MyObj=Nothing
%>
CleanFileList Method
Syntax: object.CleanFileList
Example: The following example creates a file list, then clears it:
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
call MyObj.FileScanRecursive("c:\inetpub\wwwroot\", "*.*")
response.write (MyObj.Count)
call MyObj.CleanFileList
response.write ("<br>")
response.write MyObj.Count
set MyObj=Nothing
%>
IsExists Method
Checks if the file or the directory exists.
Return Value: Nonzero if successful; otherwise 0.
Syntax: object.IsExists(strPath)

Example: The following example checks to see if the file called hello.txt exists in the MyDocuments directory:

<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
If MyObj.IsExists("c:\MyDocuments\hello.txt") then
  response.write("File exists.")
Else
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
End If
set MyObj=Nothing
%>
CopyFile Method
Return Value:Nonzero if successful; otherwise 0.
Syntax: object.CopyFile(strSrc, strDest)
  strSrc - name of an existing file
  strDst - filename to copy to
Example: The following example copies the file c:\inetpub\wwwroot\hello.txt(the source file) to c:\hello.txt(the destination file).
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
IsOk = MyObj.CopyFile("c:\inetpub\wwwroot\hello.txt", "c:\hello.txt")
if IsOk then
  Response.write("File was copied successfully.")
else
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
end if
set MyObj=Nothing
%>
GetLastErrorNbr Method
Syntax: object.GetLastErrorNbr
Returns number of a last error.
GetLastErrorDscr Method
Syntax: object.GetLastErrorDscr
Returns description of a last error.
Count Property
Syntax: object.Count
Indicates the number of files in a file list collection.
Example: The following example provides a count of the number of PDF files:
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
If (MyObj.FileScanRecursive("c:\inetpub\wwwroot", "*.pdf"))=0 then
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
Else
  response.write(MyObj.Count & " PDF FILES<br>")
End If
call MyObj.CleanFileList
set MyObj=Nothing
%>
DirCount Property
Syntax: object.DirCount
Indicates the number of directories in a directory list collection.
Example: The following example provides a count of directories in wwwroot directory:
<%
Set MyObj=Server.CreateObject("MagicBundle.MagicFile")
If (MyObj.DirScanRecursive("c:\inetpub\wwwroot"))=0 then
  response.write("Error #" & MyObj.GetLastErrorNbr & " occurred:")
  response.write(MyObj.GetLastErrorDscr)
Else
  response.write(MyObj.DirCount & " DIRECTORIES<br>")
End If
call MyObj.CleanDirList
set MyObj=Nothing
%>
top


Home | FAQ | News | How to buy | Contacts | ASP MagicPerf Component | ASP MagicShell Component