0

I am trying to write an aspx page which will crawl through a directory and find all the files contained within. I think I have that part down.

Is it possible to read to a string without first creating .txt files from the html and asp pages I'm reading through? I don't want to create a ton of new files and then end up having to delete them later.

Ultimately, I'm trying to develop a tool to search through an entire directory and find all the image tags which have empty alt attributes or no alt attributes. I wrote some jQuery which can find the tags, and I have also written the part that searches through a directory.

3
  • Do you just want to read the html files that you have discovered? Commented May 18, 2012 at 14:02
  • I don't understand. You want to find all files in directory, then load specified file into string object? Commented May 18, 2012 at 14:05
  • That's essentially it. I want to find all the files in a directory, read them to a string, and then check the string for image tags that fit my criteria. I have the directory part and the image tag search part more or less done. Can I read files to a string without first copying their contents to a .txt file? Commented May 18, 2012 at 14:08

3 Answers 3

2

If you have a file on your filesystem, you can simply read it - if you know it is a textual format, you need to use a stream with the correct encoding to do this.

Since you are reading and querying HTML, I suggest using a library that is specifically written for this task - the HTML Agility Pack - you can give it the path to the HTML file and then query it for all img elements. The source download comes with sample projects that will show you how to achieve this and other tasks.

Sign up to request clarification or add additional context in comments.

2 Comments

Will HTML Agility Pack read asp pages? I'm assuming yes but I couldn't find an answer with some quick googling.
@MNRSullivan - Not the source files. If you mean pages that you see in your browser that were created with ASP.NET, then yes, so long as you see HTML.
1

Link: http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

Example: http://www.csharp-examples.net/load-text-file-to-string/ (this example shows how to work with .txt files, but I believe if you can put any other extension)

Getting all files: http://www.csharp-examples.net/get-files-from-directory/

Edit: and don't forgot about encoding.

Comments

0

sure, why not save to environment variables, no fuss, no mess. so try something like this: will take apart an html or asp file and save to an array of variables, i have shown you how to put it back together as well. let me know how if this is a solution for you

@echo off
setlocal EnableDelayedExpansion EnableExtensions
echo.
set count=0
if exist newfile.html del newfile.html
:: to unassemble
for /f "tokens=*" %%a in (filename.html) do (
    echo %%a
    set /a count=count + 1
    set htmllinenum!count!=%%a
)& set finalcount=!count!
:: to assemble
for /l %%a in (1,1,%finalcount%) do (
    echo !htmllinenum%%a!>>newfile.html
)
notepad newfile.tmp
    set count=0
if exist newfile.asp del newfile.asp
:: to unassemble
for /f "tokens=*" %%a in (filename.asp) do (
    echo %%a
    set /a count=count + 1
    set asplinenum!count!=%%a
)& set finalcount=!count!
:: to assemble
for /l %%a in (1,1,%finalcount%) do (
    echo !asplinenum%%a!>>newfile.asp
)
notepad newfile.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.