Jump to content

Overwrite existsing file


Recommended Posts

I have been updating code that a former colleague wrote to update a file on remote systems. If the file does not exists when the script is run (and the collect data button is pressed) the file is created successfully. However, if the file exists the file cannot be opened, deleted, etc. even though the file attributes return 'A' and that the file exists. The user account being used has full permissions to the target file. I cannot seem to find out why the file cannot be modified when it was just created properly moments before and has since been closed (running the script against the target PC a second time).

INI file

[Sections]
1="User Info"
[Attributes.1]
1="Asset Tag"
2="Employee ID"
3="Primary"
4="Description"
5="Location"
6="Area"
7="Remarks"
8="Date Placed"
[Attributes.1.1]
ID = 1
Type = String(4)
[Attributes.1.2]
ID = 2
Type = String(5)
[Attributes.1.3]
ID = 3
Type = String(3)
[Attributes.1.4]
ID = 4
Type = String(50)
[Attributes.1.5]
ID = 5
Type = String(50)
[Attributes.1.6]
ID = 6
Type = String(50)
[Attributes.1.7]
ID = 7
Type = String(50)
[Attributes.1.8]
ID = 8
Type = Date

GUI

#include <GuiConstants.au3>
#Include <Constants.au3>
#include <Array.au3>
#include <Inet.au3>
#include <File.au3>
;"20100930125046.000000+***"
;  Modify
Global $tag, $pc, $submit, $name, $size, $mifhandle, $mifline, $SG, $SA = 0, $attribcount, $eq, $val, $attribcount2, $ver, $slashes = '\\', $slash = '\', $av, $ip, $inifile = @scriptdir & '\AssetScan.ini', $wmidate = @year & @mon & @mday & @Hour & @min & @sec & '.000000+***', $Input_DatePlaced, $placed, $map, $mapdrive, $mif, $newmif
$inisections = IniReadSection($inifile,"Sections")
; _ArrayDisplay($inisections)
$iniattribs = IniReadSection($inifile,'Attributes.' & $inisections[1][0])
; _ArrayDisplay($iniattribs)
Dim $names[1] = ["Names"], $IDs[1] = ["IDs"], $Types[1] = ["Types"], $Values[1] = ["Values"]
$GUI1 = GUICreate("Enter PC",200,70)
GUICtrlCreateLabel("Enter PC name",10,13,140,20)
$pc = GUICtrlCreateInput("",100,10,90,20)
$submit = GUICtrlCreateButton("Submit",70,45,60,20)
GUICtrlSetState($submit,$gui_defbutton)
GUISetState ()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $Gui_Event_Close
            Exit
        Case $msg = $submit
            $name = GUICtrlRead($pc)
            ExitLoop
    EndSelect
WEnd

$tag = StringRight($name,4)
Select
    Case DirGetSize('\\' & $name & '\c$\windows\system32\ccm\inventory\noidmifs') <> -1
        ; MsgBox(1,'32-bit XP','32-bit XP')
        $mif = '\\' & $name & '\c$\windows\system32\ccm\inventory\noidmifs\' & $tag & '.mif'
    Case DirGetSize('\\' & $name & '\c$\winnt\system32\ccm\inventory\noidmifs') <> -1
        ; MsgBox(1,'32-bit 2000','32-bit 2000')
        $mif = '\\' & $name & '\c$\winnt\system32\ccm\inventory\noidmifs\' & $tag & '.mif'
    Case DirGetSize('\\' & $name & '\c$\windows\SysWOW64\ccm\inventory\noidmifs') <> -1
        ; MsgBox(1,'64-bit','64-bit')
        $mif = '\\' & $name & '\c$\windows\SysWOW64\ccm\inventory\noidmifs\' & $tag & '.mif'
    Case 1
        MsgBox(1,'SMS not installed','The SMS Client is not installed on this client.  Please install and try again.')
        $mif = ''
Endselect
$map = Stringleft($mif,Stringlen($mif)-9)
$mapdrive = DriveMapAdd('*',$map,8)
$mif = $mapdrive & '\' & StringRight($mif,8)
; MsgBox(0,'Mapped',$mapdrive & @crlf & $mif & @crlf & FileExists($mif))
$attribcount = 0
If FileExists($mif) Then
    $mifhandle = Fileopen($mif,0)
    ; MsgBox(0,'Opening MIF','Handle: ' & $mifhandle)
    While 1
        $mifline = FileReadLine($mifhandle)
        If @error = -1 Then ExitLoop
        Select
            Case StringInStr($mifline,'End Group') > 0
                ExitLoop
            Case StringInStr($mifline,'Start Attribute') > 0
                $SA = 1
                $attribcount = $attribcount + 1
        EndSelect
    Wend
    FileClose($mif)
    $attribcount = $attribcount + 10
    $mifhandle = Fileopen($mif,0)
    Local $iniattrub = Ubound($iniattribs,1)
    Dim $attribArray[$iniattrub][5]
    $attribArray[0][0] = 'Name'
    $attribArray[0][1] = 'ID'
    $attribArray[0][2] = 'Type'
    $attribArray[0][3] = 'Value'
    $attribArray[0][4] = 'ValueDisplay'
    ; _ArrayDisplay($attribArray)
    $SA = 0
    While 1
        $mifline = FileReadLine($mifhandle)
        If @error = -1 Then ExitLoop
        Select
            Case StringInStr($mifline,'End Group') > 0
                ExitLoop
            Case StringInStr($mifline,'Start Attribute') > 0
                $SA = 1
                $attribcount2 = $attribcount2 + 1
            Case StringInStr($mifline,'End Attribute') > 0
                $SA = 0
            Case $SA = 1 AND StringInStr($mifline,'Name') > 0
                $val1 = AttribValue($mifline)
                $attribArray[$attribcount2][0] = $val1
            Case $SA = 1 AND StringInStr($mifline,'ID') > 0
                $val2 = AttribValue($mifline)
                $attribArray[$attribcount2][1] = $val2
            Case $SA = 1 AND StringInStr($mifline,'Type') > 0
                $val3 = AttribValue($mifline)
                $attribArray[$attribcount2][2] = $val3
            Case $SA = 1 AND StringInStr($mifline,'Value') > 0
                $val4 = AttribValue($mifline)
                $attribArray[$attribcount2][3] = $val4
                $val4 = StringLeft($val4,StringLen($val4)-1)
                $val4 = StringRight($val4,StringLen($val4)-1)
                $attribArray[$attribcount2][4] = $val4
        EndSelect
    Wend
    ; _ArrayDisplay($attribArray)
    FileClose($mifhandle)
Else
    Local $iniattribs1 = Ubound($iniattribs,1)
    Dim $attribArray[$iniattribs1][5]
    $attribArray[0][0] = 'Name'
    $attribArray[0][1] = 'ID'
    $attribArray[0][2] = 'Type'
    $attribArray[0][3] = 'Value'
    $attribArray[0][4] = 'ValueDisplay'
Endif
Call ("PCData",$ver)
; _ArrayDisplay($attribArray)
Call ('FindMissing',$iniattribs, $attribArray, $iniFile)
Call ("GuiAndRun")
Exit

Func GuiAndRun()        
;gui box creation for entering asset data
$GUI2 = GuiCreate("Enter Asset Tracking Info", 350, 510)
GuiSetBkColor(0xffffff) 

$asset2 = GuiCtrlCreateLabel("PC Name", 10, 20, 75, 20)
$asset3 = GuiCtrlCreateInput(StringUpper($name), 135, 20, 140, 20)
;GuiCtrlSetData($asset3, $asset)
$EmpID = GuiCtrlCreateLabel("Employee ID:", 10, 50, 75, 20) 
If $attribArray[2][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$Input_EmpID = GuiCtrlCreateInput($attribArray[2][4], 135, 50, 140, 20)
$PC_AssetTag = GuiCtrlCreateLabel("PC Asset Tag:", 10, 80, 75, 20)
If $attribArray[1][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$Input_PCAssetTag = GuiCtrlCreateInput($attribArray[1][4], 135, 80, 140, 20)
$Primary = GuiCtrlCreateLabel("Primary PC:", 10, 110, 75, 20)
If $attribArray[3][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$p = GuiCtrlCreateCheckbox("",135, 110, 20, 20)
If $attribArray[3][3] = '"Yes"' Then
    GUICtrlSetState($p, $GUI_CHECKED)
Else
    GUICtrlSetState($p, $GUI_UNCHECKED)
EndIf
$DescNote = GuiCtrlCreateLabel("(PC Description \ Location on Floor)", 10, 135, 280, 20) 
$LocationLabel = GuiCtrlCreateLabel("Location:", 10, 165, 75, 20)
If $attribArray[5][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$Input_Location = GuiCtrlCreateInput($attribArray[5][4], 135, 165, 140, 20)
$AreaLabel = GuiCtrlCreateLabel("Area:", 10, 195, 75, 20)
If $attribArray[6][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$Input_Area = GuiCtrlCreateInput($attribArray[6][4], 135, 195, 140, 20)
$RemarksLabel = GuiCtrlCreateLabel("Remarks", 10, 225, 100, 20)
If $attribArray[7][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$Input_Remarks = GuiCtrlCreateInput($attribArray[7][4], 135, 225, 140, 20)
$DatePlaced = GuiCtrlCreateLabel("Date Placed:", 10, 255, 75, 20)
If $attribArray[8][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$dpc = GUICtrlCreateCheckBox('',295,255,20,20)
$Input_DatePlaced = GuiCtrlCreateInput($attribArray[8][4], 135, 255, 140, 20)
$Desc = GUICtrlCreateLabel("PC Description", 10, 285, 75, 20)
If $attribArray[4][4] = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$Input_Desc = GuiCtrlCreateInput("Enter PC Description", 135, 285, 180, 20) ;10, 285, 280, 20)
GuiCtrlSetData($Input_Desc, $attribArray[4][4])

$calabel = GUICtrlCreateLabel("CA Version", 10, 315, 100, 20)
If $av = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$cadata = GuiCtrlCreateLabel($av, 135, 315, 140, 20)
$iplabel = GUICtrlCreateLabel("IP Address", 10, 345, 75, 20)
If $ip = '' Then
    GUICtrlSetColor(-1, 0xff0000)   ; Red
Endif
$ipdata = GuiCtrlCreateLabel($ip, 135, 345, 140, 20)
$button1 = GuiCtrlCreateButton("Collect Data", 100, 480, 130, 20)
$button2 = GuiCtrlCreateButton("Array", 250, 480, 50, 20)

GUICtrlSetState($button1, $gui_defbutton)
GuiSetState()

;tray icon starts
Opt("TrayMenuMode",0)    ; Default tray menu items (Script Paused/Exit) will not be shown.

TraySetState()
TraySetIcon("c:\windows\system32\remotepg.dll", 0)
TrayTip("Rittal Asset Scan", "collect data will only take a moment", 20)

;tray icon ends

;set data while loop
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $dpc
        If GUICtrlRead($dpc) = 1 Then
            GUICtrlSetData($Input_DatePlaced,$wmidate)
            GUICtrlSetColor($Input_DatePlaced,0x006400)
            GUICtrlSetColor($DatePlaced,0x000000)
        Elseif GUICtrlRead($dpc) = 4 Then
            GUICtrlSetData($Input_DatePlaced,'')
            GUICtrlSetColor($DatePlaced,0xff0000)
        Endif
    Case $msg = $GUI_EVENT_CLOSE
        DriveMapDel($mapdrive)
        Exit
    Case $msg = $button1
        $attribArray[2][4] = GUICtrlRead($Input_EmpID)
        ; GuiCtrlSetData($Input_EmpID, $attribArray[2][4])
        $attribArray[2][3] = '"' & $attribArray[2][4] & '"'
        $attribArray[1][4] = GUICtrlRead($Input_PCAssetTag)
        ; GuiCtrlSetData($Input_PCAssetTag, $attribArray[1][4])
        $attribArray[1][3] = '"' & $attribArray[1][4] & '"'
        If GUICtrlRead($p) = 1 Then
            $attribArray[3][3] = '"Yes"'
        Else
            $attribArray[3][3] = '"No"'
        Endif
        ; GuiCtrlSetData($p, $p3)
        If GUICtrlRead($Input_DatePlaced) <> '' Then
            $attribArray[8][3] = '"' & GUICtrlRead($Input_DatePlaced) & '"'
        Else
            $attribArray[8][3] = '""'
        Endif
        ; GuiCtrlSetData($Input_DatePlaced, $placed)
        $attribArray[4][4] = GUICtrlRead($Input_Desc)
        $attribArray[4][3] = '"' & $attribArray[4][4] & '"'
        ; GuiCtrlSetData($Input_Desc, $desc10)
        $attribArray[5][4] = GUICtrlRead($Input_Location)
        $attribArray[5][3] = '"' & $attribArray[5][4] & '"'
        ; GuiCtrlSetData($Input_Location, $location)
        $attribArray[6][4] = GUICtrlRead($Input_Area)
        $attribArray[6][3] = '"' & $attribArray[6][4] & '"'
        ; GuiCtrlSetData($Input_Area, $area)
        $attribArray[7][4] = GUICtrlRead($Input_Remarks)
        $attribArray[7][3] = '"' & $attribArray[7][4] & '"'
        ; GuiCtrlSetData($Input_Remarks, $remarks)
        ; _ArrayDisplay($attribArray)
        ExitLoop
    Case $msg = $button2
        _ArrayDisplay($attribArray)
    EndSelect
Wend
; Local $newmif = fileopen(@tempdir & '\' & $attribArray[1][4] & '.mif',2)
; Local $newmif = fileopen($mapdrive & '\' & $attribArray[1][4] & '.mif',2)
MsgBox(0,$mif,Fileexists($mif) & @crlf & FileGetAttrib($mif) & @crlf & FileClose($mifhandle) & @crlf & FileFlush($mifhandle))
$newmif = FileDelete($mif)
MsgBox(0,'Delete result',$newmif)
$newmif = fileopen($mif,2)
MsgBox(0,'Opening New MIF','Handle: ' & $newmif & @crlf & 'Attribs: ' & FileGetAttrib($newmif))
FileWrite($newmif,'Start Component' & @crlf & Chr(9) & 'Name = "Asset Info"' & @crlf & Chr(9) & 'Start Group' & @crlf & Chr(9) & Chr(9) & 'Name = "User Info"' & @crlf & Chr(9) & Chr(9) & 'ID = 1' & @crlf & Chr(9) & Chr(9) & 'Class = "UserInfo"' & @crlf)
For $a = 1 to Ubound($attribArray,1) - 1
        FileWrite($newmif,Chr(9) & Chr(9) & 'Start Attribute' & @crlf)
        FileWrite($newmif,Chr(9) & Chr(9) & Chr(9) & $attribArray[0][0] & ' = ' & $attribArray[$a][0] & @crlf)
        FileWrite($newmif,Chr(9) & Chr(9) & Chr(9) & $attribArray[0][1] & ' = ' & $attribArray[$a][1] & @crlf)
        FileWrite($newmif,Chr(9) & Chr(9) & Chr(9) & $attribArray[0][2] & ' = ' & $attribArray[$a][2] & @crlf)
        FileWrite($newmif,Chr(9) & Chr(9) & Chr(9) & $attribArray[0][3] & ' = ' & $attribArray[$a][3] & @crlf)
        FileWrite($newmif,Chr(9) & Chr(9) & 'End Attribute' & @crlf)
Next
FileWrite($newmif,Chr(9) & 'End Group' & @crlf & 'End Component')
FileClose($newmif)
; Local $source = @tempdir & '\' & $attribArray[1][4] & '.mif'
; Local $dest = $mapdrive & '\' & $attribArray[1][4] & '.mif'
; Local $filecopy = RunWait('copy /y ' & $source & ' ' & $dest)
; MsgBox(0,'MIF Exists','Source: ' & FileExists($source)& @crlf & 'Destination: ' & FileExists($dest))
; Local $filecopy = Filecopy(@tempdir & '\' & $attribArray[1][4] & '.mif',$mapdrive & '\' & $attribArray[1][4] & '.mif',1)
; If FileExists($dest) = 1 Then
    ; Local $delmif = FileDelete($mapdrive & '\*.mif')
    ; If $delmif = 1 Then
        ; MsgBox(0,'Deleted', $dest & @crlf & $delmif & @crlf & FileExists($dest))
    ; Else
        ; MsgBox(0,'Deleted', $dest & @crlf & $delmif & @crlf & FileExists($dest))
    ; Endif
; Endif
; Local $filecopy = Filecopy($source,$dest,9)

; If $filecopy <> 0 Then
    ; MsgBox(0,'Success','MIF file copied' & @crlf & $filecopy & @crlf & @error)
; Else
    ; MsgBox(0,'Failure','MIF not copied' & @crlf & $filecopy & @crlf & @error)
; Endif
DriveMapDel($mapdrive)


EndFunc

Func AttribValue($line)
    $eq = StringInStr($line,'=')
    $val = StringMid($line,$eq+2)
    ; $val = StringLeft($val,StringLen($val)-1)
    ; $val = StringRight($val,StringLen($val)-1)
    ; MsgBox(0,'Attrib Value:','Line: ' & $line & @crlf & 'Locate =: ' & $eq & @crlf & 'Value:',$val)
    Return $val
EndFunc

Func PCData($ver)
    $av = RegRead($slashes & $name & $slash & "HKEY_LOCAL_MACHINE\SOFTWARE\ComputerAssociates\eTrustITM\CurrentVersion","Version")
    
    $num = 0    
    $ip2 = ""
    If $ver <> 3 Then
        While $IP2 = ""
            $ip2 = RegRead($slashes & $name & $slash & "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\NetworkCards\" & $num, "ServiceName")    
            If $ip2 = "" Then
                $num = $num +1
            Else
                ExitLoop
            EndIf
        WEnd
        $ip = RegRead($slashes & $name & $slash & "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" & $ip2 & "\Parameters\Tcpip","DhcpIPAddress")
    EndIf
    $desc1 = RegRead($slashes & $name & $slash & "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters","srvcomment")
EndFunc

Func FindMissing($srcArray, $tgtArray, $ini)
    Local $src2ubound = Ubound($srcArray,2) - 1
    Local $src1ubound = Ubound($srcArray,1) - 1
    ; MsgBox(0,'ubound','col 1: ' & Ubound($srcArray,1) & @crlf & 'col 2: ' & $src2ubound)
    ; Local $newSrc
    ; For $y =0 to $src1ubound
        ; _ArrayAdd($newSrc,$srcArray[$y][1])
    ; Next
    ; _ArrayDisplay($newSrc)
    For $x = 1 to $src1ubound
        ; MsgBox(0,'Attribute to be Searched for:',$srcArray[$x][$src2ubound])
        Local $attribval = $srcArray[$x][$src2ubound]
        If _ArraySearch($tgtArray, $attribval) = -1 Then
            ; MsgBox(0,'Not Found:',$srcArray[$x][$src2ubound])
            $attribArray[$x][0] = $srcArray[$x][$src2ubound]
            $attribArray[$x][1] = IniRead($ini,'Attributes.1.' & $x, 'ID',$x)
            $attribArray[$x][2] = IniRead($ini,'Attributes.1.' & $x, 'Type','')
        Endif
    Next
    ; _ArrayDisplay($attribArray)
EndFunc
Link to comment
Share on other sites

$mifhandle = Fileopen($mif,0)
    ; MsgBox(0,'Opening MIF','Handle: ' & $mifhandle)
    While 1
        $mifline = FileReadLine($mifhandle)
        If @error = -1 Then ExitLoop
        Select
            Case StringInStr($mifline,'End Group') > 0
                ExitLoop
            Case StringInStr($mifline,'Start Attribute') > 0
                $SA = 1
                $attribcount = $attribcount + 1
        EndSelect
    Wend
    FileClose($mif)

I don't know if it can help you but you use FileClose($mif) instead of FileClose($mifhandle) Posted Image

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

That was it! I've looked at this I don't know how many times. I'm glad this board exists so that I can have someone else look over my code and find the mistakes. Thanks wakillon.

$mifhandle = Fileopen($mif,0)
    ; MsgBox(0,'Opening MIF','Handle: ' & $mifhandle)
    While 1
        $mifline = FileReadLine($mifhandle)
        If @error = -1 Then ExitLoop
        Select
            Case StringInStr($mifline,'End Group') > 0
                ExitLoop
            Case StringInStr($mifline,'Start Attribute') > 0
                $SA = 1
                $attribcount = $attribcount + 1
        EndSelect
    Wend
    FileClose($mif)

I don't know if it can help you but you use FileClose($mif) instead of FileClose($mifhandle) Posted Image

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