Sign in to follow this  
Followers 0
caramen

FileClose Issue

9 posts in this topic

#1 ·  Posted (edited)

               Case $Num_PC

                  Local $MyNum_PC = InputBox("Nump_PC", "Que voulez vous comme numéro de pc?"&@CRLF &"Pour Planitec 2.0")

                  Local Const $NumPCPath = "C:\windows\system32\Num_PC.reg"
                  _FileCreate ( $NumPCPath )
                  Local $hFileOpen = FileOpen($NumPCPath, $FO_APPEND)

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

                  FileOpen ($NumPCPath)

                  FileWriteLine($hFileOpen, "Windows Registry Editor Version 5.00")
                  FileWriteLine($hFileOpen, @CRLF)
                  FileWriteLine($hFileOpen, "[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]")
                  FileWriteLine($hFileOpen,@CRLF)
                  FileWriteLine($hFileOpen,'"NUM_PC"="'&$MyNum_PC&'"')

                  FileClose ($NumPCPath)

                  NumpRunDeletion ()

i got this case in my switch

I think if i understand good.... the "Case" keep the file open even if i do FileClose ($NumPCPath) until the "Case" is not ended

so i cant run the NumpRunDeletion () function properly becose i got a file usef error this is the commandprompt error message

(i see the file in my system32 and yes he is used becose i cant delet it while the script is running  )

E:\>C:\Windows\System32\cmd.exe /k C:\WINDOWS\System32\reg.exe Import Num_Pc.reg

ERROR: Error opening the file. There may be a disk or file system error.

missed to give you the fonction :

Func NumpRunDeletion ()
   ShellExecute (@ScriptDir & "\NUM-PC.bat")
   FileDelete ($NumPCPath)
EndFunc

the strange thing is shellexecute does not work

filedelete work... hmmmmmmmmmmm

 

I add this : if i close my script and try again  NumpRunDeletion () it s working

Edited by caramen

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

RE post with updated script : Still same issue the reg wont be imported and the file is deleted if i quit the script (ONLY if i run Func NumpRunDeletion () )

Case $Num_PCMake

                  Local $MyNum_PC = InputBox("Nump_PC", "Que voulez vous comme numéro de pc?"&@CRLF &"Pour Planitec 2.0")

                  Local Const $NumPCPath = "C:\windows\system32\Num_PC.reg"
                  _FileCreate ( $NumPCPath )
                  Local $hFileOpen = FileOpen($NumPCPath, $FO_APPEND)

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

                  FileOpen ($NumPCPath)

                  FileWriteLine($hFileOpen, "Windows Registry Editor Version 5.00")
                  FileWriteLine($hFileOpen, @CRLF)
                  FileWriteLine($hFileOpen, "[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment]")
                  FileWriteLine($hFileOpen,@CRLF)
                  FileWriteLine($hFileOpen,'"NUM_PC"="'&$MyNum_PC&'"')

                  FileClose ($NumPCPath)

               Case $Num_PCRun

                  NumpcRunDeletion ()
Func NumpcRunDeletion ()
   FileClose ($NumPCPath)
   ShellExecute (@ScriptDir & "\NUM-PC.bat")
   FileDelete ($NumPCPath)
EndFunc

To clarify and make this simple

Case $Num_PCMake = Make a reg file

Case $Num_PCRun = Run the reg and delete it

If i run Case $Num_PCMake And Case $Num_PCRun It s not working (File in use)

If i only run $Num_PCMake Close the script and run $Num_PCRun It s working

how to solve that file close issue ... i tryed a lot of thing...

Edited by caramen

Share this post


Link to post
Share on other sites

Hi Caraman,

You appear to call 'FileOpen' twice without any 'FileClose', is this correct?? 

I would remove the second 'FileOpen'

Share this post


Link to post
Share on other sites

Sorry I missed the main issue.

You're FileClose ($NumPCPath) should be FileClose ($hFileOpen)

You should use the handle not the filepath.

And remove the second FileOpen()

Hope this helps


 

Share this post


Link to post
Share on other sites

Corrected the lines still same issue

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

i just found the solution and it was the correct code if i post full script it's 1800 lines

The solution is if i call file delete directly after the execute commande the file delete is lunched while the files is runned with a pause of 3000 ms this is now working

Func NumpcRunDeletion ()
   Local $NumPCPath = "C:\windows\system32\Num_PC.reg"
   ShellExecute (@ScriptDir & "\NUM-PC.bat")

   Sleep (3000)

   FileDelete ($NumPCPath)
EndFunc

i guess autoit dont get it becose of the batch file is out of autoit and then he cant figure this out

 

ShellExecuteWait can be a solution too i guess i am gonna try

Edited by caramen

Share this post


Link to post
Share on other sites

working also with ShellExecuteWait thx all :) and without pause

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

better route for async:

Func NumpcRunDeletion ()
    FileClose ($NumPCPath)
    $iPid = ShellExecute (@ScriptDir & "\NUM-PC.bat")
    While ProcessExists($iPid)
        Sleep(10)
    WEnd
    FileDelete ($NumPCPath)
EndFunc

would probably want a max execution length as well

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

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
Sign in to follow this  
Followers 0