Jump to content

erase txt when script close , without filedelete


faustf
 Share

Recommended Posts

hi guy 

i have  a  script , but it  happen somthing  very strange ,  go all ok this  script  grab  html link and  name  of page web  and  write  in  txt , but  whenclose  one of txt go away , how  is possible ??? 

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.12.0
    Author:         faustf

    Script Function:
    GROSSISTI GRABBER
    BY faustf

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <InetConstants.au3>
#include <WinAPI.au3>
#include <WinAPIsysinfoConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#include <INet.au3>
#include <Excel.au3>
#include <File.au3>



; # VARIABILI GLOBALI ===============================================================================================

Global $oIE, $CONTROLLO_PROFONDITA = 0, $gruppi_N_link, $gruppi_N_nomi
Global $percorso = (@ScriptDir & "\Espri_temp")
Global $gruppi_merciologici[9999], $A = 0
Global $link_gruppo, $gruppo, $lettera_excel = 0, $numeri_excel = 1
Global $oWorkbook, $aArray, $Nomi_Menu, $aArray1, $Link_Menu

; ===================================================================================================================


_FileBorn($percorso & "\Gruppi_" & $A & ".txt") ;  controllo se cè file  per listare i gruppi
_FileBorn($percorso & "\Gruppi_link_" & $A & ".txt") ;  controllo se cè file  per listare i link dei gruppi

_entra_dentro() ;  entra dentro il sito web
_excel_Crea()

Local $sHTML = _IEBodyReadHTML($oIE)

_inizio()


Func _inizio()
    Select
        Case $CONTROLLO_PROFONDITA = 0
            $aArray = StringRegExp($sHTML, '(?i)<span class="nomeCat" abp="(.*?)">(.*?)</span>', $STR_REGEXPARRAYGLOBALMATCH) ; per estrarre i gruppi pag. 0
            $Nomi_Menu = ($percorso & "\Gruppi_" & $A & ".txt")
            $aArray1 = StringRegExp($sHTML, '(?i)class="btn btn-mini gradient" href="(.*?)" abp="', $STR_REGEXPARRAYGLOBALMATCH) ;per estrarre i link da oag. 0
            $Link_Menu = ($percorso & "\Gruppi_link_" & $A & ".txt")

            _estrazioni_menu()

            $gruppi_N_nomi = _FileCountLines($Nomi_Menu)
            $gruppi_N_link = _FileCountLines($Link_Menu)


            ; idea con gruppi_n_nomi so quanti  cicli fare ,  ciclo questo sotto, e in mezzo a  quello verifico se non sono nei filtri e  se sono ne filtri
            ; estraggo  tutto e incremento numeri_excell

            $gruppo = _True_FileReadLine($Nomi_Menu, 1)
            _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $gruppo, Chr(Asc("A") + $lettera_excel) & $numeri_excel)
            $link_gruppo = _True_FileReadLine($Link_Menu, 1)

            $sFileRead = _Read_Canc_1_riga($Link_Menu, 1, 0)

            ;MsgBox(0, 'percorso', $sFileRead)
            _IENavigate($oIE, $sFileRead)


            Local $bodi_filtro = _IEGetObjById($oIE, "miniFiltri")
            ;*********************
            If $bodi_filtro <> "" Then ;  controllo se sono nella pagina filtro se ci sono inizio la procedura di estrazione dei prodotti

                ;_estrazione_prodotti() ;finite tutte le estrazioni torna indietro

            Else
                MsgBox(0, '', 'de ma de')

                Exit
            EndIf



        Case $CONTROLLO_PROFONDITA = 1

              MsgBox(0, '', '1')
            ;_estrazioni_menu()
    EndSelect
EndFunc   ;==>_inizio

Func _excel_Crea()
    Local $oAppl = _Excel_Open()
    If @error Then Exit MsgBox(16, "Excel UDF: _Excel_BookOpen Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    $oWorkbook = _Excel_BookNew($oAppl)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen ", "Error opening '" & $oWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
EndFunc   ;==>_excel_Crea


Func _estrazioni_menu()
    ;===============================================================================================================================================
    ;==================================  GRUPPI MERCIOLOGICI - ESTRAZIONE NOMI E LINK   ============================================================
    ;===============================================================================================================================================

    ; --------------  INIZIO RACCOLTA GRUPPI ------------------------------------------
    For $i = 0 To UBound($aArray) - 1

        If Not Mod($i, 2) Then
            ;Even
        Else
            ;Odd
            ; MsgBox($MB_SYSTEMMODAL, "dispari " & $i, $aArray[$i])
            _crea_txt_menu($Nomi_Menu, $aArray[$i])

        EndIf
    Next
    ;MsgBox(0,'de',$gruppi_merciologici[0])
    ;----------------------------------------------------------------------------------

    ; --------------  INIZIO RACCOLTA LINK GRUPPI -------------------------------------
    For $i = 0 To UBound($aArray1) - 1
        ;MsgBox($MB_SYSTEMMODAL, "gruppi " & $i, "http://it.esprinet.com"& $aArray[$i])               ; link alle categorie
        _crea_txt_menu($Link_Menu, "http://it.esprinet.com" & $aArray1[$i])

    Next
    ;----------------------------------------------------------------------------------
    $CONTROLLO_PROFONDITA += 1
    ;===============================================================================================================================================
    ;===============================================================================================================================================
    ;===============================================================================================================================================
EndFunc   ;==>_estrazioni_menu





; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: legge la  riga  di testo che  gli indichi $sFilePath = percorso txt ,   $riga_num= numero di riga
; ...............:
; ===============================================================================================================================
Func _True_FileReadLine($sFilePath, $riga_num)

    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, $riga_num)

    Return $sFileRead
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)


EndFunc   ;==>_True_FileReadLine



; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: In conbinazione con  _fileDelteLine_Shift  legge la riga e la cancella . se legge la riga $num_riga =1 devo mettere
; ...............: $FDLS_line = 0 sevoglio che lavori sulla prima riga del txt altrimenti fa letture e cancellamenti sfalzati
; ===============================================================================================================================
Func _Read_Canc_1_riga($percorso_da_leggere, $num_riga, $FDLS_line)

    Local $hFileOpen = FileOpen($percorso_da_leggere, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Errore di lettura  file.")
        Return False
    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    $sFileRead = FileReadLine($hFileOpen, $num_riga)
    FileClose($hFileOpen)

    _File_DeleteLine_Shift($percorso_da_leggere, $FDLS_line)


    Return $sFileRead
EndFunc   ;==>_Read_Canc_1_riga

; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: $path = scrivo il percorso del txt ,$line = scrivo la linea che voglio cancellare lui cancella e shifta tutto
; ...............: contenuto togliendo la liena vuota. il conteggio lienee parte da 0
; ===============================================================================================================================

Func _File_DeleteLine_Shift($path, $line)

    If $line = "" Then
        $line = 1
    EndIf
    _FileReadToArray($path, $aArray)
    Local $iDelete = FileDelete($path)
    _FileCreate($path)
    $file = FileOpen($path, 1)

    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
    Else

        For $i = 1 To UBound($aArray) - 1 ; Loop through the array.

            If $i = $line Then
                ;MsgBox (0,'', $aArray[$i])
            Else
                ;If $aArray[$i] = 34 Then

                ;Else
                FileWrite($file, $aArray[$i] & @CR)
                ;EndIf

            EndIf
        Next

    EndIf

    ; Close the handle returned by FileOpen.
    FileClose($file)
EndFunc   ;==>_File_DeleteLine_Shift


; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: entro dentro il sito
; ===============================================================================================================================

Func _entra_dentro()

    $oIE = _IECreate("http://it.esprinet.com/public/?returnurl=%2Fhome%2Easp", 0, 1, 1, 1)

    Local $oForm = _IEFormGetObjByName($oIE, "arearis")
    Local $username = _IEFormElementGetObjByName($oForm, "utente")
    _IEFormElementSetValue($username, "*********")
    Local $pass = _IEFormElementGetObjByName($oForm, "pwd")
    _IEFormElementSetValue($pass, "********")
    _IEFormSubmit($oForm, 0)
    Sleep(1000)
    _IEImgClick($oIE, "Entra nel sito", "alt")
    _IENavigate($oIE, "http://it.esprinet.com/Area_Operativa/DServizi_jolly_Speciale_BID.asp", 1)
    _IEImgClick($oIE, "btn-Fantaricerca.png", "src")

EndFunc   ;==>_entra_dentro

; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: crea txt con link dei menu
; ===============================================================================================================================
Func _crea_txt_menu($sFilePath, $sFlieTxt)

    $file = FileOpen($sFilePath, 1)
    FileWrite($file, $sFlieTxt & @CR)
    FileClose($file)

EndFunc   ;==>_crea_txt_menu


; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: controlla se il file esiste nella cartella se si lo cancella e ne crea uno nuovo vuoto, seno non fa nulla
; ===============================================================================================================================

Func _FileBorn($sFilePath)

    Local $iFileExists = FileExists($sFilePath)
    If $iFileExists Then
        FileDelete($sFilePath)
        _FileCreate($sFilePath)
    Else
        _FileCreate($sFilePath)
    EndIf
EndFunc   ;==>_FileBorn
Link to comment
Share on other sites

I'm not sure what is going on in your code, but I find it strange that you use both FileDelete and _FileCreate together like you do for the same file.

_FileCreate not only creates a file, but if the file already exists, it nulls it (wipes it's content).

You seem to do this in two or more places, and so it could be your issue?

I would just stick to using _FileCreate

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

but not wipes a content  wips  all file , and  is  a strange is  i tryed  to insert  this  at end  

;===============================================================
;Keep the GUI alive
;===============================================================
While 1
    Sleep(1000)
WEnd

and  after include  i  write  this 

Opt("GuiOnEventMode", 1)
Opt("GUICloseOnESC", 0)

i notice , the program  run correctly  (but  with code-add not  close ) and  if  i open folder  i  see a  txt  open  and is  all correct  ,but  if  i go  near  the clock and kill the program ....bum remove immidiatly a  txt 

Link to comment
Share on other sites

hi  guy 

i try  to resolve  problem , i use  a  little  version and    go perfect  , i think the problem is in another part of program :)

#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <InetConstants.au3>
#include <WinAPI.au3>
#include <WinAPIsysinfoConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#include <INet.au3>
#include <Excel.au3>
#include <File.au3>

; # VARIABILI GLOBALI ===============================================================================================

Global $oIE, $CONTROLLO_PROFONDITA = 0, $gruppi_N_link, $gruppi_N_nomi
Global $percorso = (@ScriptDir & "\Espri_temp_test")
Global $gruppi_merciologici[9999], $A = 0
Global $link_gruppo, $gruppo_0, $lettera_excel = 0, $numeri_excel = 1
Global $oWorkbook, $aArray, $Nomi_Menu, $aArray1, $Link_Menu, $sHTML

Global $gruppi_N_nomi_1,$gruppi_N_link_1,$gruppo_1,$sFileRead_1
; ===================================================================================================================








$Link_Menu = ($percorso & "\Gruppi_link_0.txt")

$sFileRead_1 = _Read_Canc_1_riga($Link_Menu, 1, 0)
; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: In conbinazione con  _fileDelteLine_Shift  legge la riga e la cancella . se legge la riga $num_riga =1 devo mettere
; ...............: $FDLS_line = 0 sevoglio che lavori sulla prima riga del txt altrimenti fa letture e cancellamenti sfalzati
; ===============================================================================================================================
Func _Read_Canc_1_riga($percorso_da_leggere, $num_riga, $FDLS_line)

    Local $hFileOpen = FileOpen($percorso_da_leggere, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Errore di lettura  file.")
        Return False
    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    $sFileRead = FileReadLine($hFileOpen, $num_riga)
    FileClose($hFileOpen)

    _File_DeleteLine_Shift($percorso_da_leggere, $FDLS_line)


    Return $sFileRead
EndFunc   ;==>_Read_Canc_1_riga

; #FUNCTION# ====================================================================================================================
; Author ........: faustf
; Modified.......:
; What do........: $path = scrivo il percorso del txt ,$line = scrivo la linea che voglio cancellare lui cancella e shifta tutto
; ...............: contenuto togliendo la liena vuota. il conteggio lienee parte da 0
; ===============================================================================================================================

Func _File_DeleteLine_Shift($path, $line)

    If $line = "" Then
        $line = 1
    EndIf
    _FileReadToArray($path, $aArray)
    Local $iDelete = FileDelete($path)
    _FileCreate($path)
    $file = FileOpen($path, 1)

    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
    Else

        For $i = 1 To UBound($aArray) - 1 ; Loop through the array.

            If $i = $line Then
                ;MsgBox (0,'', $aArray[$i])
            Else
                ;If $aArray[$i] = 34 Then

                ;Else
                FileWrite($file, $aArray[$i] & @CR)
                ;EndIf

            EndIf
        Next

    EndIf

    ; Close the handle returned by FileOpen.
    Local $fileclosee = FileClose($file)

EndFunc   ;==>_File_DeleteLine_Shift

version mini  for test

Link to comment
Share on other sites

In your function File_DeleteLine_Shift try using this in place of your filewrite code.

For $i = 1 To UBound($aArray) - 1 ; Loop through the array.

    If $i = $line Then
        ;MsgBox (0,'', $aArray[$i])
    Else
        ;If $aArray[$i] = 34 Then

        ;Else
        FileWriteLine($file, $aArray[$i] & @CR) ; <<<<<<<<<<<<<<<<<<<<
        ;EndIf

    EndIf
Next

Also, there's no need to use _FileCreate in that code, FileOpen with the 1 parameter (append) creates the file.

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

Link to comment
Share on other sites

i belevie  find  a problem  :)

Func _True_FileReadLine($sFilePath, $riga_num)

    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "Allert", "An error occurred when reading the file.")
        Return False
    EndIf

    ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, $riga_num)


    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    Return $sFileRead

EndFunc   ;==>_True_FileReadLine

i invert a Return $sFileRead with FileClose($hFileOpen)

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