Open Read And Create Files In ASP

Tips >> ASP


The Create File function can create a new file or open an existing file. You must specify the name of the file, creation instructions, and other Characteristic. When an application creates a new file, the operating system adds it to the specified directory.

While creating a new file, The CreateFile function performs the following actions :-

  • Clears the existing file Characteristic(CREATE_ALWAYS with an existing file only).
  • Combines the file attributes and flags specified by dwFlagsAndAttributes with FILE_ATTRIBUTE_ARCHIVE.
  • Sets the file length to zero.
  • Copies the extended attributes supplied by the template file to the new file if the hTemplateFile parameter is specified.
  • Sets the SD specified by the lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure (except when using CREATE_ALWAYS on an existing file).


While opening an existing file, Create File performs the following below explained following actions :-

  • Combines the file flags (FILE_FLAG_*) specified by dwFlagsAndAttributes with existing file attributes. CreateFile ignores the file attributes (FILE_ATTRIBUTE_*) specified by dwFlagsAndAttributes.
  • Sets the file length according to the value of dwCreationDisposition.
  • Ignores the hTemplateFile parameter.
  • Ignores the lpSecurityDescriptor member of the SECURITY_ATTRIBUTES structure. The other structure members are used (for example, bInheritHandle indicates whether the file handle can be inherited).

It can be better explained by the following examples :-

Example#1 :-
This one will be the basic code we need to open a text file

<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")
filecontent = wfile.ReadAll

wfile.close
Set wfile=nothing
Set fs=nothing

response.write(filecontent)
%>

1
2
3
4
5
6
7
8
9
10
11
12

Line 2 will create the appropriate environment which allows to perform the operations involving files in the server. We have defined a variable named "fs" to do it (we may change the name of this variable).
Line 4 have create a new variable named "wfile" and we have apply the method OpenTextFile to variable "fs". We have also define which is the exact location of the file we want to open in this line (the complete path is necessary).
Line 5 have read all the content of the file to a variable named "filecontent" using the instruction "ReadAll".
Line 7 to 9 are use to let the server know we have finished all operations involving files.
Line 11 we have response to the client with the content in the variable "filecontent".

Example#2 :-
Let's suppose we have a file with different kind of information in each line (a name in the first line, the last name in the second one, and the age in the third one), and we want to use them separately. This one will be the script we may use :-

<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")
firstname = wfile.ReadLine
lastname = wfile.ReadLine
theage = wfile.ReadLine

wfile.close
Set wfile=nothing
Set fs=nothing

%>

Your first name is <% =firstname %><BR>
Your last name is <% =firstname %><BR>
Your are <% =firstname %> years old<BR>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

This example is very similar to the previous one, but in this case each line we have read from "myfile.txt" has been saved to a different variable (lines 5 to 7), and they have been used in lines 15 to 17 to respond to the client.

Example#3 :-
It explains the Open a File for Reading :-
The following code fragment uses CreateFile to open an existing file for reading.

#include <windows.h>
#include <stdio.h>

HANDLE hFile;

hFile = CreateFile(TEXT("myfile.txt"), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

In this case, CreateFile succeeds only if a file named Myfile.txt already exists in the current directory. A subsequent call to open this file with CreateFile will succeed if the call uses the same access and sharing modes.


Example#4 :-
It explains the Open a File for Writing

#include <windows.h>
#include <stdio.h>

HANDLE hFile;

hFile = CreateFile(TEXT("myfile.txt"), // file to create
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL | // normal file
FILE_FLAG_OVERLAPPED, // asynchronous I/O
NULL); // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Example#5 :-
This example will read all lines in the file, and the response page will include the content of each line with its line number.

<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")

counter=0
do while not wfile.AtEndOfStream
counter=counter+1
singleline=wfile.readline
response.write (counter & singleline & "<br>")
loop

wfile.close
Set wfile=nothing
Set fs=nothing

%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Line 6 will define the variable "counter", and in line 7 to 11 we will repeated instructions within the Do_While _Loop until the file does not reach the end of the file (the condition is "not wfile.AtEndOfStream").


Example#6 :-

Let's suppose we have a file with a number in line 1 and a second number in line 2.

<%
Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt")

number1 = Clng(wfile.ReadLine)
number2= Clng(wfile.ReadLine)

number1and2 = number1 + number2
response.write (number1and2)

wfile.close
Set wfile=nothing
Set fs=nothing

%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Example#7 :-
Let suppose we want to record the IP address of all visitor to our page to a file named "mylog.txt".

<%
VisitorsIP=Request.ServerVariables ("REMOTE_ADDR")

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.OpenTextFile("c:\Mydir\mylog.txt", 8,false,0)
wfile.WriteLine (VisitorsIP)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("IP registered")
%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

 

 
E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641