Advertisement
AZJIO

Сортировка файлов по расширению

Sep 23rd, 2011
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 9.76 KB | None | 0 0
  1. #region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_OutFile=Sort.exe
  3. #AutoIt3Wrapper_icon=Sort.ico
  4. #AutoIt3Wrapper_Compression=4
  5. #AutoIt3Wrapper_UseAnsi=y
  6. #AutoIt3Wrapper_Res_Comment=-
  7. #AutoIt3Wrapper_Res_Description=Sort.exe
  8. #AutoIt3Wrapper_Res_Fileversion=0.2.0.0
  9. #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=n
  10. #AutoIt3Wrapper_Res_LegalCopyright=AZJIO
  11. #AutoIt3Wrapper_Res_Language=1033
  12. #AutoIt3Wrapper_Run_AU3Check=n
  13. #AutoIt3Wrapper_Res_Field=Version|0.1
  14. #AutoIt3Wrapper_Res_Field=Build|2011.09.24
  15. #AutoIt3Wrapper_Res_Field=Coded by|AZJIO
  16. #AutoIt3Wrapper_Res_Field=Compile date|%longdate% %time%
  17. #AutoIt3Wrapper_Res_Field=AutoIt Version|%AutoItVer%
  18. #AutoIt3Wrapper_Run_Obfuscator=y
  19. #Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0
  20. #AutoIt3Wrapper_Run_After=del /f /q "%scriptdir%\%scriptfile%_Obfuscated.au3"
  21. #endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
  22.  
  23. #NoTrayIcon
  24. Opt("GUIOnEventMode", 1)
  25. #include <FileOperations.au3>
  26. #include <GUIConstantsEx.au3>
  27. #include <WindowsConstants.au3>
  28.  
  29. Global $iniTypeFile = 'avi;mpg;mpeg;mp4;vob;mkv;asf;asx;wmv;mov;3gp;flv;bik|mp3;wav;wma;ogg;ac3|bak;gid;log;tmp' & _
  30.         '|htm;html;css;js;php|bmp;gif;jpg;jpeg;png;tif;tiff|exe;msi;scr;dll;cpl;ax|com;sys;bat;cmd'
  31. Global $ini = @ScriptDir & '\sort_files.ini'
  32.  
  33. If Not FileExists($ini) And DriveStatus(StringLeft(@ScriptDir, 1)) <> 'NOTREADY' Then
  34.     $file = FileOpen($ini, 2)
  35.     FileWrite($file, '[Set]' & @CRLF & _
  36.             'TypeFile=' & $iniTypeFile & @CRLF & _
  37.             'TrType=' & @CRLF & _
  38.             'LastType=' & @CRLF & _
  39.             'Size=' & @CRLF & _
  40.             'PathOut=' & @CRLF & _
  41.             'Path=')
  42.     FileClose($file)
  43. EndIf
  44.  
  45. $iniTypeFile = IniRead($ini, 'Set', 'TypeFile', $iniTypeFile)
  46. $iniPath = IniRead($ini, 'Set', 'Path', '')
  47. $iniOutPath = IniRead($ini, 'Set', 'PathOut', '')
  48. $LastType = IniRead($ini, 'Set', 'LastType', '')
  49. $iniSize = IniRead($ini, 'Set', 'Size', '')
  50. $iniTrType = IniRead($ini, 'Set', 'TrType', '')
  51.  
  52. If $LastType And $iniTypeFile And StringInStr('|' & $iniTypeFile & '|', '|' & $LastType & '|') Then
  53.     StringReplace('|' & $iniTypeFile & '|', '|' & $LastType & '|', '|')
  54.     StringTrimLeft($iniTypeFile, 1)
  55.     StringTrimRight($iniTypeFile, 1)
  56. EndIf
  57.  
  58. GUICreate("Сортировка по расширению", 450, 230, -1, -1, -1, $WS_EX_ACCEPTFILES) ; размер окна
  59. If Not @Compiled Then GUISetIcon(@ScriptDir & '\Sort.ico')
  60. ; GUISetBkColor(0xF9F9F9)
  61. GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
  62. GUISetOnEvent($GUI_EVENT_DROPPED, "_Dropped")
  63.  
  64. $StatusBar = GUICtrlCreateLabel(@CRLF & 'Строка состояния', 5, 196, 330, 34)
  65.  
  66. GUICtrlCreateLabel("Путь - источник (используйте drag-and-drop)", 20, 13, 400, 20)
  67.  
  68. $inpPath = GUICtrlCreateInput("", 20, 30, 410, 22)
  69. GUICtrlSetState(-1, 8)
  70. If $iniPath Then GUICtrlSetData(-1, $iniPath)
  71.  
  72. GUICtrlCreateLabel("Путь - получатель (используйте drag-and-drop)", 20, 63, 400, 20)
  73. $OutPath = GUICtrlCreateInput("", 20, 80, 410, 22)
  74. GUICtrlSetState(-1, 8)
  75. If $iniOutPath Then GUICtrlSetData(-1, $iniOutPath)
  76.  
  77. $Ignore = GUICtrlCreateRadio("Игнорировать типы файлов", 20, 113, 180, 17)
  78. GUICtrlSetTip(-1, "Если поле раскрывающегося списка" & @CRLF & "пустое, то этот пункт не учитывается")
  79. $deltype = GUICtrlCreateRadio("Удалять типы файлов", 20, 130, 180, 17)
  80. GUICtrlSetTip(-1, "Если поле раскрывающегося списка" & @CRLF & "пустое, то этот пункт не учитывается")
  81.  
  82. $typefile = GUICtrlCreateCombo("", 200, 117, 230, 24)
  83. GUICtrlSetData(-1, $LastType & '|' & $iniTypeFile, $LastType)
  84. GUICtrlSetTip(-1, "Оставте поле пустым" & @CRLF & "для игнорирования параметра")
  85.  
  86. GUICtrlCreateLabel("Удалять файлы менее указанного размера, кб", 20, 160, 260, 20)
  87. $size = GUICtrlCreateInput("", 280, 157, 100, 22)
  88. GUICtrlSetTip(-1, "Оставте поле пустым" & @CRLF & "для игнорирования размера")
  89. If $iniSize Then GUICtrlSetData(-1, $iniSize)
  90.  
  91. $checkAtrb = GUICtrlCreateCheckbox("Снять атрибут", 20, 180, 95, 17)
  92. GUICtrlSetState(-1, 1)
  93.  
  94. $startsh = GUICtrlCreateButton("Сортировать", 340, 190, 90, 33)
  95. GUICtrlSetOnEvent(-1, "_Sort")
  96.  
  97. $typefile0 = GUICtrlRead($typefile)
  98. Switch $iniTrType
  99.     Case 1
  100.         If $typefile0 Then GUICtrlSetState($Ignore, 1)
  101.     Case 2
  102.         If $typefile0 Then GUICtrlSetState($deltype, 1)
  103. EndSwitch
  104.  
  105. GUISetState()
  106. OnAutoItExitRegister("_Exit_Save_Ini")
  107.  
  108. While 1
  109.     Sleep(10000000)
  110. WEnd
  111.  
  112. Func _Exit_Save_Ini()
  113.     If DriveStatus(StringLeft(@ScriptDir, 1)) <> 'NOTREADY' Then
  114.         $typefile0 = GUICtrlRead($typefile)
  115.         If $typefile0 <> $LastType Then IniWrite($ini, 'Set', 'LastType', $typefile0)
  116.        
  117.         $inpPath0 = GUICtrlRead($inpPath)
  118.         If FileExists($inpPath0) And $iniPath <> $inpPath0 Then IniWrite($ini, 'Set', 'Path', $inpPath0)
  119.        
  120.         $OutPath0 = GUICtrlRead($OutPath)
  121.         If FileExists($OutPath0) And $iniOutPath <> $OutPath0 Then IniWrite($ini, 'Set', 'PathOut', $OutPath0)
  122.        
  123.         $size0 = GUICtrlRead($size)
  124.         If $size0 <> $iniSize Then IniWrite($ini, 'Set', 'Size', $size0)
  125.        
  126.         If GUICtrlRead($Ignore) = 1 And $iniTrType <> 1 Then
  127.             IniWrite($ini, 'Set', 'TrType', 1)
  128.         ElseIf GUICtrlRead($deltype) = 1 And $iniTrType <> 2 Then
  129.             IniWrite($ini, 'Set', 'TrType', 2)
  130.         ElseIf GUICtrlRead($typefile) = '' And $iniTrType <> 0 Then
  131.             IniWrite($ini, 'Set', 'TrType', 0)
  132.         EndIf
  133.     EndIf
  134. EndFunc   ;==>_Exit_Save_Ini
  135.  
  136. Func _Sort()
  137.     $del0 = 0
  138.     $ign0 = 0
  139.     $move0 = 0
  140.     $checkAtrb0 = GUICtrlRead($checkAtrb)
  141.     $Ignore0 = GUICtrlRead($Ignore)
  142.     $deltype0 = GUICtrlRead($deltype)
  143.     $OutPath0 = GUICtrlRead($OutPath)
  144.     $inpPath0 = GUICtrlRead($inpPath)
  145.     $typefile0 = StringRegExpReplace(';' & GUICtrlRead($typefile) & ';', '[;]+', ';')
  146.     $size0 = 1024 * GUICtrlRead($size)
  147.     GUICtrlSetData($StatusBar, @CRLF & 'Началось...')
  148.     If StringRegExpReplace($OutPath0, '[A-z]:\\[^/:*?"<>|]*', '') Or StringInStr($OutPath0, '\\') Or Not $OutPath0 Then Return MsgBox(0, 'Ошибка', 'Путь содержит неверные символы')
  149.     If StringRight($OutPath0, 1) = '\' Then $OutPath0 = StringTrimRight($OutPath0, 1)
  150.     ; поиск файлов
  151.     If FileExists($inpPath0) Then
  152.         $inpPath1 = StringRegExpReplace($inpPath0, "(^.*)\\(.*)$", '\1')
  153.         $FileList = _FO_FileSearch($inpPath0, '*', True, 125, 1, 0) ; список полных путей
  154.         If @error Then
  155.             GUICtrlSetData($StatusBar, 'Файлов ненайдено')
  156.             $FileList = ''
  157.             Return
  158.         EndIf
  159.         $FileList = StringRegExp($FileList, '(?m)^(.*\\)(.*?)(?:\r|\z)', 3)
  160.         $ArrSz = UBound($FileList)
  161.        
  162.         GUICtrlSetData($StatusBar, @CRLF & 'Обрабатываются ' & $ArrSz / 2 & ' файлов')
  163.         ; $fileName = $FileList[$i+1]
  164.         For $i = 0 To $ArrSz - 1 Step 2
  165.             If StringInStr($FileList[$i + 1], '.') Then
  166.                 $tmptype = StringTrimLeft($FileList[$i + 1], StringInStr($FileList[$i + 1], '.', 0, -1)) ; получить расширение файла
  167.                 $typeFolder = $tmptype
  168.                
  169.                 If $deltype0 = 1 And $typefile0 And StringInStr($typefile0, ';' & $tmptype & ';') Then ; удалить типы файлов
  170.                     If $checkAtrb0 = 1 Then FileSetAttrib($FileList[$i] & $FileList[$i + 1], "-RASHT")
  171.                     If FileDelete($FileList[$i] & $FileList[$i + 1]) Then
  172.                         $del0 += 1
  173.                     Else
  174.                         $ign0 += 1
  175.                     EndIf
  176.                     ContinueLoop
  177.                 ElseIf $Ignore0 = 1 And $typefile0 And StringInStr($typefile0, ';' & $tmptype & ';') Then ; игнорировать
  178.                     $ign0 += 1
  179.                     ContinueLoop
  180.                 EndIf
  181.             Else
  182.                 $tmptype = ''
  183.                 $typeFolder = 'файлы без расширения'
  184.             EndIf
  185.  
  186.             If $size0 And FileGetSize($FileList[$i] & $FileList[$i + 1]) <= $size0 Then ; удалить если менее указанного размера
  187.                 If $checkAtrb0 = 1 Then FileSetAttrib($FileList[$i] & $FileList[$i + 1], "-RASHT")
  188.                 If FileDelete($FileList[$i] & $FileList[$i + 1]) Then
  189.                     $del0 += 1
  190.                 Else
  191.                     $ign0 += 1
  192.                 EndIf
  193.                 ContinueLoop
  194.             EndIf
  195.             ; если файл существует в новом каталоге, то переименовать его перед перемещением, иначе переместить
  196.             $Path = $OutPath0 & '\' & $typeFolder & '\' & $FileList[$i + 1]
  197.             If FileExists($Path) Then
  198.                 If $tmptype Then
  199.                     $Name = StringLeft($FileList[$i + 1], StringInStr($FileList[$i + 1], '.', 0, -1) - 1) ; получить имя файла без расширения
  200.                     $tmptype = '.' & $tmptype
  201.                 Else
  202.                     $Name = $FileList[$i + 1]
  203.                 EndIf
  204.                 ; цикл проверки одноимённых файлов
  205.                 $j = 0
  206.                 Do
  207.                     $j += 1
  208.                     If $j = 1 Then
  209.                         $Path = $OutPath0 & '\' & $typeFolder & '\' & $Name & ' Копия' & $tmptype
  210.                     Else
  211.                         $Path = $OutPath0 & '\' & $typeFolder & '\' & $Name & ' Копия (' & $j & ')' & $tmptype
  212.                     EndIf
  213.                 Until Not FileExists($Path)
  214.             EndIf
  215.             If $checkAtrb0 = 1 Then FileSetAttrib($FileList[$i] & $FileList[$i + 1], "-RASHT")
  216.             If FileMove($FileList[$i] & $FileList[$i + 1], $Path, 8) Then ; перемещаем если одноимённого не существует
  217.                 $move0 += 1
  218.             Else
  219.                 $ign0 += 1
  220.             EndIf
  221.         Next
  222.     EndIf
  223.     GUICtrlSetData($StatusBar, 'Выполнено...  Из ' & $ArrSz / 2 & ' файлов' & @CRLF & 'перемещено: ' & $move0 & ',  удалено: ' & $del0 & ',  игнорир: ' & $ign0)
  224. EndFunc   ;==>_Sort
  225.  
  226. Func _Dropped()
  227.     Switch @GUI_DropId
  228.         Case $inpPath
  229.             GUICtrlSetData($inpPath, @GUI_DragFile)
  230.             GUICtrlSetData($OutPath, StringRegExpReplace(@GUI_DragFile, "(^.*)\\(.*)$", '\1') & '\root')
  231.         Case $OutPath
  232.             GUICtrlSetData($OutPath, @GUI_DragFile)
  233.     EndSwitch
  234. EndFunc   ;==>_Dropped
  235.  
  236. Func _Exit()
  237.     Exit
  238. EndFunc   ;==>_Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement