Jump to content

File functions


Indhu
 Share

Recommended Posts

Hi ppl,

Here I have a code snippet in which I use some of the file functions

1) to get the data from a text file

2) copy it to a variable & 

3) delete the file after use.

The problem I face here is that this snippet works fine for 'path1'. The whole section for 'path2' works only very rare, even if I have the text file. I doesn't work for the majority of time and my scriptcomes to an end. Can someone help me with what am I missing here??

Thanks in advance. 

RunWait(@DesktopDir&"\image-find-1.exe")  
Sleep(100)
;Storing the relevant information
Local $path1 = @DesktopDir&"\mouse1.txt"
If FileExists($path1) Then
   Local $tempopen1 = FileOpen($path1)
   Local $tempread1 = FileReadToArray($path1)
   Local $linecount = 2   
   For $i=0 To $linecount-1
      If $i=0 Then
         $x1 = $tempread1[$i]
      EndIf
      If $i=1 Then
         $y1 = $tempread1[$i]
      EndIf
   Next
   FileFlush($path1)
   FileClose($path1)
   Sleep(500)
   Local $iDelete = FileDelete($path1)
EndIf
RunWait(@DesktopDir&"\image-find-2.exe")  
Sleep(100)
Local $path2 = @DesktopDir&"\mouse2.txt"
If FileExists($path2)Then
   Local $tempopen2 = FileOpen($path2)
   Local $tempread2 = FileReadToArray($path2)
   Local $linecount = 2   
   For $j=0 To $linecount-1
      If $j=0 Then
         $p1 = $tempread2[$j]
      EndIf
      If $j=1 Then
         $q1 = $tempread2[$j]
      EndIf
   Next
   FileFlush($path2)
   FileClose($path2)
   Sleep(500)
   Local $iDelete = FileDelete($path2)
EndIf

P.s: My whole script was working fine a few days back (including the 'path2' issues). I have tried clearing temp files, restarting my system..still with no use. The file 'mouse2' exists & has the data, even after my script comes to an end.  I want to know why this is not working only on the 'path2' sections. 

Link to comment
Share on other sites

Try this one !
By the way : The code is not very attractive, but I left it that way (mostly).

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


; ===> Part 1 :
RunWait(@DesktopDir & "\image-find-1.exe")
If @error Then ConsoleWrite("! Error : image-find-1.exe" & @CRLF)
Sleep(100)

; Storing the relevant information
Local $sPath1, $hTempOpen1, $aTempRead1, $iLinecount1, $sX, $sY
$sPath1 = @DesktopDir & "\mouse1.txt"
If FileExists($sPath1) Then
    ConsoleWrite("+ $sPath1 = " & $sPath1 & " found" & @CRLF)

    Local $hTempOpen1 = FileOpen($sPath1) ; <== returns a handle
    Local $aTempRead1 = FileReadToArray($hTempOpen1)
    _ArrayDisplay($aTempRead1, "mouse1.txt")

    $iLinecount1 = 2

    For $i = 0 To $iLinecount1 - 1
        If $i = 0 Then
            $sX = $aTempRead1[$i]
        EndIf
        If $i = 1 Then
            $sY = $aTempRead1[$i]
        EndIf
    Next
;~  FileFlush($hTempOpen1)
    FileClose($hTempOpen1)
    Sleep(500)
    Local $iDelete = FileDelete($sPath1)

    ConsoleWrite("< $sX = " & $sX & "    $sY = " & $sY & @CRLF)
Else
    ConsoleWrite("! $sPath1 = " & $sPath1 & " not found" & @CRLF)
EndIf


; ===> Part 2 :
RunWait(@DesktopDir & "\image-find-2.exe")
If @error Then ConsoleWrite("! Error : image-find-2.exe" & @CRLF)
Sleep(100)

;Storing the relevant information
Local $sPath2, $hTempOpen2, $aTempRead2, $iLinecount2, $sP, $sQ
$sPath2 = @DesktopDir & "\mouse2.txt"
If FileExists($sPath2) Then
    ConsoleWrite("+ $sPath2 = " & $sPath2 & " found" & @CRLF)

    Local $hTempOpen2 = FileOpen($sPath2) ; <== returns a handle
    Local $aTempRead2 = FileReadToArray($hTempOpen2)
    _ArrayDisplay($aTempRead2, "mouse2.txt")

    $iLinecount2 = 2

    For $i = 0 To $iLinecount2 - 1
        If $i = 0 Then
            $sP = $aTempRead2[$i]
        EndIf
        If $i = 1 Then
            $sQ = $aTempRead2[$i]
        EndIf
    Next
;~  FileFlush($hTempOpen2)
    FileClose($hTempOpen2)
    Sleep(500)
    Local $iDelete = FileDelete($sPath2)

    ConsoleWrite("< $sP = " & $sP & "    $sQ = " & $sQ & @CRLF)
Else
    ConsoleWrite("! $sPath2 = " & $sPath2 & " not found" & @CRLF)
EndIf

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

1 hour ago, Musashi said:

Try this one !

Thank u for ur answer @Musashi, but the results are same. I want to delete the file 'mouse2.txt' after copying its contents to the variables '$sP' and '$sQ'. But the file is not getting deleted. What I want to know is, how this appraoch worked with 'mouse1.txt' and why it is not working the same with 'mouse2.txt'?

Link to comment
Share on other sites

1 hour ago, Musashi said:

By the way : The code is not very attractive, but I left it that way (mostly)

Oh, and by the way, if u feel ur way of coding is more attractive, then by all means u can share that too. I wouldn't mind using the extra help (which is why i'm here) ☺️ 

Link to comment
Share on other sites

What @Musashi wanted to show you is how to debug your script, not to solve it.  ConsoleWrite and ArrayDisplay are there to trace the path of the script.  With that you should understand why your script is not working properly.  If you still need help, please provide images of the Scite Console and the ArrayDisplay as well.

Btw, your fileopen, fileflush and fileclose are useless.

Link to comment
Share on other sites

2 minutes ago, Nine said:

Btw, your fileopen, fileflush and fileclose are useless.

yes. i know these are useless with 'part2' section (in Musashi's code). bt it was working fine with the 'part1' (in Musashi's code). I got the data stored in 'mouse1.txt' copied to x1, y1, variables, after which the file 'mouse1.txt' got deleted. That is I what i wanted to know, why the file functions wouldn't work with the 'mouse2.txt'. This is the ArrayDisplay for 'mouse1'. 

image.png.93beb36829796bf28d22c41aeb5d034f.png

my console window

image.png.bb2e2d75df65d666297dadb0194bdba9.png

here i have it as 'mouse2.txt' is not found, which is absurd, coz the file exists. But none of the file functions are not working on it, which is what i want to know why, and the same worked fine with 'mouse1.txt', the result of which is the array.

 

Link to comment
Share on other sites

Link to comment
Share on other sites

1 hour ago, Nine said:

What @Musashi wanted to show you is how to debug your script, not to solve it.

Thank's @Nine , this is what I have intended.

@Indhu here an extended DEBUG version :

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

; Version 2 : 2019/12/05 15:10

; ===> Part 1 :
ConsoleWrite("> ============ Part 1 ================" & @CRLF)

RunWait(@DesktopDir & "\image-find-1.exe")
If @error Then ConsoleWrite("! Error : image-find-1.exe" & @CRLF)
Sleep(2000)

; Storing the relevant information
Local $sPath1, $aTempRead1, $iLinecount1, $sX, $sY
$sPath1 = @DesktopDir & "\mouse1.txt"
If FileExists($sPath1) Then
    ConsoleWrite("+ $sPath1 = " & $sPath1 & " found" & @CRLF)

    Local $aTempRead1 = FileReadToArray($sPath1)
    ConsoleWrite("> FileReadToArray : @error = " & @error & "  @extended (No. of lines) = " & @extended & @CRLF)
    _ArrayDisplay($aTempRead1, "mouse1.txt")

    $iLinecount1 = 2

    For $i = 0 To $iLinecount1 - 1
        If $i = 0 Then
            $sX = $aTempRead1[$i]
        EndIf
        If $i = 1 Then
            $sY = $aTempRead1[$i]
        EndIf
    Next
    Sleep(500)

    ; show File attributes :
    ConsoleWrite("< File attributes = <" & AttributeToString(FileGetAttrib($sPath1)) & ">" & @CRLF)

    Local $iDelete = FileDelete($sPath1)
    If $iDelete Then
        ConsoleWrite("< " & $sPath1 & "  ==> successfuly deleted" & @CRLF)
    Else
        ConsoleWrite("! " & $sPath1 & "  ==> not deleted" & @CRLF)
    EndIf

    ConsoleWrite("< $sX = " & $sX & "    $sY = " & $sY & @CRLF)
Else
    ConsoleWrite("! $sPath1 = " & $sPath1 & " not found" & @CRLF)
EndIf


; ===> Part 2 :
ConsoleWrite("> ============ Part 2 ================" & @CRLF)

RunWait(@DesktopDir & "\image-find-2.exe")
If @error Then ConsoleWrite("! Error : image-find-2.exe" & @CRLF)
Sleep(2000)

;Storing the relevant information
Local $sPath2, $aTempRead2, $iLinecount2, $sP, $sQ
$sPath2 = @DesktopDir & "\mouse2.txt"
If FileExists($sPath2) Then
    ConsoleWrite("+ $sPath2 = " & $sPath2 & " found" & @CRLF)

    Local $aTempRead2 = FileReadToArray($sPath2)
    ConsoleWrite("> FileReadToArray : @error = " & @error & "  @extended (No. of lines) = " & @extended & @CRLF)
    _ArrayDisplay($aTempRead2, "mouse2.txt")

    $iLinecount2 = 2

    For $i = 0 To $iLinecount2 - 1
        If $i = 0 Then
            $sP = $aTempRead2[$i]
        EndIf
        If $i = 1 Then
            $sQ = $aTempRead2[$i]
        EndIf
    Next
    Sleep(500)

    ; show File attributes :
    ConsoleWrite("< File attributes = <" & AttributeToString(FileGetAttrib($sPath2)) & ">" & @CRLF)

    Local $iDelete = FileDelete($sPath2)
    If $iDelete Then
        ConsoleWrite("< " & $sPath2 & "  ==> successfuly deleted" & @CRLF)
    Else
        ConsoleWrite("! " & $sPath2 & "  ==> not deleted" & @CRLF)
    EndIf

    ConsoleWrite("< $sP = " & $sP & "    $sQ = " & $sQ & @CRLF)
Else
    ConsoleWrite("! $sPath2 = " & $sPath2 & " not found" & @CRLF)
EndIf


; From the AutoIt-Help : show friendlier file attribute names
Func AttributeToString($sAttribute)
    Local $aInput = StringSplit("R,A,S,H,N,D,O,C,T", ",")
    Local $aOutput = StringSplit("Read-only /, Archive /, System /, Hidden /" & _
            ", Normal /, Directory /, Offline /, Compressed /, Temporary /", ",")
    For $i = 1 To $aInput[0]
        $sAttribute = StringReplace($sAttribute, $aInput[$i], $aOutput[$i], 0, $STR_CASESENSE)
    Next
    $sAttribute = StringTrimRight($sAttribute, 2)
    Return $sAttribute
EndFunc   ;==>AttributeToString

Please post the console outout .

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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