1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

ASP File manipulations

Discussion in 'C#' started by ludwig, Jul 13, 2008.

  1. #1
    Hi guys, just a quick note for you to see how to manipulate files with ASP

    • Get the filenames of a directory
    • Copy a File or Files
    • Move a File or Files
    • Delete a File or Files
    • Read contents of File
    • Write to a File
    • Append to a File

    Get the filenames in a directory

    Replace "directoryname" with your directory name.
    Note : Replace 'directoryname' with the name od directory you wish to read
    In case of NT the directory mush have read permission for being read from anonymous account

    <%
    Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject")
    Set MyFiles=MyDirectory.GetFolder(Server.MapPath("directoryname"))
    For each filefound in MyFiles.files
    Response.write filefound.Name
    Next
    %> 
    HTML:
    Copy a File

    Use the file system operations, Copy is supported by the FileSystemObject object.
    CopyFile source, destination[, overwrite]
    
    <%
    Dim fileObj
    Set fileObj = Server.CreateObject("Scripting.FileSystemObject")
    fileObj.CopyFile "C:\InetPub\wwwroot\*.txt", "C:\Windows\Temp"
    Set fileObj = Nothing
    %>
    HTML:
    Note : The user must have write permissions to copy a file from one directory to another.

    Move a File or Files

    Use the file system operations, Rename is supported by the FileSystemObject object.
    MoveFile source, destination

    <%
    Dim fileObj
    Set fileObj = Server.CreateObject("Scripting.FileSystemObject")
    fileObj.MoveFile "C:\InetPub\wwwroot\*.txt", "C:\Windows\Temp"
    Set fileObj = Nothing
    %> 
    HTML:
    Note : The user must have minimum delete permissions on the source directory
    and write permissions on the destination directory.

    Delete a File or Files

    <%
    Dim fileObj
    Set fileObj = Server.CreateObject("Scripting.FileSystemObject")
    fileObj.DeleteFile "C:\InetPub\wwwroot\*.txt", False
    Set fileObj = Nothing
    %> 
    HTML:
    Note : The optional parameter for the DeleteFile method is Force.
    This defaults to False, and determines whether or not read-only files will be deleted.
    Only if Force is set to True will read-only files be deleted.

    Read a File
    <%
    Set fs         = CreateObject("Scripting.FileSystemObject")
    filename      = server.mappath("/somedirectory/readfile.txt")
    Set thisfile= fs.OpenTextFile(filename,1,False)
    Do until thisfile.AtEndOfStream
    	thisline = thisfile.readline
    	If thisline  "" then
    	    Response.write thisline
    	End If
    Loop
    thisfile.close
    set thisfile = Nothing
    set fs          = Nothing
    %> 
    
    HTML:
    Write to a File
    <%
    
    Set myText = "Hi, I am writing to a file "
    Set fs         = CreateObject("Scripting.FileSystemObject")
    filename      = server.mappath("/somedirectory/writefile.txt")
    Set thisfile= fs.OpenTextFile(filename,2,True)
    thisfile.writeline(myText)
    thisfile.close
    Set thisfile = Nothing
    Set fs          = Nothing
    %>
    HTML:
    Append to a File
    <%
    Set myText    = "Hi, I am writing to a file "
    Set fs          = CreateObject("Scripting.FileSystemObject")
    filename       = server.mappath("/somedirectory/appendfile.txt")
    Set thisfile = fs.OpenTextFile(filename,8,True)
    thisfile.writeline(myText)
    thisfile.close
    Set thisfile = Nothing
    Set fs          = Nothing
    %> 
    
    HTML:
    Note :
    Notice the numbers 1,2 and 8 in the above examples. Here is what they represent
    1: Opens file for reading. Cannot write to this file
    2: Opens file for writing. This file can't be read. Writing anything to this file would overwrite the previous contents.
    8: Opens the file for appending. The previous contents is not overwritten.

    Notice the "True" and "False" parameters in examples above
    True: The file will be created if it doesn't exist
    False: A new will not be created if it doesn't already exist.
     
    ludwig, Jul 13, 2008 IP