Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Classic ASP: dump text file contents into array

Results 1 to 12 of 12
Share This Thread →
  1. #1
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921

    Post Classic ASP: dump text file contents into array

    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. #2
    degsy is offline Contributing User
    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    136
    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

  3. #3
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #4
    digitaldxb is offline Contributing User
    Join Date
    Oct 2006
    Posts
    173
    Rep Power
    11

    regarding reading a wav file

    Quote Originally Posted by degsy
    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
    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. #5
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #6
    digitaldxb is offline Contributing User
    Join Date
    Oct 2006
    Posts
    173
    Rep Power
    11

    regarding wav file

    Quote Originally Posted by Shadow Wizard
    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.
    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. #7
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #8
    digitaldxb is offline Contributing User
    Join Date
    Oct 2006
    Posts
    173
    Rep Power
    11
    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. #9
    digitaldxb is offline Contributing User
    Join Date
    Oct 2006
    Posts
    173
    Rep Power
    11
    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. #10
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #11
    digitaldxb is offline Contributing User
    Join Date
    Oct 2006
    Posts
    173
    Rep Power
    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. #12
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    IIS does not connect as administrator to the network drive.
    the default account is IUSR_<machine name> so this account
    should get proper permissions over that location.

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. Program can't locate a text file
    By Happy Camper in forum .NET Development
    Replies: 3
    Last Post: May 19th, 2006, 09:50 PM
  2. IIS 6.0 Crashing Daily on Production Server
    By JMelsoner in forum Microsoft IIS
    Replies: 5
    Last Post: April 12th, 2006, 05:43 PM
  3. Replies: 0
    Last Post: December 23rd, 2005, 03:24 PM
  4. How do I close a text file?
    By jl39775 in forum .NET Development
    Replies: 1
    Last Post: May 13th, 2003, 09:17 PM
  5. Web serverístatistical analysis
    By cso in forum ASP Development
    Replies: 0
    Last Post: April 19th, 2003, 11:35 AM

ASP Free Advertisers and Affiliates