Sideway BICK BlogSideway BICK BLOG from Sideway

A Sideway to Sideway Home

Link:http://output.to/sideway/default.asp?qno=130300031

CopyFile / DeleteFile / GetFile / MoveFile, FileSystemObject Object, ASP Server Component

FileSystemObject Object

One function of FileSystemObject object is the manipulation of files of the file system.

FileSystemObject.CopyFile

FileSystemObject.CopyFile method is the method to copy one or more files from the source location to the destination location.

Syntax:

FileSystemObjectName.CopyFile(source,destination[, overwrite])

 Or in VBScript. Imply

FileSystemObjectName.CopyFile(source,destination[, overwrite])

 Or in JScript. Imply

FileSystemObjectName.CopyFile(source,destination[, overwrite])

Parameters:

FileSystemObjectName

The parameter "FileSystemObjectName" is used to specify the name of the instance of the FileSystemObject Object related to.

source

The parameter "source" is used to specify the path string of the source file specification to be copied from. Wildcard characters can be used to specify one or more files to be copied.

destination

The parameter "destination" is used to specify the path string of the destination folder specification to which the files from source are to be copied. Wildcard characters cannot be used to specify the path of  desitination folder.

overwrite

The optional parameter "overwrite" is a boolean value used to indicate that existing folders in destination are to be overwritten are not. The default value of parameter overwrite is true. Files are overwritten if the boolean value is true. Files are not overwritten if the boolean value is false. However, an error will occur if destination has the read-only attribute set, regardless of the value of overwrite.

Remarks:

FileSystemObjectName should always refer to a FileSystemObject Object.

If the parameter source contains wildcard characters or the parameter destination ends with a path separator (\), the parameter destination is assumed to be an existing folder in which to copy matching folders and subfolders. Otherwise, the parameter destination is assumed to be the name of the folder to be created for copying to.

In either case, when copying from the source to the desination,

  • If the destination does not exist, the source files gets copied.

  • If the destination is an existing file, an error occurs if overwrite is false, otherwise an attempt is made to copy source over the existing file.

  • If the destination is a directory, an error occurs .

An error also occurs if a source doesnot match any files when using wildcard characters.

Besides, the CopyFolder method stops on the first error it encounters. No attempt is made to roll back any changes were made before an error occurs.

Permission Denied Error may occur, if the folder is not shared with the corresponding Web Server.

Examples:

  • Example of using the CopyFile method to copy files

    ASP VBScript command:

    <script runat="server" language="VBScript">
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile "d:\temp1\test.txt", "d:\temp2\"
    fso.CopyFile "d:\temp1\*.txt", "d:\temp2\","true"
    </script>

    HTML web page ouput:

     

  • Example of using the CopyFile method to copy files

    ASP JScript command:

    <script runat="server" language="JScript">
    var fso;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    fso.CopyFile("d:\\temp1\\test.txt", "d:\\temp2\\");
    fso.CopyFile("d:\\temp1\\*.txt", "d:\\temp2\\","true");
    </script>

    HTML web page ouput:

     

FileSystemObject.DeleteFile Method

FileSystemObject.DeleteFile method is the method to delete the specified file related to the specified FileSystemObject Object.

Syntax:

FileSystemObjectName.DeleteFile(filespec[, force])

 Or in VBScript. Imply

FileSystemObjectName.DeleteFile(filespec[, force])

 Or in JScript. Imply

FileSystemObjectName.DeleteFile(filespec[, force])

Parameters:

FileSystemObjectName

The parameter "FileSystemObjectName" is used to specify the name of the instance of the FileSystemObject Object related to.

filespec

The parameter "filespec" is used to specify the file specification to be deleted. The parameter filespec can contain wildcard characters in the last path component.

force

The optional parameter "force" is a boolean value used to indicate that file with read-only attribute setting are to be deleted are not. The default value of parameter force is false. Files are deleted if the boolean value is true. F are not deleted if the boolean value is false.

Remarks:

FileSystemObjectName should always refer to a FileSystemObject Object.

An error occurs if no matching files are found.

Besides, the DeleteFile method stops on the first error it encounters. No attempt is made to roll back or undo  any changes were made before an error occurs.

Examples:

  • Example of using the DeleteFile method to delete the specified files

    ASP VBScript command:

    <script runat="server" language="VBScript">
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile "d:\temp2\test.txt"
    fso.DeleteFile "d:\temp2\*.txt"
    </script>

    HTML web page ouput:

     

  • Example of using the DeleteFile method to delete the specified files

    ASP JScript command:

    <script runat="server" language="JScript">
    var fso;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    fso.DeleteFile( "d:\\temp2\\test.txt" );
    fso.DeleteFile ("d:\\temp2\\*.txt");
    </script>

    HTML web page ouput:

     

FileSystemObject.GetFile Method

FileSystemObject.GetFile method is the method to return an instance of a File object corresponding to the specified file specification related to the specified FileSystemObject Object.

Syntax:

FileSystemObjectName.GetFile(filespec)

 Or in VBScript. Imply

Set FileObjectName = FileSystemObjectName.GetFile(filespec)

 Or in JScript. Imply

FileObjectName = FileSystemObjectName.GetFile(filespec)

Parameters:

FileObjectName

The parameter "FileObjectName" is the name assigned to the instance of the File object returned by the method using the FileSystemObjectName.GetFile Method.

FileSystemObjectName

The parameter "FileSystemObjectName" is used to specify the name of the instance of the FileSystemObject Object related to.

filespec

The parameter "filespec" is used to specify the file specification for which the File object to be returned. The parameter filespec is the absolute or relative path of the specific file. An error occurs if the parameter filespec of the specified file does not exist. The GetFile method does not support the use of wildcard characters, such as ? or *.

Return Values:

Folder object

The method returns an instance of Folder object corresponding to the specified folderspec related to the specified FileSystemObject Object. 

Remarks:

FileSystemObjectName should always refer to a FileSystemObject Object.

Examples:

  • Example of using the GetFile method to return an instance of File object corresponding to the specified file specification.

    ASP VBScript command:

    <script runat="server" language="VBScript">
    Dim fso, FileObjectName
    Set fso = CreateObject("Scripting.FileSystemObject")
    set FileObjectName = fso.GetFile("d:/temp1/test.txt")
    Response.Write FileObjectName.Name &  "<br />"
    </script>;

    HTML web page ouput:t:

    test.txt

  • Example of using the GetFile method to return an instance of File object corresponding to the specified file specification.on.

    ASP JScript command:

    <script runat="server" language="JScript">
    var fso, FileObjectName;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    FileObjectName = fso.GetFile("d://temp1//test.txt");
    Response.Write(FileObjectName.Name + "<br />") ;
    </script>;

    HTML web page ouput:

    test.txt

FileSystemObject.MoveFile Method

FileSystemObject.MoveFile method is the method to move one or more files according to the specified source location from the source location to the destination location related to the specified FileSystemObject Object

Syntax:

FileSystemObjectName.MoveFile(source, destination)

 Or in VBScript. Imply

FileSystemObjectName.MoveFile(source, destination)

 Or in JScript. Imply

FileSystemObjectName.MoveFile(source, destination) 

Parameters:

FileSystemObjectName

The parameter "FileSystemObjectName" is used to specify the name of the instance of the FileSystemObject Object related to.

source

The parameter "source" is used to specify the path string of the source file specification to be moved from. Wildcard characters can be used to specify one or more files to be moved. However the wildcard characters can only be used in the last path component of the path specification.

destination

The parameter "destination" is used to specify the path string of the destination file specification to which the file or files from source are to be moved. Wildcard characters cannot be used to specify the path of  desitination folder.

Remarks:

FileSystemObjectName Method should always refer to a FileSystemObject Object.

If the parameter source contains wildcard characters or the parameter destination ends with a path separator (\), the parameter destination is assumed to be an existing folder in which to copy matching folders and subfolders. Otherwise, the parameter destination is assumed to be the name of the folder to be created for copying to.

In either case, when moving from the source to the desination,

  • If the destination does not exist, the source file or files gets moved.

  • If the destination is an existing file, an error occurs.

  • If the destination is a directory, an error occurs.

An error also occurs if a source doesnot match any folders when using wildcard characters.

Besides, the CopyFolder method stops on the first error it encounters. No attempt is made to roll back any changes were made before an error occurs.

This method also allows moving folders between volumes only if supported by the operating system.

Permission Denied Error may occur, if the folder is not shared with the corresponding Web Server.

Examples:

  • Example of using the MoveFolder method to one or more folders from the source to destination.

    ASP VBScript command:

    <script runat="server" language="VBScript">
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile "d:\temp1\test.txt", "d:\temp2\"  
    </script>

    HTML web page ouput:

     

  • Example of using the MoveFolder method to one or more folders from the source to destination.

    ASP JScript command::

    <script runat="server" language="JScript">
    var fso, foldero;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    fso.MoveFile("d:\\temp1\\*.txt", "d:\\temp2\\");
    </script>

    HTML web page ouput:::

     

Previous Month  MAR  2013  Next Month
SMTWTFS
12
3456789
10111213141516
17181920212223
24252627282930
31

Previous Month  APR  2013  Next Month
SMTWTFS
123456
78910111213
14151617181920
21222324252627
282930

Sideway BICK Blog

25/03


Copyright © 2000-2020 Sideway . All rights reserved Disclaimerslast modified on 26 January 2013