barryb Posted December 21, 2008 Share Posted December 21, 2008 Hi, I am using the excellent RecurseDir() v2.2 that I found on the forum, I can get it to return an array of all the files I'm searching for, but cannot get FileOpen to open the returned result, so I can do a read / search / replace. I tried the straight path returned e.g c:\temp\doc.txt then added quotes "c:\temp\doc.txt" but no joy the error flag -1 is returned. I know it must be something simple but cannot see it Some pointers would be much appreciated, only been using AutoIt a few weeks. Thanks, Barry Local $i $dir = "C:\temp\" $mask = "*.txt" $Array = RecurseDir ($dir, $mask) If IsArray($Array) Then For $i = 1 to $Array[0] $sTextfilepath = ($Array[$i]) $sPathwithquote = '"'&$sTextfilepath&'"' ConsoleWrite ("This is returned :"& $sPathwithquote & "<--------") ; Exit $sFileopen = FileOpen($sPathwithquote,0) If $sFileopen = -1 Then MsgBox(0, "Error", "Unable to open file."& $sPathwithquote) Exit EndIf $sText = FileRead($sFileopen ,FileGetSize($sFileopen )) <--snip--> Link to comment Share on other sites More sharing options...
rasim Posted December 21, 2008 Share Posted December 21, 2008 barrybIf you want to get a help, make a small working example, which represent your problem. Link to comment Share on other sites More sharing options...
Developers Jos Posted December 21, 2008 Developers Share Posted December 21, 2008 Hi,I am using the excellent RecurseDir() v2.2 that I found on the forum, I can get it to return an array of all the files I'm searching for, but cannot get FileOpen to open the returned result, so I can do a read / search / replace. I tried the straight path returned e.g c:\temp\doc.txt then added quotes "c:\temp\doc.txt" but no joy the error flag -1 is returned.I know it must be something simple but cannot see it Some pointers would be much appreciated, only been using AutoIt a few weeks.Why are you putting the extra double quotes around the filenames? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
GEOSoft Posted December 21, 2008 Share Posted December 21, 2008 FileOpen() justs sets the mode that the file is using when it's opened Read, Append, Write. It won't actually open it in an editor if thats what you are thinking. For that (if there is a file association) just use ShellExecute($File) or if you want to open them in Notepad then use Run("Notepad.exe " & "$File"). George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
barryb Posted December 21, 2008 Author Share Posted December 21, 2008 Thanks for responding @rasim example code is at the bottom @Jos I added the quotes as it didnt work without and I though as there was spaces in the filename, maybe I needed quotes as it was somehow being truncated. I am more used to VBScript but by no means a code guru!! Would like to use AutoIt for its GUI functons, much nicer than HTA's!! Even with only 2 text files sitting at the root of c:\temp, I get the -1 error capture statement tripping as it cannot open the file. So I'm not even reaching the replace of the text and then moving the original files into a back up directory. If I can the first part workng then I can move on to refining the script. Thanks, Barry expandcollapse popup;Using RecurseDir() v2.2 Function created by Cor (corz) Local $i $dir = "C:\temp" $mask = "*.txt" $Array = RecurseDir ($dir, $mask) If IsArray($Array) Then For $i = 1 to $Array[0] $sTextpath = ($Array[$i]) $sPathwithquote = '"'&$sTextpath&'"' ConsoleWrite ("This is returned :"& $sPathwithquote & "<--------") $Txtfile = FileOpen($sPathwithquote,0) If $Txtfile = -1 Then MsgBox(0, "Error", "Unable to open file."& $sPathwithquote) Exit EndIf $sText = FileRead($sPathwithquote,FileGetSize($sPathwithquote)) $sText = StringReplace($sText, "test", "worked") FileMove($sPathwithquote, "ORIGINAL\", 9) FileWrite($sPathwithquote,$sText) FileClose ($sPathwithquote) Next EndIf func RecurseDir($dir, $mask, $dont_recurse=false, $dump="", $return_dirs=False) ;debug(@ScriptLineNumber & ": " & "func: RecurseDir " & $dir) dim $n_dirnames[333333] ; maximum number of directories which can be scanned local $n_dircount = 0 ; ^ could be set much higher, if required local $n_file local $n_search local $n_tfile local $file_array local $filenames local $filecount local $dircount = 1 local $msg = "error" ; if there was an "\" on the end of the given directory, remove that.. if StringRight($dir, 1) = "\" then $dir = StringTrimRight($dir, 1) $n_dirnames[$dircount] = $dir if not FileExists($dir) then return 0 while $dircount > $n_dircount; keep on looping until all directories are scanned.. $n_dircount += 1 $n_search = FileFindFirstFile($n_dirnames[$n_dircount] & "\*.*") while 1 ; find all subdirs in this directory and store them in a array.. $n_file = FileFindNextFile($n_search) if @error then exitloop ; skip directory references.. if $n_file = "." or $n_file = ".." then continueloop $n_tfile = $n_dirnames[$n_dircount] & "\" & $n_file ; if it's a directory, add it to the list of directories to be processed.. if StringInStr(FileGetAttrib($n_tfile ), "D") and not $dont_recurse then $dircount += 1 $n_dirnames[$dircount] = $n_tfile endif wend FileClose($n_search) ; multiple masks.. if StringInStr($mask, ",", 2) then $mask_array = StringSplit($mask, ",") else; or else create a dummy array.. dim $mask_array[2] = [1, $mask] endif ; loop through the array of masks.. for $mask_c = 1 to $mask_array[0] ; find all files that match this mask.. $n_search = FileFindFirstFile($n_dirnames[$n_dircount] & "\" & $mask_array[$mask_c] ) if $n_search = -1 then continueloop while 1 $n_file = FileFindNextFile($n_search) if @error then exitloop; end of dir if $n_file = "." or $n_file = ".." then continueloop $n_tfile = $n_dirnames[$n_dircount] & "\" & $n_file if not StringInStr(FileGetAttrib( $n_tfile ), "D") then $filecount += 1 $filenames &= $n_tfile & @LF endif wend FileClose($n_search) next wend ; flip to a string and back to remove extraneous entries ; this is quicker than redimming on every loop if $return_dirs then $tmp_str = "" $i = 1 while $n_dirnames[$i] <> "" $tmp_str &= $n_dirnames[$i] & "|" $i += 1 wend $tmp_str = StringTrimRight($tmp_str, 1) $n_dirnames = StringSplit($tmp_str, "|") return $n_dirnames endif $filenames = StringTrimRight($filenames, 1) if $filenames = "" then return 0 $file_array = StringSplit($filenames, @LF) ; dump results to a file.. if $dump then $dump_file = FileOpen($dump, 2) FileWrite($dump_file, $filenames) FileClose($dump_file) endif return($file_array) endfunc Link to comment Share on other sites More sharing options...
GEOSoft Posted December 21, 2008 Share Posted December 21, 2008 (edited) If IsArray($Array) Then For $i = 1 to $Array[0] $sTextpath = ($Array[$i]) $sPathwithquote = '"'&$sTextpath&'"' ConsoleWrite ("This is returned :"& $sPathwithquote & "<--------") $sText = FileRead($sPathwithquote,FileGetSize($sPathwithquote)) If @Error Then MsgBox(0, "Error", "Unable to open file."& $sPathwithquote) Exit EndIf $sText = StringReplace($sText, "test", "worked") FileCopy($sPathwithquote, "ORIGINAL\", 9) $Txtfile = FileOpen($sPathwithquote, 2) If $Txtfile = -1 Then MsgBox(0, "Error", "Unable to open file."& $sPathwithquote) Exit EndIf FileWrite($Txtfile,$sText) FileClose ($Txtfile) Next EndIf Don't mix filenames and handles in file operations and you had the file open in Read mode so you can't write to it. Edit: I should have added that your method would have worked if you had used FileClose($TxtFile) after the FileRead() and before you tried the FileMove() Edited December 21, 2008 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
barryb Posted December 21, 2008 Author Share Posted December 21, 2008 Hi GEOSoft, Thank you for the response, and for the revised code! I would like to understand the difference between filename and filehandle. If I do say $file = "C:\temp\test.txt" does $file become a file handle, or is that still regarded as a file name (albeit assigned a variable?) as I am unsure where my example mixes the two? is it here: $Txtfile = FileOpen($sPathwithquote,0) am I'm tellng AutoIt to file open what it regards as a filename to another filehandle? From reading your code I see that you have done, is not to do a file open, instead to do a file read to store the contents into a string, copied the original file into the backup directory then using the original file name opened the file for writing erasing the existing contents. I also removed the quotes and used the string returned by the array and it works fine. Sorry if the above question is really noobish but I like to try and understand where I'm going wrong and maybe someday help someone else Thanks, Barry Link to comment Share on other sites More sharing options...
Developers Jos Posted December 21, 2008 Developers Share Posted December 21, 2008 (edited) Barry, A filehandle is the "handle" returned by the fileopen statement. example from the helpfile: $filehandle = FileOpen("test.txt", 0) ; Check if file opened for reading OK If $filehandle = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in 1 character at a time until the EOF is reached While 1 $chars = FileRead($filehandle, 1) If @error = -1 Then ExitLoop MsgBox(0, "Char read:", $chars) WEnd FileClose($filehandle) You can also use the filename in several File* functions but then you AutoIt3 will do the Fileopen/File*/Fileclose for you. $a = FileRead("test.txt") ; =============== is equal to ========================= $a = "" $filehandle = FileOpen("test.txt", 0) ; Check if file opened for reading OK If $filehandle = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $a &= FileReadLine($filehandle) If @error = -1 Then ExitLoop WEnd FileClose($filehandle) ;======================================================= Jos Edited December 21, 2008 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
GEOSoft Posted December 21, 2008 Share Posted December 21, 2008 Barry, A filehandle is the "handle" returned by the fileopen statement. example from the helpfile: You can also use the filename in several File* functions but then you AutoIt3 will do the Fileopen/File*/Fileclose for you. [JosI thought I heard the slopes calling George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Developers Jos Posted December 21, 2008 Developers Share Posted December 21, 2008 I thought I heard the slopes calling Nah... still 6 days away but there is plenty of it at the moment.... can't wait SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
barryb Posted December 22, 2008 Author Share Posted December 22, 2008 Thanks for the help guys 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