Jump to content

Frustrated....


Recommended Posts

I'm hoping somebody can help with this script. I've been working on it for over a week now and can't seem to get it to work. I have over a 1000 text files that contain essentially the same data. I'm trying to read the first file in the directory to an array, check if a line of text exists, if it does, write the name of the file to a seperate text file and do the same over till all files in the directory have been checked. So far I haven't been able to get it to do anything. I've looked at it so long, I can't see whats wrong. I think I need another set of eyes on this. Any help would be appreciated!

Dim $search

$search = FileFindFirstFile( "c:\logs\*.txt" )  

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    $file1 = FileOpen($file, 0)
    
    $line = FileReadLine($file1)
    If @error = -1 Then ExitLoop
    If StringInStr($line, "Office Version:") Then
        FileWriteLine("C:\logs\office.txt", $file1)
    EndIf
  
WEnd

FileClose($file1)
Link to comment
Share on other sites

from help

"Either use filehandles or filenames in your routines--not both."

<{POST_SNAPBACK}>

But it isn't impossible...

I usualy use FileOpen if I want to access the file many times.

And the Filename if i want to access it once.

But you are right.

This code would be cleaner:

Dim $search

$search = FileFindFirstFile( "c:\logs\*.txt" )  

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

$filelog = FileOpen("C:\logs\office.txt", 1)

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    $file1 = FileOpen($file, 0)
    
    $line = FileReadLine($file1)
    If @error = -1 Then 
        FileClose($file1)
        ContinueLoop; Instead of ExitLoop (leaving the While Loop) this startes the While Loop from start
    EndIf
    If StringInStr($line, "Office Version:") Then
        FileWriteLine($filelog, $file)
    EndIf
  
WEnd

FileClose($filelog)
FileClose($search)
Exit

Now it definitly should work... Not tested though...

Edit: Typos... as always :whistle:

Edit: Didn't see the ExitLoop when nothing is read. Replaced it with ContinueLoop which does wath you want.

Edited by Raindancer
Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer
Link to comment
Share on other sites

I'm hoping somebody can help with this script. I've been working on it for over a week now and can't seem to get it to work. I have over a 1000 text files that contain essentially the same data. I'm trying to read the first file in the directory to an array, check if a line of text exists, if it does, write the name of the file to a seperate text file and do the same over till all files in the directory have been checked. So far I haven't been able to get it to do anything. I've looked at it so long, I can't see whats wrong. I think I need another set of eyes on this. Any help would be appreciated!

there are some errors in your script.

1.) Sometimes you use $file and then $file1

2.) You are only reading the first line of each text file. So, if the string is not contained in that first line, you won't find it.

Try this:

#include <file.au3>

Dim $search

$search = FileFindFirstFile( "c:\logs\*.txt" )  

If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

dim $aRecords

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    
    _FileReadToArray($file, $aRecords)
    
    for $n = 1 to $aRecords[0]
       If StringInStr($aRecords[$n], "Office Version:") Then
           FileWriteLine("C:\logs\office.txt", $file)
           exitloop
       EndIf
    next  
Wend

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Thanks guys!

Here's the results:

Rainlender script - Nothing gets written to the script once I run it. I'm testing with 5 files, 2 have the line I need and 3 don't. Any ideas?

/dev/null - I'm receiving this error when I run the changed script:

: ==> Subscript used with non-Array variable.:

for $n = 1 to $aRecords[0]

for $n = 1 to $aRecords^ ERROR

Link to comment
Share on other sites

is this text "Office Version:" in the FIRST line of the file???

so we know which way to continue

RainDancer's script looks at the first line of the file only

and

/dev/null 's script searches the entire file for the text

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

/dev/null - I'm receiving this error when I run the changed script:

: ==> Subscript used with non-Array variable.:

for $n = 1 to $aRecords[0]

for $n = 1 to $aRecords^ ERROR

I don't get any errors, neither with the official release nor with the beta release.

Did you copy+paste my whole script, or only adjust some parts of your script?

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Nevermind guys....I'm a jackass.

The file has to be run in the directory the files are located.

That's correct, as FileFindNextFile() returns just the filename part, without the path. I thought that was known.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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...