Sign in to follow this  
Followers 0
BaldDragon

Frustrated....

12 posts in this topic

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)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Replace:

FileWriteLine("C:\logs\office.txt", $file1)

With:

FileWriteLine("C:\logs\office.txt", $file)

$file1 is your handle, and no text. But $file contains the filename as String.

Try it...

Edit: Typos...

Edited by Raindancer

Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

from help

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

you need to open the second file to "write" in before FileWriteLine()

and then this should be

FileWriteLine($file2, $file)

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

ContinueLoop

This continues the search incase the line does not have "Office Version:" in it

very good Raindancer

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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 *

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
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 *

Share this post


Link to post
Share on other sites

Val:

If the line exists, its at the bottom, Line 22.

Dev:

I copied the entire script. I checked it in SciTE and then compiled it after your last reply, same error. I'm using v3.1.1. Should I use a new version?

Thanks

Share this post


Link to post
Share on other sites

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

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

Dev's script works perfectly and fast!

Thanks for your help.

Share this post


Link to post
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 *

Share this post


Link to post
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
Sign in to follow this  
Followers 0