 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
jaco0646
Joined: 07 Oct 2006 Posts: 556 Location: MN, USA
|
Posted: Sat Oct 21, 2006 5:05 am Post subject: Directory Compare |
|
|
This is a script that I use to check whether or not the files from my laptop are backed up onto my desktop PC and vice versa. It compares the contents of two folders based on file names and sizes. Other comparisons could easily be added as well, such as time stamps.
Simply add the absolute paths of two folders near the top where the script indicates, and it will show which files are unique to each. This has only been tested on WinXP. For another OS, the "Static9" control will likely be something else.
| Code: | #SingleInstance force
#NoTrayIcon
;__________________________________________________________
path1 = ;<-- Enter a folder path here
path2 = ;<-- Enter a second folder path here
;__________________________________________________________
Gui, Add, ListView, w640 h400 Grid
, %path1%|Size(KB)|%path2%|Size(KB)
count := Count(path1) + Count(path2)
Progress, fs14 b2 r0-%count%, Checking files`, please wait.
Compare(path1, path2, 1)
Compare(path2, path1, 3)
Progress, Off
If LV_GetCount() = 0
{
MsgBox, 4160, Equal, All files are backed up.
ExitApp
}
LV_ModifyCol()
LV_ModifyCol(2, "Float")
LV_ModifyCol(4, "Float")
Gui, Show,, Directory Compare
return
GuiClose:
ExitApp
Count(path)
{
SetTitleMatchMode, 2
Run, properties %path%, %path%, Hide
WinWait, Properties ahk_class #32770
Loop
{
ControlGetText, count, Static9
If count !=
break
}
WinClose
StringGetPos, num, count, %A_Space%
StringLeft, count, count, num
return, count
}
Compare(path1, path2, num)
{
Static count
Loop, %path1%\*.*,,1
{
count++
Progress, %count%
FileGetSize, size1, %A_LoopFileLongPath%
StringReplace, name, A_LoopFileLongPath, %path1%\
IfExist, %path2%\%name%
{
FileGetSize, size2, %path2%\%name%
If (size1 = size2)
continue
}
i++
If name contains \
name = \%name%
SetFormat, Float, 0.2
If (LV_GetCount() < i)
LV_Add("Col" num, name, size1/1024)
Else LV_Modify(i, "Col" num, name, size1/1024)
}
} |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5887
|
Posted: Sat Oct 21, 2006 8:10 am Post subject: |
|
|
Dear jaco0646,
Nice! ..
| Quote: | | This has only been tested on WinXP. For another OS, the "Static9" control will likely be something else |
The script runs fine in Windows 2000.
I compared between AutoHotkey versions 1.0.44.00 & 1.0.44.13.
Here is a snapshot: http://autohotkey.net/~goyyah/samples/directorycompare.png
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
Directory Guest
|
Posted: Sat Oct 21, 2006 12:35 pm Post subject: Re: Directory Compare |
|
|
I try in XP Pro: | jaco0646 wrote: | path1 = C:\LP\
path2 = D:\Backup\LP\ |
But doing nothing, whats wrong please? |
|
| Back to top |
|
 |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Sat Oct 21, 2006 1:17 pm Post subject: |
|
|
| Quote: |
path1 = C:\LP\
path2 = D:\Backup\LP\
But doing nothing, whats wrong please?
|
I've added basic directory checking to this version. The original gives no warning if the paths are not exactly correct.
| Code: | #SingleInstance force
#NoTrayIcon
;__________________________________________________________
path1 = ;<-- Enter a folder path here
path2 = ;<-- Enter a second folder path here
;__________________________________________________________
; Check if paths exists, if not - warn.
FileGetAttrib, attrib, %path1%
IfNotInString, attrib, D
{
MsgBox, 4096, Warning, Could not open: %path1%, 30
Return
}
FileGetAttrib, attrib, %path2%
IfNotInString, attrib, D
{
MsgBox, 4096, Warning, Could not open: %path2%, 30
Return
}
Gui, Add, ListView, w640 h400 Grid
, %path1%|Size(KB)|%path2%|Size(KB)
Count := Count(path1) + Count(path2)
Progress, fs14 b2 r0-%Count%, Checking files`, please wait.
Compare(path1, path2, 1)
Compare(path2, path1, 3)
Progress, Off
If LV_GetCount() = 0
{
MsgBox, 4160, Equal, All files are backed up.
ExitApp
}
LV_ModifyCol()
LV_ModifyCol(2, "Float")
LV_ModifyCol(4, "Float")
Gui, Show,, Directory Compare
Return
GuiClose:
ExitApp
Count(path)
{
SetTitleMatchMode, 2
Run, properties %path%, %path%, Hide
WinWait, Properties ahk_class #32770
Loop
{
ControlGetText, Count, static9
If Count !=
Break
}
WinClose
StringGetPos, num, Count, %A_Space%
StringLeft, Count, Count, num
Return, Count
}
Compare(path1, path2, num)
{
static Count
Loop, %path1%\*.*,,1
{
Count++
Progress, %Count%
FileGetSize, size1, %A_LoopFileLongPath%
StringReplace, name, A_LoopFileLongPath, %path1%\
IfExist, %path2%\%name%
{
FileGetSize, size2, %path2%\%name%
If (size1 = size2)
Continue
}
i++
If name Contains \
name = \%name%
SetFormat, Float, 0.2
If (LV_GetCount() < i)
LV_Add("Col" num, name, size1/1024)
Else LV_Modify(i, "Col" num, name, size1/1024)
}
}
|
_________________ ChrisM |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 556 Location: MN, USA
|
Posted: Sat Oct 21, 2006 3:14 pm Post subject: |
|
|
@ Directory:
You don't need to add the final backslash: " \ " to the paths. The script already does that for you. I didn't think to mention that before, but removing those should correct the problem.
| Code: | path1 = C:\LP
path2 = D:\Backup\LP |
@ ChrisM
Directory checking is a good idea. The only reason I didn't add it is that I always compare the same two folders and rarely any others. Perhaps the best method would be to add two FileSelectFolder commands for picking the directories. This would ensure that there are no trailing backslashes as well, eliminating the problem that Directory encountered. |
|
| Back to top |
|
 |
Directory Guest
|
Posted: Sat Oct 21, 2006 4:28 pm Post subject: |
|
|
| jaco0646 wrote: | @ Directory:
You don't need to add the final backslash: " \ " to the paths. The script already does that for you. I didn't think to mention that before, but removing those should correct the problem.
| Code: | path1 = C:\LP
path2 = D:\Backup\LP |
|
Thank you, but I was try with and without " \ ", same thing with the add of ChrisM (Tank you too)
maybe is my sistem problem...  |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 556 Location: MN, USA
|
Posted: Sat Oct 21, 2006 10:14 pm Post subject: |
|
|
I can't think of what the problem could be. If you don't see anything after running the script, it might be stuck in the count() function at WinWait, or in the loop thereafter. You could try limiting the loop to a number of iterations and see if that helps.
If you do find a solution, please post it. |
|
| Back to top |
|
 |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Sun Oct 22, 2006 12:49 pm Post subject: |
|
|
Here's a version that has file selection built into the GUI. Basic functionality is still the same.
One thing I can't figure out is how to get the FileSelectFolder dialogue to appear in the middle of the page. For me it's always too low and to the right so the ok and cancel buttons are off the screen.
| Code: | ; This is a script that I use to check whether or not the files from my laptop
; are backed up onto my desktop PC and vice versa. It compares the contents of
; two folders based on file names and sizes. Other comparisons could easily be
; added as well, such as time stamps.
; Simply start the script and type in or select the folders to compare,
; and it will show which files are unique to each.
; This has only been tested on WinXP. For another OS, the "Static9" control
; will likely be something else.
#SingleInstance force
#NoTrayIcon
#NoEnv ; Must have this so path vars below don't resolve to env path.
; Default path start points
path1 =
path2 =
; Read paths in from ini file
SplitPath, A_ScriptFullPath, , dir, , iniFile
iniFile = %dir%\%iniFile%.ini
i = 0
Loop
{
i++
FileReadLine, tmp, %iniFile%, %i%
If ErrorLevel
Break
path%i% := tmp
GuiControl,, path%i%, %tmp%
}
Gui, Add, Edit, x16 y20 w350 h20 vpath1
Gui, Add, Edit, x446 y20 w350 h20 vpath2
Gui, Add, Button, x370 y20 w60 h20 gSelectPath1, Browse
Gui, Add, Button, x800 y20 w60 h20 gSelectPath2, Browse
Gui, Add, Button, x16 y50 w160 h30 gCompareDirs, Compare
Gui, Add, ListView, x16 y90 w840 h540 Grid
, %path1%|Size(KB)|%path2%|Size(KB)
wCol1 = 330
wCol2 = 80
wCol3 = 330
wCol4 = 80
LV_ModifyCol()
LV_ModifyCol(2, "Float")
LV_ModifyCol(4, "Float")
LV_ModifyCol( 1, wCol1)
LV_ModifyCol( 2, wCol2)
LV_ModifyCol( 3, wCol3)
LV_ModifyCol( 4, wCol4)
Gui, Show, x82 y35 h646 w880, Directory Compare
i = 0
Loop, 2
{
i++
tmp := path%i%
GuiControl,, path%i%, %tmp%
}
Return
GuiClose:
ExitApp
Return
SaveToIni()
{
global
; Save paths to ini file
FileDelete, %iniFile%
i = 0
Loop, 2
{
i++
tmp := path%i%
FileAppend, %tmp%`n, %iniFile%
}
}
SelectPath1:
{
path1 := SelectFolder( path1 )
GuiControl,, path1, %path1%
Return
}
SelectPath2:
{
path2 := SelectFolder( path2 )
GuiControl,, path2, %path2%
Return
}
SelectFolder( path )
{
Gui +OwnDialogs
tmp = %path%
FileSelectFolder, path, *%path%, 2
If ErrorLevel
path = %tmp%
CheckDir( path )
Return path
}
CheckDir( path )
{
; Check if paths exists, if not - warn.
FileGetAttrib, attrib, %path%
IfNotInString, attrib, D
{
MsgBox, 4096, Warning, Could not open: %path%, 30
Return 1
}
Return 0
}
CompareDirs:
{
LV_Delete()
LV_ModifyCol( 1, wCol1, path1)
LV_ModifyCol( 3, wCol3, path2)
Gui Submit, NoHide
SaveToIni()
If ( CheckDir( path1 ) )
Return
If ( CheckDir( path2 ) )
Return
Count := Count(path1) + Count(path2)
Progress, fs14 b2 r0-%Count%, Checking files`, please wait.
Compare(path1, path2, 1)
Compare(path2, path1, 3)
Progress, Off
If LV_GetCount() = 0
{
MsgBox, 4160, Equal, All files are backed up.
}
Return
}
Count(path)
{
SetTitleMatchMode, 2
Run, properties %path%, %path%, Hide
WinWait, Properties ahk_class #32770
Loop
{
ControlGetText, Count, static9
If Count !=
Break
}
WinClose
StringGetPos, num, Count, %A_Space%
StringLeft, Count, Count, num
Return, Count
}
Compare(path1, path2, num)
{
static Count
Loop, %path1%\*.*,,1
{
Count++
Progress, %Count%
FileGetSize, size1, %A_LoopFileLongPath%
StringReplace, name, A_LoopFileLongPath, %path1%\
IfExist, %path2%\%name%
{
FileGetSize, size2, %path2%\%name%
If (size1 = size2)
Continue
}
i++
If name Contains \
name = \%name%
SetFormat, Float, 0.2
If (LV_GetCount() < i)
LV_Add("Col" num, name, size1/1024)
Else LV_Modify(i, "Col" num, name, size1/1024)
}
Return
} |
_________________ ChrisM
Last edited by ChrisM on Sun Oct 22, 2006 10:19 pm; edited 1 time in total |
|
| Back to top |
|
 |
ChrisM
Joined: 28 Nov 2004 Posts: 58
|
Posted: Sun Oct 22, 2006 9:58 pm Post subject: |
|
|
The above code was just edited to fix a bug that wasn't storing the ini file to the same dir as the script. _________________ ChrisM |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 556 Location: MN, USA
|
Posted: Fri Sep 21, 2007 10:01 pm Post subject: update |
|
|
I had occasion to use this script a couple days ago, and I discovered that when comparing files on two drives with different cluster sizes, the size reported for identical files will be slightly different between the two.
To solve this, I incorporated a ratio into the comparison procedure, so that files with identical names and almost identical sizes are considered equal.
Several minor changes were also made; most notably, files with unique sizes are now separated from files with unique names in the final list view (unique sizes come first).
Update: I've made a new GUI frontend for the program, with an edit box to either manually enter the paths or drag-and-drop folders onto it. It now has error checking as well.
Here is the updated script: >DOWNLOAD<
 _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|