Jump to content

Help With Script Exiting


Recommended Posts

The below script will browse files from a directory and get the name, creation time, modified time and version of all the files. You can drag the folder into the box or use the search button, after you have draged the folder in click search.

Once you have this list you can Export it to a CSV file.

If you have a previously created CSV you can run a compare using the right hand box.

You can drag a CSV file in to the box or browse (using the compare button) with file open dialogue.

This all works fine the first time, but if you highlight the file in the right hand box and delete the string, click the compare button again with a blank box, the file open dialogue re-opens and it will either exit at this point or after the CSV file is selected. It does not report a crash!

I can't figure out why it is doing this.. could anyone shed some light on it for me.

Ta

Chris

CODE

#include <GUIConstants.au3>

#include <array.au3>

#include <GuiListView.au3>

Opt("TrayIconDebug", 1)

Local $aFileList[1][4], $aCompare[1][4], $aDifferent[1]

GUICreate("Directory Compare", 831, 569, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS), $WS_EX_ACCEPTFILES)

$FolderBrowse = GUICtrlCreateInput("", 20, 30, 270, 20)

GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$SearchFolder = GUICtrlCreateButton("Search", 310, 30, 90, 20)

$Edit = GUICtrlCreateEdit("", 20, 80, 280, 400, BitOr ($ES_WANTRETURN , $WS_VSCROLL , $WS_HSCROLL , $ES_AUTOVSCROLL , $ES_AUTOHSCROLL , $ES_MULTILINE , $WS_TABSTOP , $ES_READONLY ,$WS_CLIPCHILDREN))

$ExportCSV = GUICtrlCreateButton("Export", 80, 510, 150, 20)

$TextFile = GUICtrlCreateInput("", 430, 30, 290, 20)

GUICtrlSetState(-1, $GUI_DROPACCEPTED)

$Compare = GUICtrlCreateButton("Compare", 740, 30, 80, 20)

$List_7 = GUICtrlCreateListView("File|Status|Version|Modification", 430, 80, 300, 396)

$Report = GUICtrlCreateButton("Generate Report", 510, 510, 160, 20)

GUISetState()

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $SearchFolder

Dim $aFileList[1][4] ;Resets the FileList if this is the second time running

GUICtrlSetData($List_7, "") ;Clear previous data

If GUICtrlRead($FolderBrowse) = "" Then ; Read data for folder to browse if its blank run the folder browser

$folder = FileSelectFolder("Select Folder", "")

GUICtrlSetData($FolderBrowse, $folder)

$Path = GUICtrlRead($FolderBrowse)

Else

$Path = GUICtrlRead($FolderBrowse) ;Read the data for folder to browse

EndIf

If GUICtrlRead($FolderBrowse) <> "" Then ; Check that its a valid directory

$Path = GUICtrlRead($FolderBrowse)

$search = FileFindFirstFile($Path & "*.*")

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Else

$Chop = StringLen($Path) ; chop the original path out of the resutls

If StringRight($Path, 1) = "\" Then $Chop = $Chop - 1

GUICtrlSetData($Edit, "")

;initialise the variable for the search, important if ran for a second time without closing the application

Global $Run1 = 0, $tosearch = "", $OverallQty = "", $Overall = "", $source = "", $overallpercent = "", $Progress0Text = "", $progressbar1 = "", $Progress1Text = "", $progressbar2 = "", $Progress2Text = "", $Gui = ""

Search($Path) ; run search with the path to search

For $i = 1 To UBound($aFileList) - 1 ; populate the edit box with data

GUICtrlSetData($Edit, $aFileList[$i][0] & "," & $aFileList[$i][1] & "," & $aFileList[$i][2] & "," & $aFileList[$i][3] & @CRLF, 1)

Next

EndIf

EndIf

Case $msg = $ExportCSV

If GUICtrlRead($Edit) <> "" Then ; if the edit box isn't blank we know that there is something to export

$Destination = FileSaveDialog("Export CSV file to?", "", "(*.CSV)")

If $Destination <> "" then ; if the CSV destination isn't blank then write the report

If StringRight($Destination, 4) <> ".csv" Then $Destination = $Destination & ".csv"

$CSV = FileOpen($Destination, 2)

FileWriteLine($CSV, "Filename,Version,Created,Modified" & @CRLF)

For $i = 1 To UBound($aFileList) - 1

FileWriteLine($CSV, $aFileList[$i][0] & "," & $aFileList[$i][1] & "," & $aFileList[$i][2] & "," & $aFileList[$i][3])

;MsgBox (0,"",$aFileList[$i])

Next

FileClose($CSV)

MsgBox(0, "Done", "CSV file exported to " & $Destination, 3)

Endif

; No CSV destination was selected

EndIf

Case $msg = $Compare

If GUICtrlRead($Edit) <> "" Then ; if the edit box isn't blank we know that there is something to compare to

Dim $aCompare[1][4], $aDifferent[1] ; reset the 2 compare arrays, important if this is the second time it is ran without closing

_GUICtrlListViewDeleteAllItems ($List_7) ; delete current data from the list

If GUICtrlRead($TextFile) <> "" Then ; check there is a file name to compare

$path2 = GUICtrlRead($TextFile)

If $path2 <> "" Then Compare($path2)

Else

$Path2 = ""

$path2 = FileOpenDialog("Select Compare file", "", "(*.CSV)") ; if there is no compare file entered open the file open dialogue

GUICtrlSetData($TextFile, $path2)

If $path2 <> "" Then Compare($path2) ; if the path is not blank then run compare and send the CSV path

EndIf

Else

MsgBox(0, "Error", "Nothing to compare") ; Edit box is blank so there is nothing to compare

EndIf

Case $msg = $List_7

; sort the list by the column header clicked on

Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($List_7) ] ; when this was declared at the top it sometimes crashed in Scite?

_GUICtrlListViewSort ($List_7, $B_DESCENDING, GUICtrlGetState($List_7)) ; sort data by clicking on the column tops

Case $msg = $Report

$ReportDestination = FileSaveDialog("Save file to?", "", "(*.txt)") ; where do you want the report saved to?

;Generate the report

If $ReportDestination <> "" then ; if the reportdestination isn't blank then write the report

If StringRight($ReportDestination, 4) <> ".txt" Then $ReportDestination = $ReportDestination & ".txt"

$txt = FileOpen($ReportDestination, 2)

FileWriteLine($txt, "Report file for comparing " & $Path & " and report file " & $path2 & @CRLF & @CRLF & "There are " & UBound($aDifferent) - 1 & " files different" & @CRLF & @CRLF)

FileWriteLine($txt, @CRLF & @CRLF & "========================================================================" & @CRLF)

FileWriteLine($txt, "Files that are missing:" & @CRLF)

FileWriteLine($txt, "========================================================================" & @CRLF & @CRLF)

For $i = 1 To UBound($aDifferent) - 1

If StringInStr($aDifferent[$i], "is Missing") Then FileWriteLine($txt, StringReplace($aDifferent[$i], "|", ""))

Next

FileWriteLine($txt, @CRLF & @CRLF & "========================================================================" & @CRLF)

FileWriteLine($txt, "Files that are additional:" & @CRLF)

FileWriteLine($txt, "========================================================================" & @CRLF & @CRLF)

For $i = 1 To UBound($aDifferent) - 1

If StringInStr($aDifferent[$i], "is Additional") Then FileWriteLine($txt, StringReplace($aDifferent[$i], "|", ""))

Next

FileWriteLine($txt, @CRLF & @CRLF & "========================================================================" & @CRLF)

FileWriteLine($txt, "Files that are different version:" & @CRLF)

FileWriteLine($txt, "========================================================================" & @CRLF & @CRLF)

For $i = 1 To UBound($aDifferent) - 1

If StringInStr($aDifferent[$i], " Version Number") Then FileWriteLine($txt, StringReplace($aDifferent[$i], "|", ""))

Next

FileWriteLine($txt, @CRLF & @CRLF & "========================================================================" & @CRLF)

FileWriteLine($txt, "Files that are different Modified time:" & @CRLF)

FileWriteLine($txt, "========================================================================" & @CRLF & @CRLF)

For $i = 1 To UBound($aDifferent) - 1

If StringInStr($aDifferent[$i], " modified time") Then FileWriteLine($txt, StringReplace($aDifferent[$i], "|", ""))

Next

FileClose($txt)

MsgBox(0, "Done", "Differences file exported to " & $ReportDestination, 3)

Endif

; No destination report was selected

Case Else

;;;

EndSelect

WEnd

Func Search($current) ; Search for all files in Dir

;FirstTimeRun Get original DirSize and set up Gui

If $Run1 = 0 Then

$source = $current

If StringRight($current, 1) = '\' Then $current = StringTrimRight($current, 1)

$tosearch = $current

$Overall = DirGetSize($tosearch, 1)

$OverallQty = $Overall[1]

$Gui = GUICreate("Checking Files", 420, 100, -1, -1, -1, BitOr ($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))

$Progress0Text = GUICtrlCreateLabel("Please Wait", 10, 5, 400, 20, $SS_LEFTNOWORDWRAP)

$progressbar1 = GUICtrlCreateProgress(10, 20, 400, 20)

GUICtrlSetColor(-1, 32250)

$Progress1Text = GUICtrlCreateLabel("", 10, 44, 400, 20, $SS_LEFTNOWORDWRAP)

$progressbar2 = GUICtrlCreateProgress(10, 60, 400, 20, $PBS_SMOOTH)

$Progress2Text = GUICtrlCreateLabel("", 10, 82, 400, 20, $SS_LEFTNOWORDWRAP)

GUISetFont(10, 600)

$Progress2Text2 = GUICtrlCreateLabel("", 150, 62, 400, 20)

GUICtrlSetColor(-1, 0xffffff); not working with Windows XP Style if not using windows classic style

GUISetState(@SW_SHOW)

GUICtrlSetData($Progress1Text, "Working Directory " & $tosearch)

$Run1 = 1

EndIf

$Size = DirGetSize($current, 3)

$Qty = $Size[1]

Local $search = FileFindFirstFile($current & "\*.*")

While 1

Dim $file = FileFindNextFile($search)

If @error Or StringLen($file) < 1 Then ExitLoop

If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

;Found a file so deal with itand update progress

$Qty -= 1

$LocalPercent = 100 - (($Qty / $Size[1]) * 100)

$OverallQty -= 1

$overallpercent = 100 - (($OverallQty / $Overall[1]) * 100)

GUICtrlSetData($Progress0Text, "Total Progress " & Int($overallpercent) & "% completed")

GUICtrlSetData($progressbar1, $overallpercent)

GUICtrlSetData($progressbar2, $LocalPercent)

GUICtrlSetData($Progress2Text, "Checking File " & $file)

;Get all the information required on file

$ver = FileGetVersion($current & "\" & $file)

$mod = FileGetTime($current & "\" & $file, 0, 0)

$create = FileGetTime($current & "\" & $file, 1, 0)

;Make the array bigger to accomodate new data

ReDim $aFileList[uBound($aFileList) + 1][4]

;Add the data to the array

$aFileList[uBound($aFileList) - 1][0] = StringTrimLeft($current & "\" & $file, $Chop)

$aFileList[uBound($aFileList) - 1][1] = $ver

$aFileList[uBound($aFileList) - 1][2] = $create[2] & "/" & $create[1] & "/" & $create[0]

$aFileList[uBound($aFileList) - 1][3] = $mod[2] & "/" & $mod[1] & "/" & $mod[0] & "@" & $mod[3] & ":" & $mod[4] & "." & $mod[5]

EndIf

If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then ; if the attribute is D and not . or .. its a directory

GUICtrlSetData($Progress1Text, $current & "\" & $file) ; update the working directory label

Search($current & "\" & $file)

EndIf

WEnd

FileClose($search)

;when overall percent = 100 set end gui text, delete gui and reset run1 to 0

If $overallpercent = 100 Then

GUICtrlSetData($Progress0Text, "Total Progress 100% completed")

GUICtrlSetData($progressbar1, 100)

GUICtrlSetData($progressbar2, 100)

GUICtrlSetData($Progress2Text, "Done!")

GUIDelete($Gui)

$Run1 = 0 ;Finished so reset the Run1 var to 0 for next time

EndIf

EndFunc ;==>Search

Func Compare($Cpath) ; compare the search directory with a previously saved CSV file

$Fhandle = FileOpen($Cpath, 0) ; open the CSV

While 1 ;Read in lines until EOF

$line = FileReadLine($Fhandle)

If @error = -1 Then ExitLoop

If $line <> "Filename,Version,Created,Modified" Then ; ignore this line "Filename,Version,Created,Modified"

$split = StringSplit($line, ",") ;Split the line by commas

ReDim $aCompare[uBound($aCompare) + 1][4] ;Adjust the compare array size to accomodate new values

;Add new values to array

$aCompare[uBound($aCompare) - 1][0] = $split[1]

$aCompare[uBound($aCompare) - 1][1] = $split[2]

$aCompare[uBound($aCompare) - 1][2] = $split[3]

$aCompare[uBound($aCompare) - 1][3] = $split[4]

EndIf

WEnd

FileClose($Fhandle) ; close the CSV file

;initialise Vars

$Exist = 0

$VerDiff = ""

$ModDate = ""

;Create progress window for compare

$Gui = GUICreate("Comparing Data", 420, 100, -1, -1, -1, BitOr ($WS_EX_TOOLWINDOW , $WS_EX_TOPMOST))

$progressbar1 = GUICtrlCreateProgress(10, 20, 400, 20)

GUICtrlSetColor(-1, 32250)

$Progress1Text = GUICtrlCreateLabel("", 10, 44, 400, 20, $SS_LEFTNOWORDWRAP)

GUISetState()

GUICtrlSetData($Progress1Text, "Please Wait")

$StartQty = (UBound($aFileList) - 1) + (UBound($aCompare) - 1)

$DoneQty = (UBound($aFileList) - 1) + (UBound($aCompare) - 1)

For $i = 1 To UBound($aCompare) - 1 ;aCompare is the text file we are comparing too!

For $j = 1 To UBound($aFileList) - 1 ; Work through all values in the Filelist of the browesed directory

If $aFileList[$j][0] = $aCompare[$i][0] Then

$Exist = 1 ; the file exists in both the browse DIR and the Compare CSV

If $aFileList[$j][1] <> $aCompare[$i][1] Then $VerDiff = " version number" ;Check the version numbers match if not set version number as different

If $aFileList[$j][3] <> $aCompare[$i][3] Then $ModDate = " modified time" ;Check the modified time match if not set modified time as different

If $VerDiff <> "" Or $ModDate <> "" Then ;if a value for a difference has been set then

ReDim $aDifferent[uBound($aDifferent) + 1] ; adjust the array size to fit in new data

$aDifferent[uBound($aDifferent) - 1] = $aCompare[$i][0] & "|" & " is different" & "|" & $VerDiff & "|" & $ModDate ; add data to array with pipes so that it works with the list view

EndIf

ExitLoop

EndIf

Next

$Percent = 100 - (($DoneQty / $StartQty) * 100) ; set the progress

$DoneQty -= 1

GUICtrlSetData($progressbar1, $Percent)

If $Exist = 0 Then ; the file didn't exist in the directory

ReDim $aDifferent[uBound($aDifferent) + 1] ;resize array

$aDifferent[uBound($aDifferent) - 1] = $aCompare[$i][0] & "|" & " is missing" & "|" & $VerDiff & "|" & $ModDate ; add the missing value with pipes

;File Does not exist!

EndIf

;initialise Vars for the reverse sweep to see if files exist in the Folder file but not in the original CSV making it an additional file

$Exist = 0

$VerDiff = ""

$ModDate = ""

Next

For $i = 1 To UBound($aFileList) - 1 ;aCompare is the text file we are comparing too!

For $j = 1 To UBound($aCompare) - 1

If $aFileList[$i][0] = $aCompare[$j][0] Then

$Exist = 1

ExitLoop

EndIf

Next

If $Exist = 0 Then ;File did not exist in the original text file but does now!

ReDim $aDifferent[uBound($aDifferent) + 1] ;resize array

$aDifferent[uBound($aDifferent) - 1] = $aFileList[$i][0] & "|" & " is additional" & "|" & $VerDiff & "|" & $ModDate ;add the additional tag with pipes

EndIf

$Exist = 0 ;reset the exist marker

$Percent = 100 - (($DoneQty / $StartQty) * 100) ;set the progress

$DoneQty -= 1

GUICtrlSetData($progressbar1, $Percent)

Next

GUIDelete($Gui) ; delete the progress gui

_ArraySort($aDifferent) ;sort the differences array

For $i = 1 To UBound($aDifferent) - 1 ;insert the items into the listview

_GUICtrlListViewInsertItem ($List_7, -1, $aDifferent[$i])

Next

MsgBox(0, "Complete", "There are " & UBound($aDifferent) - 1 & " differences")

EndFunc ;==>Compare

Edited by ChrisL
Link to comment
Share on other sites

Most people prefer the following:

1) short scripts under 100 lines: In [ code ] tags

2) scripts over 100 lines: attachment.

[ codebox ] s are difficult to copy-paste into SciTE

#)

Link to comment
Share on other sites

It seems that the point at which it exits is the point where the mouse tip pops up as you hover over a file to select it.. still struggling if anyone could help.

I have attached the script in the first post

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