BaldDragon Posted August 31, 2005 Share Posted August 31, 2005 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 More sharing options...
Raindancer Posted August 31, 2005 Share Posted August 31, 2005 (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 August 31, 2005 by Raindancer Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer Link to comment Share on other sites More sharing options...
Valuater Posted August 31, 2005 Share Posted August 31, 2005 (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 August 31, 2005 by Valuater Link to comment Share on other sites More sharing options...
Raindancer Posted August 31, 2005 Share Posted August 31, 2005 (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) ExitNow it definitly should work... Not tested though...Edit: Typos... as always Edit: Didn't see the ExitLoop when nothing is read. Replaced it with ContinueLoop which does wath you want. Edited August 31, 2005 by Raindancer Say: "Chuchichäschtli"My UDFs:_PrintImage UDF_WinAnimate UDFGruess Raindancer Link to comment Share on other sites More sharing options...
Valuater Posted August 31, 2005 Share Posted August 31, 2005 ContinueLoop This continues the search incase the line does not have "Office Version:" in it very good Raindancer 8) Link to comment Share on other sites More sharing options...
/dev/null Posted August 31, 2005 Share Posted August 31, 2005 (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 $file12.) 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 WendCheersKurt Edited August 31, 2005 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 More sharing options...
BaldDragon Posted August 31, 2005 Author Share Posted August 31, 2005 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 More sharing options...
Valuater Posted August 31, 2005 Share Posted August 31, 2005 (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 August 31, 2005 by Valuater Link to comment Share on other sites More sharing options...
/dev/null Posted August 31, 2005 Share Posted August 31, 2005 /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^ ERRORI 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?CheersKurt __________________________________________________________(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 More sharing options...
BaldDragon Posted August 31, 2005 Author Share Posted August 31, 2005 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 Link to comment Share on other sites More sharing options...
BaldDragon Posted August 31, 2005 Author Share Posted August 31, 2005 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. Link to comment Share on other sites More sharing options...
/dev/null Posted August 31, 2005 Share Posted August 31, 2005 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.CheersKurt __________________________________________________________(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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now