Jump to content

Loop within a Loop


AGsol
 Share

Recommended Posts

Hi

I'm new to the forum and I have read the FAQ but couldn't find an answer. I have the below script which works to certain degree but I can't get it to loop on the $Username?

I hope someone can help.

CODE
$Loop = 1

$Log = FileOpen("c:\test\test.w3c", 0)

$input = FileOpen("c:\test\test.txt", 0)

$Output = FileOpen("c:\test\isa03.csv", 1)

$While 1

$Username = FileReadLine($input)

If @error = -1 Then ExitLoop

While 1

$Read = FileReadLine($Log, $Loop)

If @error = -1 Then ExitLoop

If StringInStr($Read, $Username) Then

FileWriteLine($Output, $Read )

Else

EndIf

$Loop = $Loop + 1

WEnd

WEnd

FileClose($Log)

FileClose($input)

FileClose($Output)

Link to comment
Share on other sites

Hi

I'm new to the forum and I have read the FAQ but couldn't find an answer. I have the below script which works to certain degree but I can't get it to loop on the $Username?

I hope someone can help.

CODE
$Loop = 1

$Log = FileOpen("c:\test\test.w3c", 0)

$input = FileOpen("c:\test\test.txt", 0)

$Output = FileOpen("c:\test\isa03.csv", 1)

$While 1

$Username = FileReadLine($input)

If @error = -1 Then ExitLoop

While 1

$Read = FileReadLine($Log, $Loop)

If @error = -1 Then ExitLoop

If StringInStr($Read, $Username) Then

FileWriteLine($Output, $Read )

Else

EndIf

$Loop = $Loop + 1

WEnd

WEnd

FileClose($Log)

FileClose($input)

FileClose($Output)

Please explain what you want to do clearly.

You want to write in output only if in line that was read from file $input exist line read from $log?

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Please explain what you want to do clearly.

Yeah sorry that was a typo on the code page not on my script it is While 1 not $While.

I want to Read a file with a list of usernames a line at a time and then with that username check through another file line by line to output all the lines relevant to that user once finished with that username move on to the next.

Thanks for the quick response hope this makes it clear.

Link to comment
Share on other sites

$users = FileOpen("users.txt", 0)
$out = FileOpen("matching lines.txt", 1)

While 1
    $user = FileReadLine($users)
    If @error Then ExitLoop
    
    $log = FileOpen("file to be checked.txt", 0)
    
    While 1
        $line = FileReadLine($log)
        If @error Then ExitLoop
        
        If StringInStr($line, $user) Then
            FileWriteLine($out, $line)
        EndIf
    WEnd
    
    FileClose($log)
WEnd

FileClose($users)
FileClose($out)

this should work, you have to close the logfile everytime you loop through it to reset the filepointer...

Link to comment
Share on other sites

$users = FileOpen("users.txt", 0)
$out = FileOpen("matching lines.txt", 1)

While 1
    $user = FileReadLine($users)
    If @error Then ExitLoop
    
    $log = FileOpen("file to be checked.txt", 0)
    
    While 1
        $line = FileReadLine($log)
        If @error Then ExitLoop
        
        If StringInStr($line, $user) Then
            FileWriteLine($out, $line)
        EndIf
    WEnd
    
    FileClose($log)
WEnd

FileClose($users)
FileClose($out)

this should work, you have to close the logfile everytime you loop through it to reset the filepointer...

Thanks alot that worked fine.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...