Jump to content

Aut2Exe fails to replace open files on server shares: Close & replace it!


Recommended Posts

Hello,

Either drag drop the opened file on the server and the temp file, Aut2Exe could not copy to the destination file, as that one is opened, to the edits of this GUI, or take the Aut2Exe error message line to your clipboard, the line will be split automatically.

This script does not check

  • For the required rights to the destination folder (to close open file handles)
  • for correct input
  • Use either clipboard (Aut2Exe message), drag & drop, or copy the full paths of source and destination file *IN ONE* (don't type, if you want to do so, modify the script, for me it's fine this way ;-)

 

Example Error Message:

!>11:19:15 Problem copying file from: C:\Users\UserName\AppData\Local\AutoIt v3\Aut2exe\~AU98E6.tmp.exe To :z:\MyAutoitExeForTheUsers.exe

image.png.a994921160334a62fded92db43f84d07.png

#include <GUIConstantsEx.au3>
#include <NetShare.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <EditConstants.au3>
; Enumerate open files on the server


$DragDropOpenFile = "<drag drop the opened file on a server share here>"
$DragDropNewFile = "<drag drop the file supposed to replace the above one here>"
$NewFile = ""
$Gui_h = 250
$Gui_w = 800
$vDist = 7
; GUICreate($GuiTitle, $w, $h, @DesktopWidth - $w - 100, @DesktopHeight - $h - 60, -1, $WS_EX_ACCEPTFILES) ; generally enable drag-drop for files into other GUI controls
$myGui = GUICreate("Tool to forcibly close & replace open files on server shares", $Gui_w, $Gui_h, 100, 100, -1, $WS_EX_ACCEPTFILES)

$InputFileToClose = GUICtrlCreateInput($DragDropOpenFile, 20, $vDist, $Gui_w - 40, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; allow drag-droping files for this control, $InputFile
Opt("Guicoordmode", 2)
$InputFileNew = GUICtrlCreateInput($DragDropNewFile, -1, $vDist)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; allow drag-droping files for this control, $InputFile
GUICtrlSetState(-1, $GUI_DISABLE)
$lServer = GUICtrlCreateLabel("<server>", -1, $vDist)
$lShare = GUICtrlCreateLabel("<share-mapping>", -1, $vDist)
$lPath = GUICtrlCreateLabel("<sub-path>", -1, $vDist)
$lFile = GUICtrlCreateLabel("<file>", -1, $vDist)

$doit = GUICtrlCreateButton("Search and close for open file handles", -1, $vDist)
GUICtrlSetState(-1, $GUI_DISABLE)
$exit = GUICtrlCreateButton("Cancel", -1, $vDist)



GUISetState()



$FN = False
$FNnew = ""
$FNmatch = False
$ToolTitle = ""
$ToolTxt = ""


$RegExA2E = "(?i)^(?:.*?Problem copying file from: )(.*?)(?: To :)(.*$)" ; $1 = compiled local TEMP file, $2 = not replacable destination file
; Examle Replacement failed output: !>11:19:15 Problem copying file from: C:\Users\USERNAME\AppData\Local\AutoIt v3\Aut2exe\~AU98E6.tmp.exe To :z:\MyAutoitExeForTheUsers.exe

While 1
    $Clip = ClipGet()
    If StringRegExp($Clip, $RegExA2E) Then
        $Src = StringRegExpReplace($Clip, $RegExA2E, "$2")
        GUICtrlSetData($InputFileToClose, $Src)
        $Dst = StringRegExpReplace($Clip, $RegExA2E, "$1")
        GUICtrlSetData($InputFileNew, $Dst)
    EndIf

    $input = GUICtrlRead($InputFileToClose)
    If $input <> $DragDropOpenFile Then ; da wurde was reingezogen
        $DragDropOpenFile = $input
        If StringLeft($input, 2) = "\\" Then
            $Type = "UNC"
            $ServerShareUNC = StringLeft($input, StringInStr($input, "\", 0, 4) - 1)
            $fRelPathFN = StringReplace($input, $ServerShareUNC, "")
            $fPath = StringLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1))
            $FN = StringTrimLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1))
        ElseIf StringMid($input, 2, 1) = ":" Then ; Pfad mit Laufwerksbuchstabe, evtl. Netzwerk Mapping (erforderlich)
            $drive = StringLeft($input, 2)
            $Type = DriveGetType($drive)
            If $Type = "Network" Then
                $ServerShareUNC = DriveMapGet($drive)
                $foo = StringReplace($input, $drive, $ServerShareUNC) ; Laufwerkspfad in UNC Pfad umwandeln
                $fRelPathFN = StringReplace($foo, $ServerShareUNC, "")
                $fPath = StringLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1))
                $FN = StringTrimLeft($fRelPathFN, StringInStr($fRelPathFN, "\", 0, -1))
            Else
                MsgBox(48, @ScriptLineNumber, "This script can *ONLY* close remotely handles for files open on server shares!" & @CRLF & _
                        $input & @CRLF & _
                        $drive & " = " & $Type)
                ContinueLoop 2
            EndIf
        EndIf
        $Server = StringLeft($ServerShareUNC, StringInStr($ServerShareUNC, "\", 0, 3) - 1)
        $Share = StringTrimLeft($ServerShareUNC, StringInStr($ServerShareUNC, "\", 0, 3) - 1)
        GUICtrlSetData($lServer,"Server  = '" &  $Server& "'")
        GUICtrlSetData($lShare, "Mapping = '" & $Share & "'")
        GUICtrlSetData($lPath,  "SubPath = '" &$fPath & "'")
        GUICtrlSetData($lFile,  "FileName= '" & $FN &"'")
        GUICtrlSetState($doit, $GUI_ENABLE)
        GUICtrlSetState($InputFileNew, $GUI_ENABLE)
    EndIf

    If $NewFile <> GUICtrlRead($InputFileNew) Then
        $NewFile = GUICtrlRead($InputFileNew)
        If $NewFile <> $DragDropNewFile Then
            If StringInStr($NewFile, $DragDropNewFile) Then
                $NewFile = StringReplace($NewFile, $DragDropNewFile, "")
                GUICtrlSetData($InputFileNew, $NewFile)
            EndIf
            $FNnew = StringTrimLeft($NewFile, StringInStr($NewFile, "\", 0, -1))
            If $FN = $FNnew Then
                If $FNmatch = False Then
                    $FNmatch = True
                    GUICtrlSetData($doit, "Search for & Close open file handles, then replace file")
                EndIf
            Else
                If $FNmatch Then
                    $FNmatch = False
                    GUICtrlSetData($doit, "Search for & Close open file handles, then replace file")
                    GUICtrlSetData($InputFileNew, $DragDropNewFile)
                EndIf
            EndIf
        EndIf
    EndIf
    Switch GUIGetMsg()
        Case $exit, $GUI_EVENT_CLOSE
            GUIDelete($myGui)
            Exit
        Case $doit
            AbArbeiten($Server, $Share, $fPath, $FN)
    EndSwitch
WEnd





Func AbArbeiten($_Srv, $_Shr, $_fPth, $_fNme)

    Local $iID = 0
    Local $iRights = 1
    Local $iLckCount = 2
    Local $iFPFN = 3
    Local $iUser = 4

    ConsoleWrite($_fPth & $_fNme & @CRLF)

    Local $aFile = _Net_Share_FileEnum($_Srv)
    If IsArray($aFile) Then
        ; _ArrayDisplay($aFile)
        Local $x
        $ToolTxt = "Open File Handles:"
        $ToolTitle = "Handles to be checked total: " & $aFile[0][0]
        UpdateToolTip()
        AdlibRegister(UpdateToolTip, 1000)
        For $x = $aFile[0][0] To 1 Step -1
            $ToolTitle = $x & " handles remaining for checking..."

            If Not StringInStr($aFile[$x][$iFPFN], $_fPth & $_fNme) Then
                ; ConsoleWrite("Nix Enthalten in: " & $aFile[$x][$iFPFN] & @CRLF)
                _ArrayDelete($aFile, $x)
            Else
                $ToolTxt &= @CRLF & $aFile[$x][$iFPFN] & ", User = " & $aFile[$x][$iUser]
                ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToolTxt = ' & $ToolTxt & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
            EndIf
        Next
        $aFile[0][0] = UBound($aFile) - 1
        _ArraySort($aFile, 0, 1, 0, $iUser)
        ; _ArrayDisplay($aFile, $aFile[0][0] & " FileLocks found.")
        If $aFile[0][0] = 0 Then
            $ToolTitle = "Done, no open file handles were found"
            $ToolTxt &= @CRLF & ", no handles to be closed for this file!"
            Sleep(2000)
            $ToolTxt = ""
            ReplaceFile()
        Else
            $ToolTitle =  $aFile[0][0] & " open file handles were found..."
            $CloseErr = 0
            For $x = 1 To $aFile[0][0]
                If _Net_Share_FileClose($Server, $aFile[$x][$iID]) Then
                    $ToolTxt &= @CRLF & @TAB & "Handle closed: " & $aFile[$x][$iID]
                Else
                    $ToolTxt &= @CRLF & "ERROR: Handle Close Failed! --> " & $aFile[$x][$iFPFN] & ", User = " & $aFile[$x][$iUser]
                    $CloseErr += 1
                EndIf
            Next
            ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $CloseErr = ' & $CloseErr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
            If $CloseErr = 0 Then
                $ToolTitle = "All found open file handles could be closed."
                ReplaceFile()
            Else
                $ToolTitle = $CloseErr & " Handles could *NOT* be closed!"
                Sleep(2000)
            EndIf
            Sleep(1000)
            $ToolTxt = ""
            $ToolTitle = ""
        EndIf
    Else
        MsgBox(0, "not an array", $aFile & @CRLF & @error & @CRLF & @extended & @CRLF & _
                "Unable to retrieve the array of open file handles for " & $Share)
    EndIf
EndFunc   ;==>AbArbeiten


Func UpdateToolTip()
    ToolTip($ToolTxt, MouseGetPos(0) + 20, MouseGetPos(1) + 20, $ToolTitle)
    ; ConsoleWrite( $ToolTxt & @CRLF & $ToolTitle & @CRLF & "-------------------" & @CRLF)
EndFunc   ;==>UpdateToolTip


Func ReplaceFile()
    If FileExists($NewFile) Then
        If FileCopy($NewFile, $input, 1 + 8) Then
            $ToolTitle = "File successfully replaced."
            $ToolTxt = "Done"
        Else
            $ToolTitle = "File could *NOT* be replaced."
            $ToolTxt = "Possibly another open file handle spawned while this script was running." & @CRLF & _
                    "Simply start over again, please."
            MsgBox(48, $ToolTitle, $ToolTxt)
        EndIf
    EndIf
EndFunc   ;==>ReplaceFile

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By marcoauto
      ciao
      I have a Delphi compiled program that I use for work and I can read some logs with this function:
      Func __ReadExternalListBox() Local $hListBox, $iItemCount $hListBox = ControlGetHandle("Software Setup", "", "[CLASS:TListBox; INSTANCE:1]") ConsoleWrite("$hListBox = " & $hListBox & @CRLF) $iItemCount = _GUICtrlListBox_GetCount($hListBox) For $i = $iStart To $iItemCount - 1 ConsoleWrite(GUICtrlListBox_GetText($hListBox,$i) & @CRLF) Next EndFunc ;==>__ReadExternalListBox I can find pid and full path of the process.
      The problem is when I must open another process: I can't read the new ListBox because the title of the window to read is the same and the function 
      $hListBox = ControlGetHandle("Software Setup", "", "[CLASS:TListBox; INSTANCE:1]") return me only one handle.
      With Autoinfo I can read handle: is, for this instance, 0x0001099C and is $hListBox

      But I need to automize some function and I can't always change in runtime this values.
      Ideas? Is there e method to read this title windows associated to a determined pid?
      PS:
      I don't need the handle from the pid windows application, but from its control
      if I use
      _WinAPI_EnumProcessWindows() the function return me window handle, not Control handle
      I
      In this case Windows Handle is 0x001007DA but I need Control Handle that is 0x0001099C
      Thankyou
      Marco
    • By XGamerGuide
      I'm trying to call WinList() with a handle. That should be possible:
      But I only get back an 2d array that looks like this:
      ---------
      0 |
      (That means no window was found.)
      It's not the window or the handle. When I call other functions like WinExists() or WinGetHandle() it works.
      Is it because of me, the description in the reference or because of Autoit who makes a mistake?
      No, because of the rest of my program I don't want to use WinGetTitle() because the format of WinList() has to be kept. Alternatively, I could create a 2d array myself, but it should also work with WinList() ... I hope.
    • By nacerbaaziz
      hello sirs
      i've some questions about StringRegExpReplace i hope you can help me
       
      i tried to make a function that give me the host of the url and other give me the url with out host
      for example i've this link
      https://www.example.com/vb/result.php
      i need the first give me the
      example.com
      and the other give me 
      /vb/result.php
      i find that
      $s_source = "https://www.google.com/vb/index.php" Local $s_Host = StringRegExpReplace($s_Source, '.*://(.*?)/.*', '\1') Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*)', '\1') msgBox(64, $s_Host, $s_Page)  
      but i found some problems i need your help to correct it
      first: when i get the host if the url has www i want to remove it
      second: if the url with out host did not have other things 
      i need the result to be ""
      e.g
      https://www.example.com
      the first i want it
      example.com
      and the second i want it to be ""
      i hope that you can help me
      thanks in advance
    • By dascondor
      So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file.
       
      First File:
      jumps
       
      Or:
      Text1 = jumps
      Text2 = fence
      Second File:
      My Dog XXXXX over the YYYYY.
       
       
      After Script:
      My Dog jumps over the fence
       
      I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house.
       
      Any help is appreciated.
       
       
    • By ThePoro
      Hi everyone.
      I want to ask about this : 
      I want it runs from 1 to 100 and It opens 10 firefox profiles then access youtube. After I close a firefox window, the loop runs and wait for another window close until loop ends
      I have a loop like this.
      Func launch() Local $from = Int(GUICtrlRead($input1)) Local $to = Int(GUICtrlRead($input2)) If $to <> "" Then While $from <= $to Local $profile = $to _RunDos("start firefox.exe -p " & $profile & " -no-remote youtube.com") $to=$to+1 WEnd Else Local $profile = $to _RunDos("start firefox.exe -p " & $profile & " -no-remote youtube.com") EndIf EndFunc Is there any solution?
      Thank you!
×
×
  • Create New...