Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse multiple files in a loop until the last file is parsed 1

Status
Not open for further replies.

EchoAlertcom

IS-IT--Management
Oct 8, 2002
239
US
Hello,

I have a script that parses a text file and depending on the content of each line, it will write that line to the appropriate text file. I have this working great now (thanks to members of this group on Tek-tips).

The next task that I'm stuck on is having the script parse one file after the next in a loop. Right now it is set up for the user to select a file from the list and click submit. The user has to do this for each file in the directory where these files reside. There is no reason they can't start the script once and have it go to the first file in the directory, parse it and then go to the next and parse it until there are no more files in the directory.

Can someone suggest an over structure for this? I'm not sure how to make the loop find the next file from the one it had in the last iteration. The file names are always different but to make it easy i listed them as below.

I have what the parsing does finished I'm only looking for help with looping through the files in the directory automatically.

For example:

I would need it to parse the following files.

C:\inetpub\intranet\files\text1.txt
C:\inetpub\intranet\files\text2.txt
C:\inetpub\intranet\files\text3.txt

Warmest Regards,
Steve
 
Thank you for your response. My question wasn't worded well. Let me try again....

How would I alter a script (I'm assuming I would have to add a loop plus some other stuff) that processes text files 1 at a time to a script that will parse the first, then second, then third file until no more files existed in the directory. I already have all the code that opens, reads, and deletes the individual files. I just need it to go on to the next file automatically when it's done with the current file until all files are deleted from the directory.

I hope this is more clear this time.

Thank you for your patience.

Regards,
Steve
 
That is exactly what the second part of that code does:

You can also search google on more specifics of filesystem objects, when I need to find something that is about ASP you just type "ASP loop through files in folder" or "ASP filesystem object" it works well for any type of programming I would suggest looping through the files in the below code then grabbing the directory and then loop through them after the filesystem object is closed, thuse you can read them in a regular loop and not while you have two FSO's open.

Jason

post if you have further questions

FSO:

Code:
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")


' this is "main" folder that they chose, this loops through each file in the directory. in inside the loop below is where you would want to collect all the directory names then after its done do another loop through the directorys and read them... i wouldnt do them inside the loop... but maybe that work well... it just sounds more efficient to read them outside of the loop below...
Dim objFolder
Set objFolder = objFSO.GetFolder("C:\whateverfolder")


'Loop through the Files collection
Dim objFile
For Each objFile in objFolder.Files
  Response.Write objFile.Name & "<br>"
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

Jason

[red]Army[/red] : [white]Combat Engineer[/white] : [blue]21B[/blue]
 
Oh, great. I missed that. Thank you for your help.

Warmest Regards,
Steve
 
If there is a danger of other files being left in that directory that you don't want to parse, or you have to do them specifically in order, you could use something like:
Code:
Dim fso, fil
Set fso = Server.CreateObject("Scripting.FileSystemObject")

Dim filCtr
filCtr = 1

Do While fso.FileExists("C:\yourPath\text" & filCtr & ".txt")

   fil = fso.OpenTextFile("C:\yourPath\text" & filCtr & ".txt")
   'do stuff
   fil.Close
   'presumably you'll also be deleting it when your done, or moving it to another location. That would be here

   filCtr = filCtr + 1
Loop

-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top