| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
The code below is reading text file and putting its lines into array.
After the code is executed, each item of the array will be one line of the text file. Code:
<% Option Explicit %>
<%
Function ReadTextFile(strFilePath)
Dim objFSO, objFile, strAllFile
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath)
strAllFile = ""
If Not(objFile.AtEndOfStream) Then
strAllFile = objFile.ReadAll
End If
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
strAllFile = Replace(strAllFile, Chr(13)&Chr(10), Chr(13))
strAllFile = Replace(strAllFile, Chr(10)&Chr(13), Chr(13))
ReadTextFile = Split(strAllFile, Chr(13))
End Function
'usage
Const FILE_NAME="myfile.txt"
Dim arrLines, x, curLine
Response.Write("reading file: " & FILE_NAME & "...<br />")
arrLines = ReadTextFile(Server.MapPath(FILE_NAME))
Response.Write("amount of lines: " & (UBound(arrLines)+1) & "<br />")
Response.Write("file contents:<hr />")
For x=0 To UBound(arrLines)
curLine = arrLines(x)
Response.Write("Line #" & (x+1) & " " & Server.HTMLEncode(curLine) & "<br />")
Next
Response.Write("<hr />")
%>
for questions or comments, post here. ![]() Last edited by Shadow Wizard : November 6th, 2006 at 02:40 AM. |
|
#2
|
|||
|
|||
|
You can do the same using
Code:
Function ReadTextFile(strFilePath)
Dim objFSO, objFile, strAllFile, Counter
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath)
strAllFile = Array()
Counter = 0
Do While Not (objFile.AtEndOfStream)
Redim Preserve strAllFile(Counter)
strAllFile(Counter) = objFile.ReadLine
Counter = Counter + 1
Loop
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
ReadTextFile = strAllFile
End Function
__________________
CyberTechHelp |
|
#3
|
||||
|
||||
|
I don't like the Redim Preserve - it's not efficient and "eat"
lots of memory. one more thing, I stumbled on files that ReadLine didn't really read one line but rather gave the whole file.. found the problem was with weird line breaks in the file - different combination of Line Feed and Carriage Return than what expected by vbscript. |
|
#4
|
|||
|
|||
|
regarding reading a wav file
Quote:
I am not able to read from this code , nothing happens with this its just shows the blank screen , is there antyhing to follow before you implement the script , where this sript will be storing the output and how i can see that. Regards |
|
#5
|
||||
|
||||
|
the code posted by Desgy is only function, it's won't show anything.
try executing the code I gave and put your wav file name as the value of FILE_NAME. |
|
#6
|
|||
|
|||
|
regarding wav file
Quote:
I am able to see the output displayed in the browser but two problem what i am facing is its not taking the file located in my drive and displayed an error but if i copy the file into the same folder where script in my webserver it works !! So, how to read the files located in my d drive in the server or any map drive in the server . Secondly after reading this is it possible to use any loop or any function which find out certian value which i am looking into the wav file output text , ie say for example to know where the file is fully downloaded or ended i can read a tag "8880000" at each of the wav file i am getting with this i can know that wav file is complete and downloaded successfully. Regards |
|
#7
|
||||
|
||||
|
regarding your first question, give full path:
Code:
Const FILE_NAME="D:\MyFolder\myfile.wav" then change this line: Code:
arrLines = ReadTextFile(Server.MapPath(FILE_NAME)) to this: Code:
arrLines = ReadTextFile(FILE_NAME) regarding your second question, please reply in your own thread with exact details of what you need. |
|
#8
|
|||
|
|||
|
thanks What i did is changed this code from top
Dim FILE_NAME FILE_NAME = StrFilePath ' Contains my file name full path and changed as you suggested it worked thanks!! arrLines = ReadTextFile(FILE_NAME) |
|
#9
|
|||
|
|||
|
one quick query is that this all scripts are not working with the Map drive do i need to use any file system object with this script to work.
Regards |
|
#10
|
||||
|
||||
|
StrFilePath can be "I:\MyFolder\myfile.wav" where "I:" is the mapped
network drive.. it should work as long as there are proper permissions. feel free to post in your own thread about how to analyze the file. ![]() |
|
#11
|
|||
|
|||
|
yes i tried the same , seems to be permission issue but don't know how to overcome this , since the administrator has full rights to this drive but not sure what is the issue.
but anyhow if i keep it in my local its working good , great help !!! thanks a lot 3 cheers for you. |
|
#12
|
||||
|