bloopie

Array to File issue

20 posts in this topic

#1 ·  Posted (edited)

Hello all,

I'm having an issue getting a file written from an array. I'm still new to AutoIt so go easy on my scripting skills :lol:! I have also been chopping this section up and down for some time trying to get this working, but anyway...here is my relevant code:

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>


HotKeySet("{f1}", "Terminate")

Global $aColletionArray


RecursiveSearchArray()
ArrayWriteToFile()

Func RecursiveSearchArray()
    Local $hTimer = TimerInit()
    $aColletionArray = _FileListToArrayRec(@HomeDrive & "\", "Rkill.txt;FSS.txt;FSSExport.txt||Dropbox;Atwin;Geany;Nmap;Python*;Sublime*;*cortana*", 1, 1)
    Local $fTimeDiff = TimerDiff($hTimer)
    MsgBox(0, "File Gathering Complete!", "This search took " & StringLeft(($fTimeDiff / 1000), 5) & " Seconds to complete.")
    If IsArray($aColletionArray) Then _ArrayDisplay($aColletionArray, "Files Collected", "", 8)
EndFunc   ;==>RecursiveSearchArray

Func ArrayWriteToFile()
    Local $sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
    If $sFileToWrite = -1 Then MsgBox(0, "ERROR", "Error: " & @error & @CRLF & " - " & " Extended: " & @extended)
    _FileWriteFromArray($sFileToWrite, $aColletionArray)
    If @error Then MsgBox(0, "Error", @error & @CRLF & @extended & @CRLF)
    ShellExecute($sFileToWrite)
EndFunc   ;==>ArrayWriteToFile

Func Terminate()
    Exit
EndFunc   ;==>Terminate

The search always ~10s to complete, and I get the array displayed with three files listed (as expected), then I get nothing more and the script exits normally. I get no msgbox with any errors, no file opened...nothing. _FileCreate didn't work for me either. Am I missing something silly here, or am I just going about this the wrong way?

EDIT: The reason I use _FileListToArrayRec is because on other machines I won't know how many files will be pulled (and this is only a couple of files for testing...the real script could have possibly 10 or more files pulled, or no files pulled)

My Version is 3.3.14.2

Thanks in advance for any light shed on my ignorance! :)

bloopie

Edited by bloopie
EDIT:

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

1 hour ago, bloopie said:

ShellExecute($sFileToWrite)

"$sFileToWrite" should be a string, not a handle when passed into ShellExecute. Like:

ShellExecute(@DesktopDir & "\brandnewfile.txt")

I would also suggest calling FileClose after writing, just for good measure.

Rest of the script works fine it seems.

Edited by genius257

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello genius, and thanks,

But I did also try exactly that earlier, and got the exact same result (or lack there of). When it didn't work the first time I thought that there must be a filepath passed with shellexecute, and so I did exactly what you have suggested...still no result.

EDIT: And yes, I have also listed FileClose and FileDelete in testing, but there is no file to do so with. :(

Is there another method I could use to debug or something? Maybe setting @extended macro's (I've never really applied these before because the help file is not very descriptive with them)?

bloopie

Edited by bloopie
the EDIT:

Share this post


Link to post
Share on other sites

#4 ·  Posted

bloopie,

This works for me.  Note comments in code...

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>


HotKeySet("{f1}", "Terminate")

Global $aColletionArray

RecursiveSearchArray()
ArrayWriteToFile()

Func RecursiveSearchArray()
    Local $hTimer = TimerInit()
    $aColletionArray = _FileListToArrayRec(@HomeDrive & "\", "Rkill.txt;FSS.txt;FSSExport.txt||Dropbox;Atwin;Geany;Nmap;Python*;Sublime*;*cortana*", 1, 1)
    Local $fTimeDiff = TimerDiff($hTimer)
    MsgBox(0, "File Gathering Complete!", "This search took " & StringLeft(($fTimeDiff / 1000), 5) & " Seconds to complete.")
    If IsArray($aColletionArray) Then _ArrayDisplay($aColletionArray, "Files Collected", "", 8)
EndFunc   ;==>RecursiveSearchArray

Func ArrayWriteToFile()
    Local $sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
    If $sFileToWrite = -1 Then MsgBox(0, "ERROR", "Error: " & @error & @CRLF & " - " & " Extended: " & @extended)
    _FileWriteFromArray(@DesktopDir & "\brandnewfile.txt", $aColletionArray)    ; <- file path instead of handle
    If @error Then MsgBox(0, "Error", @error & @CRLF & @extended & @CRLF)
    fileclose($sFileToWrite)                                                    ; <- close file
    ShellExecute(@DesktopDir & "\brandnewfile.txt")                             ; <- file path instead of handle
EndFunc   ;==>ArrayWriteToFile

Func Terminate()
    Exit
EndFunc   ;==>Terminate

kyhlomas

1 person likes this

Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#5 ·  Posted

7 hours ago, bloopie said:

FileDelete

Don't use FileDelete. The flags you have chosen in FileOpen will overwrite the file. So in testing, there's no reason to delete it just yet.

No error or anything for me, file opens fine also.

Func ArrayWriteToFile()
    Local $sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
    _FileWriteFromArray($sFileToWrite, $aColletionArray)
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
    FileClose($sFileToWrite)
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
    ShellExecute(@DesktopDir & "\brandnewfile.txt")
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
EndFunc   ;==>ArrayWriteToFile

 

1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted

Hey, and thanks for the replies and info! :) I'll get to testing that in a bit....

==========

_FileWriteFromArray(@DesktopDir & "\brandnewfile.txt", $aColletionArray)    ; <- file path instead of handle

That's interesting, because the help file on _FileWriteFromArray states you can use the path, OR the handle returned from FileOpen...I guess that's not always the case then? Interesting to note, thanks for that!

Quote

Don't use FileDelete. The flags you have chosen in FileOpen will overwrite the file. So in testing, there's no reason to delete it just yet.

I know, I was just letting you know what I've been doing while butchering the script to get it to work...lol! :) And thanks for the extra Debugging concepts there...it certainly does become much more easy to read when you script it that way writing to the Console. :thumbsup:

==========

...Honestly, I really never considered the help file to be inaccurate...but I guess when working with UDF's, it's best to keep the 'variable passing' to a minimum until you're sure everything is working the way it should.

Many thanks for the assistance both of you, it is greatly appreciated! 

==========

...One other question: Is this the best and/or the fastest way to retrieve multiple files from unknown locations on a machine? The search could take quite a long time when running through the entire drive and many different folders as well, so scan times can be an issue...do you suggest another method, or is this the best way with AutoIt?

bloopie

Share this post


Link to post
Share on other sites

#7 ·  Posted

5 minutes ago, bloopie said:

That's interesting, because the help file on _FileWriteFromArray states you can use the path, OR the handle returned from FileOpen...I guess that's not always the case then? Interesting to note, thanks for that!

@genius257 wasn't telling to not use the file handle with _FileWriteFromArray, @genius257 was telling to not use it with the ShellExecute function. _FWTA will work with a file handle or a file path.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I understood what genious wrote...but I was responding to kylomas re-write of the code with that statement, not genious's (I quoted the line with kyloma's comment in it) ...kyloma wrote that comment to use the filepath when I originally had the handle in that line of _FWFA.

If you could use either with _FWFA, then why the comment?

(Sorry if I wasn't clear)

bloopie

Edited by bloopie
_FWFA actually + clarification

Share this post


Link to post
Share on other sites

#9 ·  Posted

bloopie,

That is just how I got it to work.  The help file states clearly that you can use either path or handle.  Apologies for the ambiguity.

kylomas


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

That's no problem, and no worries...I'm just wondering why you changed it if it didn't need to be changed (kinda made me question the help file is all).

Thanks again. :)

bloopie

EDIT: Oh, yeah...any comment on my other question?:

Quote

Is this the best and/or the fastest way to retrieve multiple files from unknown locations on a machine? The search could take quite a long time when running through the entire drive and many different folders as well, so scan times can be an issue...do you suggest another method, or is this the best way with AutoIt?

 

Edited by bloopie
the EDIT:

Share this post


Link to post
Share on other sites

#11 ·  Posted

1 hour ago, bloopie said:

(kinda made me question the help file is all).

The Help file is solid.  Guinness, et al did an excellent job with it.

I noticed the longish runtimes also when using @homedrive (C: on my setup).  Whether or not this is the best way to get files is more of an organizational issue.

kylomas


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#12 ·  Posted

Okay, after testing with updated code(s), I still see no discernible difference between this and what I had before beginning the thread. I still get nothing after the _ArrayDisplay!

Even when running the "Debugging" code by genius, I still get nothing after the Array Displays, and then a normal exit. When I began the thread, I was using my work machine...and now I still get the same result on my home machine. They are nearly the same computers as well (same mobos, and same processors (home processor is slightly faster), both running Windows 10 pro). I can't see why the script won't run the second function (at least it seems that way).

...Wait, that's what's happening!!....the second function is not running at all! I just put a 5 second sleep in the second function, and it never processed that sleep! I'm going to try to rename the second function or something else...maybe I gave it a conflicting name...?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Try a consolewrite as the first statement in the second function.  Post the current code, please.

Edited by kylomas

Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

7 hours ago, bloopie said:

...Wait, that's what's happening!!....the second function is not running at all! I just put a 5 second sleep in the second function, and it never processed that sleep! I'm going to try to rename the second function or something else...maybe I gave it a conflicting name...?

Create an entire new script and try the code below. If it does not work, i would check the directory you are trying to access and if there's nothing there, i would try and approach the developers of AutoIt about a very specific bug ^^'

#include <File.au3>
#include <FileConstants.au3>

$aColletionArray = _FileListToArrayRec(@HomeDrive & "\", "Rkill.txt;FSS.txt;FSSExport.txt||Dropbox;Atwin;Geany;Nmap;Python*;Sublime*;*cortana*", 1, 1)
$sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
If $sFileToWrite = -1 Then Exit
_FileWriteFromArray($sFileToWrite, $aColletionArray)
FileClose($sFileToWrite)
ShellExecute(@DesktopDir & "\brandnewfile.txt")

Edit:

Updated FileOpen error checking thank to @kylomas

Edited by genius257
Changed _FileListToArrayRec parameters

Share this post


Link to post
Share on other sites

#15 ·  Posted

g257,

You probably mean this

#include <File.au3>
#include <FileConstants.au3>

$aColletionArray = _FileListToArrayRec(@HomeDrive & "\", "Rkill.txt;FSS.txt;FSSExport.txt||Dropbox;Atwin;Geany;Nmap;Python*;Sublime*;*cortana*", 1, 1)
If @error<>0 Then Exit
$sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
_FileWriteFromArray($sFileToWrite, $aColletionArray)
FileClose($sFileToWrite)
ShellExecute(@DesktopDir & "\brandnewfile.txt")

kylomas


Forum Rules         Procedure for posting code

Share this post


Link to post
Share on other sites

#16 ·  Posted

17 minutes ago, kylomas said:

You probably mean this

No but i see why you would think that ^^'

I thought FileOpen set @error on failure, but apparently checking the return value is the only way :$

So it's:

If $sFileToWrite = -1 Then Exit

 

Share this post


Link to post
Share on other sites

#17 ·  Posted

18 hours ago, kylomas said:

Try a consolewrite as the first statement in the second function.  Post the current code, please.

Okay, gave the consolewrite a go, and still nothing....here is the current code with only minor adjustments to make the search faster and return a couple of more files that I currently have on this system:
 

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>

HotKeySet("{f1}", "Terminate")

Global $aColletionArray

RecursiveSearchArray()
ArrayWriteToFile()

Func RecursiveSearchArray()
    Local $hTimer = TimerInit()
    $aColletionArray = _FileListToArrayRec(@HomeDrive & "\", "MTB.txt;FSS.txt;FRST.txt;Addition.txt||Dropbox;Nmap;Geany;Python*;Sublime*;*cortana*", 1, 1)
    Local $fTimeDiff = TimerDiff($hTimer)
    MsgBox(0, "File Gathering Complete!", "This search took " & StringLeft(($fTimeDiff / 1000), 5) & " Seconds to complete.")
    If IsArray($aColletionArray) Then _ArrayDisplay($aColletionArray, "Files Collected", "", 8)
EndFunc   ;==>RecursiveSearchArray

Func ArrayWriteToFile()
    ConsoleWrite(@CRLF & "----THIS IS TEST TEXT----" & @CRLF & "----THIS IS MORE TEST TEXT----" & @CRLF)
    Sleep(5000)
    Local $sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
    If $sFileToWrite = -1 Then MsgBox(0, "ERROR", "Error: " & @error & @CRLF & " - " & " Extended: " & @extended)
    _FileWriteFromArray(@DesktopDir & "\brandnewfile.txt", $aColletionArray)
    If @error Then MsgBox(0, "Error", @error & @CRLF & @extended & @CRLF)
    ShellExecute(@DesktopDir & "\brandnewfile.txt")
    FileClose($sFileToWrite)
EndFunc   ;==>ArrayWriteToFile

Func Terminate()
    Exit
EndFunc   ;==>Terminate

#cs
Func ArrayWriteToFile()
    Local $sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
    _FileWriteFromArray($sFileToWrite, $aColletionArray)
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
    FileClose($sFileToWrite)
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
    ShellExecute(@DesktopDir & "\brandnewfile.txt")
    ConsoleWrite("Debug at line: "&@ScriptLineNumber&@CRLF&@TAB&"@error: "&@error&@TAB&","&"@extended: "&@extended&@CRLF)
EndFunc   ;==>ArrayWriteToFile
#ce

This above code still fails at the second function...no consolewrite at all, and exits normally....

==========

However, it indeed looks like we have a bug...when I run genius's test code, it works just fine...I get a "brandnewfile.txt" opened with the contents of the array written in it!

I also tried to comment out the "If @error Then MsgBox(0, "Error", @error & @CRLF & @extended & @CRLF)" statement, but it still fails. I don't know if it's a function-related issue yet or not, I'll need to test more to find out where it fails and under what circumstance. Very interesting indeed! :)

Thanks again,

bloopie

Share this post


Link to post
Share on other sites

#18 ·  Posted

Okay, I figured out what the problem is. There is indeed a bug.

It's the _ArrayDisplay UDF causing the script to stop. After thinking on it for a few minutes, I realized _ArrayDisplay was always the last thing that worked in every case, so I added in _ArrayDisplay to genius's test code, and after displaying the array, it exited normally!

So there is the bug....but I've got to test some other scenarios to see what else might make a script fail with it. ...Just for the record, I have commented out the _ArrayDisplay line and the code completes correctly. But, UNcomment that _ArrayDisplay line, and the script fails immediately after displaying the array:
 

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>

HotKeySet("{f1}", "Terminate")

RecursiveFileSearchArrayFailsDisplay()

Func RecursiveFileSearchArrayFailsDisplay()
    $aColletionArray = _FileListToArrayRec(@HomeDrive & "\", "Rkill.txt;FSS.txt;FSSExport.txt||Dropbox;Atwin;Geany;Nmap;Python*;Sublime*;*cortana*", 1, 1)
;~     _ArrayDisplay($aColletionArray)
    $sFileToWrite = FileOpen(@DesktopDir & "\brandnewfile.txt", 8 + 2 + 512)
    If $sFileToWrite = -1 Then Exit
    _FileWriteFromArray($sFileToWrite, $aColletionArray)
    FileClose($sFileToWrite)
    ShellExecute(@DesktopDir & "\brandnewfile.txt")
EndFunc   ;==>RecursiveFileSearchArrayFailsDisplay

Func Terminate()
    Exit
EndFunc   ;==>Terminate

==========

I'm not ready to post it in the Bug Tracker because on first glance, I have much more work to do to see if it actually belongs there (discussed prior...etc)! If maybe kylomas already knows it should be there, then by all means. :lol:

Big thanks to @kylomas and @genius257 for steering me in the right direction!

bloopie

Share this post


Link to post
Share on other sites

#19 ·  Posted

How are you closing the _ArrayDisplay box? Also, what do you mean by " the script fails immediately after displaying the array"?

I modified that script to return only txt files to a depth of -2, and the _ArrayDisplay showed 719 files, and then the text file opened showing the same 719 files. So, it's not failing for me.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#20 ·  Posted

30 minutes ago, BrewManNH said:

How are you closing the _ArrayDisplay box?

Well you've pinpointed it for me, thank you BrewMan!! ...it's not a bug (at least I don't think so anymore)! I've been using my Esc button (on my wireless keyboard and mouse combo) which never failed me in the past and that became my 'go-to' button for closing ArrayDisplay boxes...however, after you asked that question I looked at my setup again, and changed keyboards to a wired one...then it worked flawlessly...no problems at all!

I've been testing this over and over now with the wired keyboard, and it's working every time now! I can't believe my keyboard is what caused that!!! :(

==========

...I was sure I used the "x" close button a few times as well, but it may have been under different circumstances (when running no second function for testing I suppose)...I also did use the "copy" options after the array displayed, but still went back to the Esc button to close the display.

That's quite a bit of an anti-climax to this thread (if there ever was one), but thanks to BrewManNH for seeing the one thing I did not look at during testing...my keyboard!!

The funny thing is that I have the exact same logitec wireless keyboard and mouse combo at work, as well as home (because I liked it so much)!

59 minutes ago, BrewManNH said:

Also, what do you mean by " the script fails immediately after displaying the array"?

Sorry, I meant to say the script "exits" immediately after displaying the array...i.e. after I pressed Esc

...So that's it...problem 'kinda' solved due to a non standard keyboard and mouse combo...still not sure why it won't work with my wireless's, but I'll save that testing for another day. I've already spent too much time on this one issue to begin with.

A great big thanks for everyone who replied and offered help on this weird one! Much is appreciated! :)

bloopie

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