#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=icons\shell32_dll_Ico529_ico_Ico1.ico #AutoIt3Wrapper_Outfile=dir2subs.exe #AutoIt3Wrapper_Compile_Both=y #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** $sAppDesc = "" $sAppName = "Dir2Subs" $sAppAuthor = "Sean Shrum" $sAppVersion = "2.02" ; ============================================================================================== ; LANGUAGE OPTIONS Opt("TrayMenuMode",1) AutoItSetOption("MustDeclareVars", 0) ; ================================================================================================================ ; INCLUDES (update as needed) #Include #Include #include #include ; ================================================================================================================ ; DUPLICATE INSTANCE CHECK (may need to remove or move this around depending on where it is needed) If UBound(ProcessList(@ScriptName)) > 2 Then Exit ; ================================================================================================================ ; SCRIPT IDENTITY (this is based on the filename of the script/executable) $sAppEmail = StringLower($sAppName)& "@shrum.net" $sAppHome = "http://www.shrum.net/cats.php?dir=/sean/code/" & StringLower($sAppName) $sAppNews = $sAppHome & "/updates.html" $sAppSupport = "http://www.shrum.net/cats.php?dir=/sean/code/" & StringLower($sAppName) & "&body=/sean/support.shtml" $sAppFileType ="." & StringLower($sAppName) $aAppBuild = FileGetTime(@ScriptFullPath) $sAppBuild = $aAppBuild[0] & "." & $aAppBuild[1] & "." & $aAppBuild[2] & " @ " & $aAppBuild[3] & ":" & $aAppBuild[4] & ":" & $aAppBuild[5] $sAppTitle = $sAppName & " v." & $sAppVersion $sAppSettings = "HKCU\software\shrum.net\" & $sAppName ; ================================================================================================================ ; VARIABLE DECLARATIONS / DEFAULTS Global $bDebug=False, $bVerbose $iNewFolders = 0 $iNotCopied = 0 $iChars = ""; "1" $sOverwrite = ""; '"l" ; can be 'l'arger, 's'maller, 'n'ewer, 'o'lder, or 'y'es to overwrite regardless $sSource = ""; "K:\code\autoit-httpd\source\v.1\public_html\templates" $sDestination =""; "K:\code\autoit-httpd\source\v.1\public_html\templates" ; ================================================================================================================ ; TRAY ICON AND TOOLTIP SET (looks for .ico in same folder as .exe otherwise uses the build icon) TraySetToolTip($sAppTitle) TraySetIcon($sAppName & ".ico") ; ================================================================================================================ ; SPLASH SCREEN / TRAYTIP BUBBLE (*only* if no command line params are present: automated run/batch call) if $CmdLine[0] = 0 Then Traytip($sAppTitle,$sAppHome & @CRLF & "By " & $sAppAuthor & " :: " & $sAppEmail,5) ; ================================================================================================================ ; REGISTRY UPDATES (location, version, runcount) $iRunCount = RegRead($sAppSettings, "Executed") RegWrite($sAppSettings, "Executed", "REG_SZ", $iRunCount + 1) RegWrite($sAppSettings, "", "REG_SZ", @Scriptdir) RegWrite($sAppSettings, "Version", "REG_SZ", $sAppVersion) ; ================================================================================================================ ; PROGRAM GROUP / SHORTCUTS CREATION (fires only when Executed reg value = "" from above) If $iRunCount = "" Then DirCreate(@ProgramsCommonDir & "\shrum.net\" & $sAppName) FileCreateShortcut(@ScriptFullPath, @ProgramsCommonDir & "\shrum.net\" & $sAppName & "\" & $sAppName & ".lnk") FileCreateShortcut($sAppHome, @ProgramsCommonDir & "\shrum.net\" & $sAppName & "\Homepage.lnk") FileCreateShortcut($sAppSupport, @ProgramsCommonDir & "\shrum.net\Support.lnk") FileCreateShortcut(@ScriptFullPath, @UserProfileDir & "\application data\microsoft\Internet Explorer\Quick Launch\" & $sAppName & ".lnk") EndIf ; ================================================================================================================ ; SYSTRAY MENU DEFINITIONS (if any) TrayCreateItem ("[ Project ]") $mAbout = TrayCreateItem ("...About") TrayItemSetState (-1, $TRAY_DEFAULT) $mUpdate = TrayCreateItem ("...Update") $mHomepage = TrayCreateItem ("...Homepage") $mNews = TrayCreateItem ("...News") $mSupport = TrayCreateItem ("...Support") $mDebug = TrayCreateItem ("...Debug") $mExit = TrayCreateItem ("...Exit") TraySetState () TraySetClick (16) ; ================================================================================================================ ; LAST RUN SETTINGS LOAD _SettingsLoad() ; ================================================================================================================ ; COMMAND-LINE / .INI EVALUATION if $CmdLine[0] <> 0 Then for $i = 1 to $CmdLine[0] $aParam = StringSplit($CmdLine[$i], "=") $sParam = StringUpper(stringleft($aParam[1],1)) Select case $sParam = "C"; Chars $iChars = $aParam[2] case $sParam = "D"; Sestination $sDestination = $aParam[2] case $sParam = "S"; Source $sSource = $aParam[2] case $sParam = "O"; Overwrite $sOverwrite = $aParam[2] case $sParam = "V"; Verbose $bVerbose = stringupper($aParam[2]) case $sParam = "!"; Debug :: (setting '!=' to anything will turn debug on) _DefineSetting("Debug", True) EndSelect Next if $sDestination = "" then $sDestination = $sSource endif else if fileexists($sAppName & ".ini") Then $sMsg = "Run with the following settings (pulled from INI file)?" & @CRLF & @CRLF $sKey = "Chars" $sTemp = IniRead($sAppName & ".ini", @ComputerName, $sKey, "") if $sTemp = "" Then $sTemp = IniRead($sAppName & ".ini", "Default", $sKey, "") $sMsg &= " " & $sKey & " = " & $sTemp & @crlf $iChars = $sTemp $sKey = "Source" $sTemp = IniRead($sAppName & ".ini", @ComputerName, $sKey, "") if $sTemp = "" Then $sTemp = IniRead($sAppName & ".ini", "Default", $sKey, "") $sMsg &= " " & $sKey & " = " & $sTemp & @crlf $sSource = $sTemp $sKey = "Destination" $sTemp = IniRead($sAppName & ".ini", @ComputerName, $sKey, "") if $sTemp = "" Then $sTemp = IniRead($sAppName & ".ini", "Default", $sKey, "") $sMsg &= " " & $sKey & " = " & $sTemp & @crlf $sDestination = $sTemp $sKey = "Overwrite" $sTemp = IniRead($sAppName & ".ini", @ComputerName, $sKey, "") if $sTemp = "" Then $sTemp = IniRead($sAppName & ".ini", "Default", $sKey, "") $sMsg &= " " & $sKey & " = " & $sTemp & @crlf $sOverwrite = $sTemp $sKey = "Debug" $sTemp = IniRead($sAppName & ".ini", @ComputerName, $sKey, "") if $sTemp = "" Then $sTemp = IniRead($sAppName & ".ini", "Default", $sKey, "") $sMsg &= " " & $sKey & " = " & $sTemp & @crlf $bDebug = $sTemp $sKey = "Verbose" $sTemp = IniRead($sAppName & ".ini", @ComputerName, $sKey, "") if $sTemp = "" Then $sTemp = IniRead($sAppName & ".ini", "Default", $sKey, "") $sMsg &= " " & $sKey & " = " & $sTemp & @crlf $bVerbose = $sTemp if msgbox(4,$sAppTitle, $sMsg, 10) <> 6 then $iChars="" $sDestination="" $sSource="" $sOverwrite="" $bVerbose="" EndIf EndIf EndIf ; ================================================================================================================ ; ARGUMENT CHECKS / PROMPTS if $iChars = "" then _DefineSetting("Chars") if $sSource = "" then _DefineSetting("Source") if $sDestination = "" then _DefineSetting("Destination") if $sOverwrite = "" then _DefineSetting("Overwrite") ;~ if $bVerbose = "" then _DefineSetting("Verbose") if $bVerbose = "" then $bVerbose = True ; ================================================================================================================ ; SETTINGS SAVE (all user passed params should be formatted to program-desired formats by this point) _SettingsSave() ; ================================================================================================================ ; HOTKEY DEFINITIONS (if any) _HotkeysSet() ; ================================================================================================================ ; DEBUG WINDOW (notepad; if requested) if $bDebug then _DebugSetup() ; ================================================================================================================ ; MAIN PROGRAM if $bverbose then TrayTip($sAppTitle, "Retrieving filenames from: " & @CRLF & $sSource,5) $aFiles = _FileSearch($sSource, "*.*") if $aFiles[0] = 0 then traytip(0,$sAppTitle,"No files were found in the source folder. Exiting.",5) sleep(5000) Exit endif if $bverbose then TrayTip($sAppTitle, "Parsing " & $aFiles[0] & " filenames...",5) TraySetToolTip($sAppTitle & " - 0.00% done") For $i = 1 to $aFiles[0] - 1 $fPercent = round(($i / $aFiles[0]) * 100,2) $sFilename = stringright($aFiles[$i], stringlen($aFiles[$i]) - stringinstr($aFiles[$i], "\", "", -1)) $sChars = StringStripWS(Stringleft($sFilename, $iChars),7) ;~ if msgbox(4,"Pre-move check; is this correct?","From:" & @CRLF & $aFiles[$i] & @CRLF & "To:" & @CRLF & $sDestination & "\" & $sChars & "\" & $sFilename) = 7 then Exit if not FileExists($sDestination & "\" & $sChars) then $iNewFolders += 1 $sFrom = $aFiles[$i] $sTo = $sDestination & "\" & $sChars & "\" & $sFilename if $bVerbose then TrayTip($sAppTitle, "Moving " & $i & " of " & $aFiles[0] & @CRLF & $fPercent & "% done",5) if FileExists($sTo) Then Switch $sOverwrite case "l" if FileGetSize($sTo) < FileGetSize($sFrom) then filemove($sFrom, $sTo, 9) Else $iNotCopied += 1 EndIf case "s" if FileGetSize($sTo) > FileGetSize($sFrom) then filemove($sFrom, $sTo, 9) Else $iNotCopied += 1 EndIf case "o" if FileGetTime($sTo) > FileGetTime($sFrom) then filemove($sFrom, $sTo, 9) Else $iNotCopied += 1 EndIf case "n" if FileGetTime($sTo) < FileGetTime($sFrom) then filemove($sFrom, $sTo, 9) Else $iNotCopied += 1 EndIf case "y" filemove($sFrom, $sTo, 9) case Else $iNotCopied += 1 EndSwitch Else filemove($sFrom, $sTo, 8) EndIf Next _ShellExecute($sDestination) if $bverbose then TrayTip($sAppTitle, $aFiles & " file(s) moved / " & $iNotCopied & " not moved (due to overwrite condition)" & @CRLF & $iNewFolders & " new folder(s) created",5) Sleep(5000) EndIf ; ================================================================================================================ ; FUNCTIONS Func _AboutDialog() _ConsoleWrite("+ _AboutDialog()") msgbox(0,"About", $sAppTitle & " ( build " & $sAppBuild & " ) by " & $sAppAuthor & @crlf & $sAppHome & " :: " & StringLower($sAppName) & "@shrum.net") _ConsoleWrite("- _AboutDialog()") EndFunc Func _ArrayMerge (ByRef $aTemp1, $aTemp2, $iBase=1) _Consolewrite("+ _ArrayMerge()") $iTimer = TimerInit() $iRows1 = ubound($aTemp1) - $iBase $iCols1 = ubound($aTemp1,2) $iRows2= ubound($aTemp2) - $iBase $iCols2 = ubound($aTemp2,2) If $iCols1 <> $iCols2 then seterror(1) Exit endif if $iCols1 = 0 Then $iTotal = $aTemp1[0] + $aTemp2[0] redim $aTemp1[$iTotal+1] For $i = $aTemp1[0] + $iBase to $iTotal $aTemp1[$i] = $aTemp2[$i - ($aTemp1[0] + $iBase) + 1] next $aTemp1[0] = $iTotal Else $iTotal = $aTemp1[0][0] + $aTemp2[0][0] redim $aTemp1[$iTotal+1][$iCols1] For $i = $aTemp1[0][0] + $iBase to $iTotal for $j = 0 to ($iCols1 - 1) $aTemp1[$i][$j] = $aTemp2[$i - ($aTemp1[0][0] + $iBase) + 1][$j] Next next $aTemp1[0][0] = $iTotal EndIf _ConsoleWrite(": " & $iTotal & " entries merged successfully") _ConsoleWrite(": Complete in " & Round(TimerDiff($iTimer)/1000,2) & " secs" & @CRLF) _Consolewrite("- _ArrayMerge()") EndFunc func _ConsoleWrite($sMsg) _DebugOut($sMsg) Consolewrite($sMsg & @CRLF) EndFunc Func _DefineSetting($sSetting="", $sValue="") _consolewrite("+ _DefineSetting()") _ConsoleWrite(": Setting '" & $sSetting & "' = '" & $sValue & "'") if $sSetting = "" or $sSetting = "Chars" Then $iChars = InputBox($sAppTitle, "Define the number of left [c]haracters to evaluate/substructure (1='foobar.txt'->'f', 2='foobar.txt'->'fo', etc.).","2") if @error then Exit EndIf if $sSetting = "" or $sSetting = "Source" Then $sSource = FileSelectFolder("Select the [s]ource folder that contains the files you want moved:","") if @error then Exit EndIf if $sSetting = "" or $sSetting = "Destination" Then $sDestination = FileSelectFolder("Select a [d]estination folder to create your substructure in:","",7,$sSource) if @error then Exit EndIf if $sSetting = "" or $sSetting = "Overwrite" Then if msgbox(4,$sAppTitle,"Do you want overwrite files that exist in the destination? (You'll be prompted for conditions if you answer yes)") = 6 then $sOverwrite = InputBox($sAppTitle, "On what condition do you want source files to overwrite destination files? Type 'l' for larger size, 's' for smaller size, 'n' for newer, 'o' for older, or 'y' for everytime.","l") if @error then Exit EndIf EndIf if $sSetting = "" or $sSetting = "Debug" Then if $sValue then $bDebug = $sValue Else if not $bDebug then $bDebug = True Else $bDebug = False EndIf endif if $bDebug Then _DebugSetup($sAppTitle & " - debugging info") _ConsoleWrite("Debug console :: " & $sAppTitle & " :: build " & $sAppBuild) _ConsoleWrite("========================[ START ]=============================") opt("TrayIconDebug",1) TrayItemSetState($mDebug,1) Else _ConsoleWrite("=========================[ END ]==============================") opt("TrayIconDebug",0) TraySetToolTip($sAppTitle) TrayItemSetState($mDebug,4) EndIf EndIf if $sSetting = "" or $sSetting = "Verbose" Then if $sValue then $bVerbose = $sValue Else if msgbox(4,$sApptitle, "Enable [v]erbose notifications?") = 7 Then $bverbose = "N" if $bVerbose = "N" or "false" Then _DefineSetting("Verbose", False) Else _DefineSetting("Verbose", True) EndIf endif EndIf _consolewrite("- _DefineSetting()") EndFunc Func _Exit() _ConsoleWrite("+ _Exit()") _SettingsSave() Exit EndFunc Func _FileSearch($sPath, $sMask="*.*", $sArguments="/a-d /b /s /on", $iStripped=0) _ConsoleWrite("+ _FileSearch()") $iTimer = TimerInit() local $iLine = 0, $sLine = "", $aLine = "", $aRaw Dim $aFiles[1] $aFiles[0] = 0 $sPath = StringReplace($sPath & "\", "\\", "\") $iPathLen = stringlen($sPath) $aMask = stringsplit($sMask, ",;|") for $i = 1 to $aMask[0] _ConsoleWrite(': dir "' & $sPath & $aMask[$i] & '" ' & $sArguments & ' > ' & @TempDir & '\filelist.tmp') Runwait(@ComSpec & ' /c dir "' & $sPath & $aMask[$i] & '" ' & $sArguments & " > " & @TempDir & "\filelist.tmp", @SystemDir, @SW_HIDE) _FileReadToArray(@TempDir & "\filelist.tmp", $aRaw) if $aRaw[$aRaw[0]] = "" Then Redim $aRaw[$aRaw[0]] $aRaw[0] = $aRaw[0] - 1 EndIf _ArrayMerge($aFiles, $aRaw) next $aFiles[0] = UBound($aFiles,1) - 1 _ConsoleWrite(": Returning " & $aFiles[0] & " entries") _ConsoleWrite("- _FileSearch()") Return $aFiles EndFunc func _HotkeysSet() _ConsoleWrite("+ _HotkeysSet()") hotkeyset("!^x", "_exit") _ConsoleWrite("- _HotkeysSet()") EndFunc func _MenuMonitor() $msg = TrayGetMsg () Select Case $msg = $TRAY_EVENT_PRIMARYDOWN _AboutDialog() Case $msg = $mAbout _AboutDialog() Case $msg = $mDebug _DefineSetting("Debug", Not $bDebug) case $msg = $mUpdate _UpdateCheck() case $msg = $mHomepage _ShellExecute($sAppHome) case $msg = $mSupport _ShellExecute($sAppSupport) case $msg = $mNews _ShellExecute($sAppNews) case $msg = $mExit _Exit() EndSelect EndFunc Func _SettingsLoad() _ConsoleWrite("+ _SettingsLoad()") ;~ ****** $bVerbose = RegRead($sAppSettings, "Verbose") _ConsoleWrite("- _SettingsLoad()") EndFunc Func _SettingsSave() _ConsoleWrite("+ _SettingsSave()") ;~ ****** Regwrite($sAppSettings, "Verbose", "REG_SZ", $bVerbose) _ConsoleWrite("- _SettingsSave()") EndFunc Func _ShellExecute($sCmd) _ConsoleWrite("+ _ShellExecute()") if $sCmd = "" Then _ConsoleWrite(": No command passed") Else _ConsoleWrite(": Running '" & $sCmd & "'") shellExecute($sCmd) EndIf _ConsoleWrite("- _ShellExecute()") endfunc Func _ShellExecuteWait($sCmd) _ConsoleWrite("+ _ShellExecuteWait()") if $sCmd = "" Then _ConsoleWrite(": No command passed") Else _ConsoleWrite(": Running '" & $sCmd & "'") ShellExecuteWait($sCmd) EndIf _ConsoleWrite("- _ShellExecuteWait()") endfunc func _UpdateCheck() _ConsoleWrite("+ _UpdateCheck()") $sTemp = "" $aTemp = stringsplit(_InetGetSource($sAppHome & "/" & stringlower($sAppName) & ".au3"), @CRLF) For $i = 1 to $aTemp[0] if StringInStr($aTemp[$i], "$sAppVersion") then $sTemp = $aTemp[$i] ExitLoop endif Next if $sTemp <> "" then $aOnlineVersion = StringSplit($sTemp, "=") $sOnlineVersion = StringReplace(StringStripWS($aOnlineVersion[2], 8), '"', "") _ConsoleWrite(": Online = " & $sOnlineVersion) _ConsoleWrite(": User = " & $sAppVersion) if $sOnlineVersion <> "" and Not Stringinstr($sOnlineVersion, "") Then If $sAppVersion < $sOnlineVersion Then _ConsoleWrite(": User is OLDER") $sMsg = "A newer version of this program is available for download." & @CRLF & @CRLF $sMsg &= "Do you want to visit the " & $sAppname & " site to download v." & $sOnlineVersion & "?" & @CRLF & @CRLF $sMsg &= "Selecting 'Yes' will close this app and take you to the website." If MsgBox(4, $sAppTitle, $sMsg) = 6 Then _ConsoleWrite(": Launching default web browser to " & $sAppHome) _ShellExecute($sAppHome) Exit EndIf ElseIf $sAppVersion >= $sOnlineVersion Then _ConsoleWrite(": User is UP-TO-DATE") TrayTip($sAppTitle, "Your version is up-to-date.",5) EndIf Else _ConsoleWrite(": Data received but no version info found") traytip($sAppTitle,"Unable to locate versioning info in data received." & @CRLF & "Please go to the homepage for further update details.",5) EndIf Else _ConsoleWrite(": Unable to obtain version content") traytip($sAppTitle,"Unable to obtain online version info." & @CRLF & "Check your connection and try again.",5) EndIf _ConsoleWrite("- _UpdateCheck()") EndFunc