DoubleMcLovin Posted January 8, 2011 Share Posted January 8, 2011 (edited) The problem I am having is that periodically this script somehow "locks" a directory and I can't do anything with it. The locked directory ends up looping the script. The _MoveTarget() Function is the only place that moves the file, but the _RenameTarget() function also "moves" the file. expandcollapse popupOpt('MustDeclareVars', 1) #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #Auto Unpacker#=============================================================================================================== ; Name...........: Auto Unpacker ; Description ...: Searches set directory and unpacks files automatically ; Syntax ........: ; Parameters ....: ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: The change log readout has several characters before text. They are explained below: ; ...............: @ = Indicates a soft warning. Eg. An error that has already handled itself ; ...............: ! = Indicates an error that could not be automatically solved. ; ...............: # = Indicates a notice for a file displacement ; ...............: ^ = Indicates a file deletion ; ...............: % = Indicates a general notice ; =============================================================================================================================== ;watch dirs delineated by a "|" Local $wDir = "c:\system66\downloads\done\" Local $wDirs = StringSplit($wDir,"|") ;This is a list of words that can seperate "parts" of a files, or segments of files Local $arc_type = "rar|zip|tar" Local $arc_types = StringSplit($arc_type,"|") ;an array of different archive types Local $movie_dir = "c:\system66\media\movies\" Local $os_dir = "c:\system66\useful\os\" Local $game_dir = "c:\system66\media\games\" Local $series_dir = "c:\system66\media\series\" Local $other_dir = "c:\system66\downloads\extracted" ;extract dir for unkown files Local $delay = 5000 ;Delay time in milliseconds If FileExists (@ScriptDir & "\logs\changes.log") Then FileDelete (@ScriptDir & "\logs\changes.log") Local $log = FileOpen (@ScriptDir & "\logs\changes.log",41) Local $target, $files, $file_dir, $target_tmp, $error, $root, $fAttrib, $rar_part, $rar_parts, $category, $line_num, $file Global $subfile, $subfiles, $exit, $move_target Local $fType ;Used to indicate filetype. 1 = archive. 2 = directory. 3 = loose file. Global $time = @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " || " Global $footer = @TAB & "LINE: " ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: Main ; Description ...: ; Author ........: DoubleMcLovin ; Remarks .......: ; =============================================================================================================================== While 1 For $i1 = 1 to $wDirs[0] ;Cycles through watched dirs FileSetAttrib ($wDirs[$i1] & "\*","-R",1) ;Removes the Read-Only attribute. $error = FileChangeDir ($wDirs[$i1]) If $error = 0 Then ;failed to change Dir If FileExists ($wDirs[$i1]) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & $wDirs[$i1] & '". Unknown reason, exiting.' & $footer & @ScriptLineNumber & @CRLF) Exit Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & $wDirs[$i1] & '". Directory does not exist. Exiting.' & $footer & @ScriptLineNumber & @CRLF) Exit EndIf EndIf $files = FileFindFirstFile ("*.*") If @error Then ;empty dir FileWrite ($log,$time & '@ "' & $wDirs[$i1] & '" is empty, sleeping for ' & $delay & ' milliseconds.' & $footer & @ScriptLineNumber & @CRLF) Sleep ($delay) ElseIf $files = -1 Then ;Error in search string FileWrite ($log,$time & '! Script encountered an error, closing.' & $footer & @ScriptLineNumber & @CRLF) Else While 1 $exit = 0 ; Resets exit code $fType = 0 ; Resets file type $target = FileFindNextFile ($files) ;Cycles through files in watched dirs If @error Then ExitLoop If @extended = 1 Then $fType = 2 ;directory check ConsoleWrite ("Changed $target to " & $target & @CRLF) $error = FileExists (@WorkingDir & '\' & $target) If $error = 1 And $fType = 0 Then ;exists and is not a directory _GetType ($target) ElseIf $error = 0 Then ;DNE $fType = 0 ElseIf $fType = 2 Then ;directory Else ;bizzarre unexplainable error FileWrite ($log,$time & '! Script encountered an error, closing. Current dir\file: "' & @WorkingDir & '\' & $target & '".' & $footer & @ScriptLineNumber & @CRLF) Exit EndIf Switch $fType Case 0 ;File does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $target & '" does not exist.' & $footer & @ScriptLineNumber & @CRLF) Case 1 ;Archive _RenameTarget ($target, 1, @ScriptLineNumber) ConsoleWrite ("target - " & $target & ". move_target - " & $move_target & @CRLF) _Extract ($wDirs[$i1],$target, @ScriptLineNumber) Case 2 ;Directory _RenameTarget ($target, 2, @ScriptLineNumber) ConsoleWrite ("target - " & $target & ". move_target - " & $move_target & @CRLF) _ExploreDir ($target, @ScriptLineNumber) _GetCategory ($target, @ScriptLineNumber) Case 3 ;File _RenameTarget ($target, 1, @ScriptLineNumber) ConsoleWrite ("target - " & $target & ". move_target - " & $move_target & @CRLF) _PutFile ($target, @ScriptLineNumber) EndSwitch ;~ If $exit = 1 Then ExitLoop WEnd EndIf FileClose ($files) FileClose ($subfiles) ConsoleWrite ("Sleeping" & @CRLF) ;~ Exit Sleep ($delay) Next WEnd ; #Main ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _ExploreDir ; Description ...: Opens directories to interact with files therein ; Syntax ........: _ExploreDir ($file) ; Parameters ....: $file - Initial directory working in ; ...............: $line_num - Line number from where this func was called ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _ExploreDir ($file, $line_num) $root = @WorkingDir $error = FileChangeDir ($file) If $error = 0 Then ;failed to change Dir If FileExists ($file) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Unknown reason, flagging exit code.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) ConsoleWrite ('! Exit code' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) $exit = 1 Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Directory does not exist.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Return EndIf EndIf $subfiles = FileFindFirstFile ("*") If @error Then ;If the dir is empty FileChangeDir ($root) _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir is empty.') $file = "" ElseIf $subfiles = -1 Then ;error matcing FileWrite ($log,$time & '@ Error, no files matched the search string.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Return Else While 1 $fType = 0 ;reset fType $subfile = FileFindNextFile ($subfiles) If @error Then ExitLoop If @extended = 1 Then $fType = 2 ;directory check $error = FileExists (@WorkingDir & '\' & $subfile) If $error = 1 And $fType = 0 Then ;exists _GetType ($subfile) ElseIf $error = 0 Then ;DNE $fType = 0 ElseIf $fType = 2 Then Else FileWrite ($log,$time & '! Script encountered an error, closing. Current dir\file: "' & @WorkingDir & '\' & $subfile & '".' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Exit EndIf Switch $fType Case 0 ;File does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $subfile & '" does not exist.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Case 1 ;Archive _Extract (@WorkingDir,$subfile, @ScriptLineNumber) Case 2 ;Directory _UnloadDir ($subfile, @ScriptLineNumber) Case 3 ;File _RenameTarget ($subfile, 1, @ScriptLineNumber) ConsoleWrite ("subfile - " & $subfile & ". move_target - " & $move_target & @CRLF) EndSwitch WEnd EndIf FileClose ($subfiles) $error = FileChangeDir ($root) If $error = 0 Then ;failed to change Dir If FileExists ($file) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Unknown reason.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) $file = "" Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Directory does not exist.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Return EndIf EndIf Return EndFunc ; # _ExploreDir ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _MoveTarget ; Description ...: Moves a file or folder ; Syntax ........: _MoveTarget (ByRef $move_target, $destination, $type, $line_num) ; Parameters ....: $move_target - File/Dir to be moved ($target) ; ...............: $destination - Where the file/folder is being moved ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; Return values .: $move_target - For $target association ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _MoveTarget (ByRef $move_target, $destination, $type, $line_num) Switch $type Case 1 ;File If FileExists ($move_target) Then $error = FileMove (@WorkingDir & '\' & $move_target,@WorkingDir & '\' & $destination,9) If $error = 1 Then ;success FileWrite ($log,$time & '# File "' & @WorkingDir & '\' & $move_target & '" was moved to "' & @WorkingDir & '\' & $destination & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $move_target & '" cannot be moved to "' & @WorkingDir & '\' & $destination & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else ;If the file trying to be moved does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $move_target & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $move_target = "" ;clears the target EndIf Case 2 ;Directory If FileExists ($move_target) Then $error = DirMove (@WorkingDir & '\' & $move_target,@WorkingDir & '\' & $destination,1) If $error = 1 Then ;success FileWrite ($log,$time & '# Directory "' & @WorkingDir & '\' & $move_target & '" was moved to "' & @WorkingDir & '\' & $destination & '". ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $move_target & '" cannot be moved to "' & @WorkingDir & '\' & $destination & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else ;If the directory trying to be moved does not exist FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $move_target & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $move_target = "" ;clears the target EndIf EndSwitch ConsoleWrite ('move_target originates as (' & @ScriptLineNumber & '): ' & $move_target & @CRLF) $move_target = $destination ;sets the new name on the file for further work Return EndFunc ; # _MoveTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _RenameTarget ; Description ...: Renames file to remove bad syntax and put all files into a standard format ; Syntax ........: _RenameTarget (ByRef $file, $type, $line_num) ; Parameters ....: $file - Dir/File being renamed ($target) ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; Return values .: $file - New name for $target ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _RenameTarget (ByRef $file, $type, $line_num) Switch $type Case 1 ;File $move_target = StringReplace (StringReplace (StringRegExpReplace (StringRegExpReplace ($file," ","."),"[-()_]","."),"..","."),"..",".") ConsoleWrite ('move_target originates as (' & @ScriptLineNumber & '): ' & $move_target & @CRLF) If StringRight ($move_target,1) = "." Then $move_target = StringTrimRight($move_target,1) If FileExists (@WorkingDir & '\' & $move_target) Then If Not $move_target = $file Then FileWrite ($log,$time & '@ File already exists: "' & @WorkingDir & '\' & $move_target & '". Overwriting.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) EndIf EndIf ConsoleWrite ("File pre " & $file & " " & @ScriptLineNumber & @CRLF) _MoveTarget ($file, $move_target, 1, @ScriptLineNumber) ConsoleWrite ("File post " & $file & " " & @ScriptLineNumber & @CRLF) Case 2 ;Dir $move_target = StringReplace (StringReplace (StringRegExpReplace (StringRegExpReplace ($file," ","."),"[-()_]","."),"..","."),"..",".",1) ConsoleWrite ('move_target originates as (' & @ScriptLineNumber & '): ' & $move_target & ". From " & $file & @CRLF) If StringRight ($move_target,1) = "." Then $move_target = StringTrimRight($move_target,1) If FileExists (@WorkingDir & '\' & $move_target) Then If FileGetSize (@WorkingDir & '\' & $file) <= FileGetSize (@WorkingDir & '\' & $move_target) Then ;If the newer is smaller than the older If Not $move_target = $file Then FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $file & '" flagged for deletion. Directory "' & @WorkingDir & '\' & $move_target & '" already exists. ' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $file, @ScriptLineNumber, "Copy in destination is larger than original.") EndIf ElseIf FileGetSize (@WorkingDir & '\' & $file) > FileGetSize (@WorkingDir & '\' & $move_target) Then FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $move_target & '" flagged for deletion. Directory "' & @WorkingDir & '\' & $file & '" is larger. ' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $move_target, @ScriptLineNumber, "Copy in destination is smaller than original.") Return EndIf EndIf ConsoleWrite ("File pre " & $file & " " & @ScriptLineNumber & @CRLF) _MoveTarget ($file, $move_target, 2, @ScriptLineNumber) ConsoleWrite ("File post " & $file & " " & @ScriptLineNumber & @CRLF) EndSwitch ConsoleWrite ('move_target ends as (' & @ScriptLineNumber & '): ' & $move_target & @CRLF) $file = $move_target ;Associates $target to new name Return EndFunc ; # _RenameTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _RemoveTarget ; Description ...: Deletes directory with error check ; Syntax ........: _RemoveTarget($type, $file, $line_num, $reason) ; Parameters ....: $dir - Directory to be removed ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; ...............: $reason - A text string by user input for use in the log ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Removes recursively ; =============================================================================================================================== Func _RemoveTarget ($type, $file, $line_num, $reason) Switch $type Case 1 ;File $error = FileDelete ($file) If $error = 1 Then ;success FileWrite ($log,$time & '^ File "' & @WorkingDir & '\' & $file & '" was deleted. ' & $reason & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! File "' & @WorkingDir & '\' & $file & '" cannot be deleted. Unknown reason. ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf Case 2 ;Dir $error = DirRemove ($file,1) If $error = 1 Then ;success FileWrite ($log,$time & '^ Directory "' & @WorkingDir & '\' & $file & '" was deleted. ' & $reason & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be deleted. Unknown reason.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf EndSwitch Return EndFunc ; # _RemoveTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _PutFile ; Description ...: Organizes loose files ; Syntax ........: _PutFile($file, $line_num) ; Parameters ....: $file - File to be extracted ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: This function attempts to place loose files in the download directory into a directory named after themselves. ; If two or more files have similar names, they will be grouped in the same folder. Similar name is determined by removing all ; numeric characters from the name (excluding extension) for analysis. ; =============================================================================================================================== Func _PutFile ($file, $line_num) Switch StringRegExp ($file,"(?i).*\..{1,4}",0) ;performs a file check by looking for a decimal and 1-4 characters after that (extenstion check) Case 1 ;If it appears to be a file ;Start by stripping name of extension and numbers $target_tmp = StringRegExpReplace (StringReplace ($file,StringRight ($file,4),""),"\d","") If StringRight ($target_tmp,1) = "." Then StringTrimRight ($target_tmp,1) ;in case of 4 character extension, this will remove the trailing decimal FileMove ($file,@WorkingDir & '\' & $target_tmp & '\',9) FileWrite ($log,$time & '# The file "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $target_tmp & '". ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Case 2 ;Does not appear to be a file EndSwitch Return EndFunc ; # _PutFile ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _GetType ; Description ...: Gets the file type for future operations ; Syntax ........: _GetType ($file) ; Parameters ....: $file - File being analyzed ; ...............: $fType - The handle for file type variable ; Return values .: $fType - A number corresponding to type of file, directory, or archive. ; ...............: | 1 Archive ; ...............: | 2 Directory ; ...............: | 3 File ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _GetType ($file) $fAttrib = FileGetAttrib ($file) Switch StringRegExp ($file,"(?i).*(\.part\d{1,}\.rar|\.(\d{3}|r\d{2}))\b") ;Didn't feel like making this an if Case 1 Switch StringRegExp ($file,"(?i).*(\.part(0{1,}1|1)\.rar\b") Case 1 $fType = 1 ;If it is the first part of the archive, deal with it. Case Else $fType = 4 ;If we are dealing with a part of an archive, don't touch it EndSwitch EndSwitch Switch StringRegExp ($file,"(?i).*\.((r|t)ar|zip)",0) ;Checks to see if file is an archive Case 1 $fType = 1 ;Archive Case Else $fType = 3 ;File EndSwitch Return EndFunc ; # _GetType ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _Extract ; Description ...: Extracts specified file ; Syntax ........: _Extract($fDir, $file) ; Parameters ....: $fDir - Directory working in ; ...............: $file - File to be extracted ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: This function simply extracts the directed file into the done directory ; =============================================================================================================================== Func _Extract ($fDir, $file, $line_num) Local $extract_code = RunWait ('"' & @ProgramFilesDir & '\winrar\winrar.exe" x "' & @WorkingDir & '\' & $file & '" "' & $fDir & '"') Switch $extract_code Case 0 ;extracted successfully FileWrite ($log,$time & '# Extracted archive "' & @WorkingDir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) _RemoveTarget (1, $file, @ScriptLineNumber, 'Successful extraction - deleting archive.') If StringRegExp ($file,"(?i).*\.(rar|zip)\b",0) = 1 Then ;if we have a zip or rar file check for parts and delete them too $rar_parts = FileFindFirstFile ("*") While 1 $rar_part = FileFindNextFile ($rar_parts) If @error Then ExitLoop ;out of files Switch StringRegExp ($rar_part,"(?i).*(part\d{1,}\.rar|\.(\d{3}|r\d{2}))\b",0) ;Checks to see if each file matches the parts filter Case 1 Local $rar_part_tmp = StringRegExpReplace ($rar_part,"(?i)(part\d{1,}\.(rar|zip)|\.(\d{3}|(z|r)\d{2}))\b","") Local $target_tmp = StringRegExpReplace ($file,"(?i)(part\d{1,}\.(rar|zip)|\.(\d{3}|(z|r)r\d{2}))\b","") If $rar_part_tmp = $target_tmp Then _RemoveTarget (1, $rar_part, @ScriptLineNumber, 'File is a part of an extracted archive.') EndSwitch WEnd EndIf Case 1 FileWrite ($log,$time & "! Failed to extract archive " & @WorkingDir & '\' & $file & '.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Case Else FileWrite ($log,$time & "@ Unknown result for extract of " & @WorkingDir & '\' & $file & '. Extract code = "' & $extract_code & '"' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndSwitch Return EndFunc ; # _Extract ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _UnloadDir ; Description ...: Pulls files out of a subdir and places them in current dir ; Syntax ........: _UnloadDir ($sub) ; Parameters ....: $sub - subdir to unload ; ...............: $line_num - line number that called this func ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Should never be called in watch dir ; =============================================================================================================================== Func _UnloadDir ($file, $line_num) If StringInStr ($file,"sub") Then ;ignore subtitles directories FileWrite ($log,$time & '% Subtitles dir located at "' & @WorkingDir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) ElseIf StringInStr ($file,"sample") Then _RemoveTarget (2, $file, @ScriptLineNumber, 'Sample dir.') Else ;Takes all files/folders from dir and moves them to dir's root FileMove (@WorkingDir & '\' & $file & "\*.*",@WorkingDir & '\' & $file,9) DirMove (@WorkingDir & '\' & $file & "\*",@WorkingDir & '\' & $file,1) FileWrite ($log,$time & '# Moved contents of "' & @WorkingDir & '\' & $file & '" to "' & @WorkingDir & '".' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir unloaded.') EndIf Return EndFunc ; # _UnloadDir ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _GetCategory ; Description ...: Determines category of folder for classification ; Syntax ........: _GetCategory ($file, $line_num) ; Parameters ....: $file - Directory being reviewed ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Organizes according to $category ; ...............: - 1 movie ; ...............: - 2 game ; ...............: - 4 OS ; ...............: - 5 other ; ...............: - 6 series ; =============================================================================================================================== Func _GetCategory (ByRef $file, $line_num) $category = 0 If StringRegExp ($file,"(?i).*(s\d{1,2}e\d{1,2}.*|series\d{1,2}).*",0) Then ;Series filter $category = 6 ElseIF StringRegExp ($file,"(?i).*\.\d{4}\.((dvd|br|bluray)rip|r5)\..*",0) Then ;Movie filter $category = 1 Else $root = @WorkingDir $target_tmp = $file $error = FileChangeDir ($file) If $error = 0 Then ;failed to change Dir If FileExists ($file) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 $file = "" Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Directory does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Return EndIf EndIf $subfiles = FileFindFirstFile ("*") If @error Then ;If the dir is empty FileChangeDir ($root) _RemoveTarget (2, $file, @ScriptLineNumber, 'Empty dir.') ElseIf $subfiles = -1 Then ;error matcing FileWrite ($log,$time & '@ Error, no files matched the search string.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else While 1 $subfile = FileFindNextFile ($subfiles) If @error Then ExitLoop If StringRight ($subfile,3) = "iso" Then ;iso check If FileGetSize ($subfile) >= 700000000 Then ;700MB If StringRegExp ($subfile,"(?i).*(microsoft|adobe|OS(X|.+X))",0) Then $category = 4 Else $category = 2 ;size must be > 700MB, and not include major software names, must be .iso EndIf EndIf EndIf WEnd EndIf FileClose ($subfiles) EndIf If $category = 0 Then $category = 5 Switch $category Case 1 $file_dir = $movie_dir Case 2 $file_dir = $game_dir Case 3 $file_dir = $more_dir Case 4 $file_dir = $os_dir Case 5 $file_dir = $other_dir Case 6 $file_dir = $series_dir EndSwitch $file = $target_tmp $error = FileChangeDir ($root) If $error = 0 Then ;failed to change Dir If FileExists ($root) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & $root & '". Unknown reason, exiting.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Exit Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & $root & '". Directory does not exist, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 Return EndIf EndIf If FileExists ($file_dir & '\' & $file) Then If FileGetSize ($file) <= FileGetSize ($file_dir & '\' & $file) Then _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir is smaller than, or equal to, duplicate in destination directory.') Return Else _RemoveTarget (2, $file_dir & '\' & $file, @ScriptLineNumber, 'Dir is smaller than, or equal to, duplicate to be moved.') EndIf EndIf $error = DirMove (@workingdir & '\' & $file,$file_dir & '\',1) If $error = 0 Then If FileExists ($file_dir & '\' & $file) Then FileWrite ($log,$time & '@ Warning moving dir "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '". Directory exists, unable to overwrite.flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 Else FileWrite ($log,$time & '! Error moving dir "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '". Unknown Reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else FileWrite ($log,$time & '# Moved directory "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf Return EndFunc ; # _GetCategory Edited January 8, 2011 by DoubleMcLovin Link to comment Share on other sites More sharing options...
PsaltyDS Posted January 8, 2011 Share Posted January 8, 2011 You seem to have some confusion about variable scopes. At the top of your script you have both Local and Global variable declarations. That doesn't work. Every variable declared outside a function that way will be Global even if you put "Local" in front of it. The $move_target variable is declared with Global. Then, in the _MoveTarget() function declaration, you declare a Local ByRef variable with the same name as $move_target. Also in _RenameTarget() you have cases that set the value of $move_target without declaring it, so that will be the Global. Inside _RenameTarget() is at least one instance of this line: _MoveTarget($file, $move_target, 2, @ScriptLineNumber) Note how you use $move_target as the second parameter, and $file is the first parameter. But looking at the declaration for _MoveTarget(): Func _MoveTarget(ByRef $move_target, $destination, $type, $line_num) So this function receives $file ByRef as the local $move_target, and it receives the global $move_target as the local $destination. With an arrangement that confused, it would be amazing for any of it to work. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
DoubleMcLovin Posted January 8, 2011 Author Share Posted January 8, 2011 (edited) I restyled the way that and a few other functions work to remove that. I am still having the script get stuck on a single directory and just kinda loop over and over on a locked directory. expandcollapse popupOpt('MustDeclareVars', 1) #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #Auto Unpacker#=============================================================================================================== ; Name...........: Auto Unpacker ; Description ...: Searches set directory and unpacks files automatically ; Syntax ........: ; Parameters ....: ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: The change log readout has several characters before text. They are explained below: ; ...............: @ = Indicates a soft warning. Eg. An error that has already handled itself ; ...............: ! = Indicates an error that could not be automatically solved. ; ...............: # = Indicates a notice for a file displacement ; ...............: ^ = Indicates a file deletion ; ...............: % = Indicates a general notice ; =============================================================================================================================== ;watch dirs delineated by a "|" Local $wDir = "c:\system66\downloads\done\" Local $wDirs = StringSplit($wDir,"|") ;This is a list of words that can seperate "parts" of a files, or segments of files Local $arc_type = "rar|zip|tar" Local $arc_types = StringSplit($arc_type,"|") ;an array of different archive types Local $movie_dir = "c:\system66\media\movies\" Local $os_dir = "c:\system66\useful\os\" Local $game_dir = "c:\system66\media\games\" Local $series_dir = "c:\system66\media\series\" Local $other_dir = "c:\system66\downloads\extracted" ;extract dir for unkown files Local $delay = 500 ;Delay time in milliseconds If FileExists (@ScriptDir & "\logs\changes.log") Then FileDelete (@ScriptDir & "\logs\changes.log") Local $log = FileOpen (@ScriptDir & "\logs\changes.log",41) Local $target, $files, $file_dir, $target_tmp, $error, $root, $fAttrib, $rar_part, $rar_parts, $category, $line_num, $fDir, $file, $subfile, $subfiles, $exit, $move_target, $fType Global $time = @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " || " Global $footer = @TAB & "LINE: " ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: Main ; Description ...: ; Author ........: DoubleMcLovin ; Remarks .......: ; =============================================================================================================================== While 1 For $i1 = 1 to $wDirs[0] ;Cycles through watched dirs FileSetAttrib ($wDirs[$i1] & "\*","-R",1) ;Removes the Read-Only attribute. $error = FileChangeDir ($wDirs[$i1]) If $error = 0 Then ;failed to change Dir If FileExists ($wDirs[$i1]) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & $wDirs[$i1] & '". Unknown reason, exiting.' & $footer & @ScriptLineNumber & @CRLF) Exit Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & $wDirs[$i1] & '". Directory does not exist. Exiting.' & $footer & @ScriptLineNumber & @CRLF) Exit EndIf EndIf $files = FileFindFirstFile ("*.*") If @error Then ;empty dir FileWrite ($log,$time & '@ "' & $wDirs[$i1] & '" is empty, sleeping for ' & $delay & ' milliseconds.' & $footer & @ScriptLineNumber & @CRLF) Sleep ($delay) ElseIf $files = -1 Then ;Error in search string FileWrite ($log,$time & '! Script encountered an error, closing.' & $footer & @ScriptLineNumber & @CRLF) Else While 1 $exit = 0 ; Resets exit code $fType = 0 ; Resets file type $target = FileFindNextFile ($files) ;Cycles through files in watched dirs If @error Then ExitLoop If @extended = 1 Then $fType = 2 ;directory check $error = FileExists (@WorkingDir & '\' & $target) ;Determine type for $fType If $error = 1 And $fType = 0 Then ;exists and is not a directory _GetType ($target) ElseIf $error = 0 Then ;DNE $fType = 0 ElseIf $fType = 2 Then ;directory Else ;bizzarre unexplainable error FileWrite ($log,$time & '! Script encountered an error, closing. Current dir\file: "' & @WorkingDir & '\' & $target & '".' & $footer & @ScriptLineNumber & @CRLF) Exit EndIf ;Perform actions based on $fType (archive, directory, file) Switch $fType Case 0 ;File does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $target & '" does not exist.' & $footer & @ScriptLineNumber & @CRLF) Case 1 ;Archive _RenameTarget ($target, 1, @ScriptLineNumber) _Extract ($wDirs[$i1],$target, @ScriptLineNumber) Case 2 ;Directory _RenameTarget ($target, 2, @ScriptLineNumber) _ExploreDir ($target, @ScriptLineNumber) _GetCategory ($target, @ScriptLineNumber) Case 3 ;File _RenameTarget ($target, 1, @ScriptLineNumber) _PutFile ($target, @ScriptLineNumber) EndSwitch ;~ If $exit = 1 Then ExitLoop WEnd EndIf FileClose ($files) FileClose ($subfiles) ;~ Exit Sleep ($delay) Next WEnd ; #Main ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _ExploreDir ; Description ...: Opens directories to interact with files therein ; Syntax ........: _ExploreDir ($fDir) ; Parameters ....: $fDir - Initial directory working in ; ...............: $line_num - Line number from where this func was called ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _ExploreDir ($fDir, $line_num) If $exit = 1 Then Return $root = @WorkingDir $error = FileChangeDir ($fDir) If $error = 0 Then ;failed to change Dir If FileExists ($fDir) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Unknown reason, flagging exit code.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) $exit = 1 Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Directory does not exist.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Return EndIf EndIf $subfiles = FileFindFirstFile ("*") If @error Then ;If the dir is empty FileChangeDir ($root) _RemoveTarget (2, $fDir, @ScriptLineNumber, 'Dir is empty.') $fDir = "" ElseIf $subfiles = -1 Then ;error matcing FileWrite ($log,$time & '@ Error, no files matched the search string.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Return Else While 1 $fType = 0 ;reset fType $subfile = FileFindNextFile ($subfiles) If @error Then ExitLoop If @extended = 1 Then $fType = 2 ;directory check $error = FileExists (@WorkingDir & '\' & $subfile) If $error = 1 And $fType = 0 Then ;exists _GetType ($subfile) ElseIf $error = 0 Then ;DNE $fType = 0 ElseIf $fType = 2 Then Else FileWrite ($log,$time & '! Script encountered an error, closing. Current dir\file: "' & @WorkingDir & '\' & $subfile & '".' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Exit EndIf Switch $fType Case 0 ;File does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $subfile & '" does not exist.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Case 1 ;Archive _Extract (@WorkingDir,$subfile, @ScriptLineNumber) $subfiles = FileFindFirstFile ('*.*') ;Reloads files to prevent loading removed archive parts Case 2 ;Directory _UnloadDir ($subfile, @ScriptLineNumber) Case 3 ;File _RenameTarget ($subfile, 1, @ScriptLineNumber) EndSwitch WEnd EndIf FileClose ($subfiles) $error = FileChangeDir ($root) If $error = 0 Then ;failed to change Dir If FileExists ($fDir) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Unknown reason.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) $fDir = "" Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Directory does not exist.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) Return EndIf EndIf Return EndFunc ; # _ExploreDir ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _MoveTarget ; Description ...: Moves a file or folder ; Syntax ........: _MoveTarget (ByRef $file, $destination, $type, $line_num) ; Parameters ....: $file - File/Dir to be moved ($target) ; ...............: $destination - Where the file/folder is being moved ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; Return values .: $file - For $target association ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _MoveTarget (ByRef $file, $destination, $type, $line_num) If $exit = 1 Then Return Switch $type Case 1 ;File If FileExists ($file) Then $error = FileMove (@WorkingDir & '\' & $file,@WorkingDir & '\' & $destination,9) If $error = 1 Then ;success FileWrite ($log,$time & '# File "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $destination & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be moved to "' & @WorkingDir & '\' & $destination & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else ;If the file trying to be moved does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $file & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $file = "" ;clears the target EndIf Case 2 ;Directory If FileExists ($file) Then $error = DirMove (@WorkingDir & '\' & $file,@WorkingDir & '\' & $destination,1) If $error = 1 Then ;success FileWrite ($log,$time & '# Directory "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $destination & '". ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be moved to "' & @WorkingDir & '\' & $destination & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else ;If the directory trying to be moved does not exist FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $file & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $file = "" ;clears the target EndIf EndSwitch $file = $destination ;sets the new name on the file for further work Return EndFunc ; # _MoveTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _RenameTarget ; Description ...: Renames file to remove bad syntax and put all files into a standard format ; Syntax ........: _RenameTarget (ByRef $file, $type, $line_num) ; Parameters ....: $file - Dir/File being renamed ($target) ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; Return values .: $file - New name for $target ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _RenameTarget (ByRef $file, $type, $line_num) If $exit = 1 Then Return Switch $type Case 1 ;File $move_target = StringReplace (StringReplace (StringRegExpReplace (StringRegExpReplace ($file," ","."),"[-()_]","."),"..","."),"..",".") If StringRight ($move_target,1) = "." Then $move_target = StringTrimRight($move_target,1) If FileExists (@WorkingDir & '\' & $move_target) Then If Not $move_target = $file Then FileWrite ($log,$time & '% File already exists: "' & @WorkingDir & '\' & $move_target & '". Overwriting.' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) EndIf EndIf _MoveTarget ($file, $move_target, 1, @ScriptLineNumber) If $exit = 1 Then Return Case 2 ;Dir $move_target = StringReplace (StringReplace (StringRegExpReplace (StringRegExpReplace ($file," ","."),"[-()_]","."),"..","."),"..",".",1) If StringRight ($move_target,1) = "." Then $move_target = StringTrimRight($move_target,1) If FileExists (@WorkingDir & '\' & $move_target) Then If FileGetSize (@WorkingDir & '\' & $file) <= FileGetSize (@WorkingDir & '\' & $move_target) Then ;If the newer is smaller than the older If Not $move_target = $file Then FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $file & '" flagged for deletion. Directory "' & @WorkingDir & '\' & $move_target & '" already exists. ' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $file, @ScriptLineNumber, "Copy in destination is larger than original.") EndIf ElseIf FileGetSize (@WorkingDir & '\' & $file) > FileGetSize (@WorkingDir & '\' & $move_target) Then FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $move_target & '" flagged for deletion. Directory "' & @WorkingDir & '\' & $file & '" is larger. ' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $move_target, @ScriptLineNumber, "Copy in destination is smaller than original.") Return EndIf EndIf _MoveTarget ($file, $move_target, 2, @ScriptLineNumber) EndSwitch $file = $move_target ;Associates $target to new name Return EndFunc ; # _RenameTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _RemoveTarget ; Description ...: Deletes directory with error check ; Syntax ........: _RemoveTarget($type, $file, $line_num, $reason) ; Parameters ....: $dir - Directory to be removed ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; ...............: $reason - A text string by user input for use in the log ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Removes recursively ; =============================================================================================================================== Func _RemoveTarget ($type, $file, $line_num, $reason) If $exit = 1 Then Return Switch $type Case 1 ;File $error = FileDelete ($file) If $error = 1 Then ;success FileWrite ($log,$time & '^ File "' & @WorkingDir & '\' & $file & '" was deleted. ' & $reason & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! File "' & @WorkingDir & '\' & $file & '" cannot be deleted. Unknown reason. ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf Case 2 ;Dir $error = DirRemove ($file,1) If $error = 1 Then ;success FileWrite ($log,$time & '^ Directory "' & @WorkingDir & '\' & $file & '" was deleted. ' & $reason & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be deleted. Unknown reason.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf EndSwitch Return EndFunc ; # _RemoveTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _PutFile ; Description ...: Organizes loose files ; Syntax ........: _PutFile($file, $line_num) ; Parameters ....: $file - File to be extracted ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: This function attempts to place loose files in the download directory into a directory named after themselves. ; If two or more files have similar names, they will be grouped in the same folder. Similar name is determined by removing all ; numeric characters from the name (excluding extension) for analysis. ; =============================================================================================================================== Func _PutFile ($file, $line_num) If $exit = 1 Then Return Switch StringRegExp ($file,"(?i).*\..{1,4}\b",0) ;performs a file check by looking for a decimal and 1-4 characters after that but only at the end (extenstion check) Case 1 ;If it appears to be a file ;Start by stripping name of extension and numbers $target_tmp = StringRegExpReplace (StringReplace ($file,StringRight ($file,4),""),"\d","") If StringRight ($target_tmp,1) = "." Then StringTrimRight ($target_tmp,1) ;in case of 4 character extension, this will remove the trailing decimal FileMove ($file,@WorkingDir & '\' & $target_tmp & '\',9) FileWrite ($log,$time & '# The file "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $target_tmp & '". ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Case 2 ;Does not appear to be a file EndSwitch Return EndFunc ; # _PutFile ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _GetType ; Description ...: Gets the file type for future operations ; Syntax ........: _GetType ($file) ; Parameters ....: $file - File being analyzed ; ...............: $fType - The handle for file type variable ; Return values .: $fType - A number corresponding to type of file, directory, or archive. ; ...............: | 1 Archive ; ...............: | 2 Directory ; ...............: | 3 File ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _GetType ($file) If $exit = 1 Then Return $fAttrib = FileGetAttrib ($file) Switch StringRegExp ($file,"(?i).*(\.part\d{1,}\.rar|\.(\d{3}|r\d{2}))\b") ;Didn't feel like making this an if Case 1 Switch StringRegExp ($file,"(?i).*(\.part(0{1,}1|1)\.rar\b") Case 1 $fType = 1 ;If it is the first part of the archive, deal with it. Case Else $fType = 4 ;If we are dealing with a part of an archive, don't touch it EndSwitch EndSwitch Switch StringRegExp ($file,"(?i).*\.((r|t)ar|zip)",0) ;Checks to see if file is an archive Case 1 $fType = 1 ;Archive Case Else $fType = 3 ;File EndSwitch Return EndFunc ; # _GetType ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _Extract ; Description ...: Extracts specified file ; Syntax ........: _Extract($fDir, $file) ; Parameters ....: $fDir - Directory working in ; ...............: $file - File to be extracted ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: This function simply extracts the directed file into the done directory ; =============================================================================================================================== Func _Extract ($fDir, $file, $line_num) If $exit = 1 Then Return Local $extract_code = RunWait ('"' & @ProgramFilesDir & '\winrar\winrar.exe" x "' & @WorkingDir & '\' & $file & '" "' & $fDir & '"') Switch $extract_code Case 0 ;extracted successfully FileWrite ($log,$time & '# Extracted archive "' & @WorkingDir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) _RemoveTarget (1, $file, @ScriptLineNumber, 'Successful extraction - deleting archive.') If StringRegExp ($file,"(?i).*\.(rar|zip)\b",0) = 1 Then ;if we have a zip or rar file check for parts and delete them too $rar_parts = FileFindFirstFile ("*") While 1 $rar_part = FileFindNextFile ($rar_parts) If @error Then ExitLoop ;out of files Switch StringRegExp ($rar_part,"(?i).*(part\d{1,}\.rar|\.(\d{3}|r\d{2}))\b",0) ;Checks to see if each file matches the parts filter Case 1 Local $rar_part_tmp = StringRegExpReplace ($rar_part,"(?i)(part\d{1,}\.(rar|zip)|\.(\d{3}|(z|r)\d{2}))\b","") Local $target_tmp = StringRegExpReplace ($file,"(?i)(part\d{1,}\.(rar|zip)|\.(\d{3}|(z|r)r\d{2}))\b","") If $rar_part_tmp = $target_tmp Then _RemoveTarget (1, $rar_part, @ScriptLineNumber, 'File is a part of an extracted archive.') EndSwitch WEnd EndIf Case 1 FileWrite ($log,$time & "! Failed to extract archive " & @WorkingDir & '\' & $file & '.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Case Else FileWrite ($log,$time & "@ Unknown result for extract of " & @WorkingDir & '\' & $file & '. Extract code = "' & $extract_code & '"' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndSwitch Return EndFunc ; # _Extract ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _UnloadDir ; Description ...: Pulls files out of a subdir and places them in current dir ; Syntax ........: _UnloadDir ($sub) ; Parameters ....: $sub - subdir to unload ; ...............: $line_num - line number that called this func ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Should never be called in watch dir ; =============================================================================================================================== Func _UnloadDir ($file, $line_num) If $exit = 1 Then Return If StringInStr ($file,"sub") Then ;ignore subtitles directories FileWrite ($log,$time & '% Subtitles dir located at "' & @WorkingDir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) ElseIf StringInStr ($file,"sample") Then _RemoveTarget (2, $file, @ScriptLineNumber, 'Sample dir.') Else ;Takes all files/folders from dir and moves them to dir's root FileMove (@WorkingDir & '\' & $file & "\*.*",@WorkingDir & '\' & $file,9) DirMove (@WorkingDir & '\' & $file & "\*",@WorkingDir & '\' & $file,1) FileWrite ($log,$time & '# Moved contents of "' & @WorkingDir & '\' & $file & '" to "' & @WorkingDir & '".' & $footer & $line_num & ',' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir unloaded.') EndIf Return EndFunc ; # _UnloadDir ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _GetCategory ; Description ...: Determines category of folder for classification ; Syntax ........: _GetCategory ($file, $line_num) ; Parameters ....: $file - Directory being reviewed ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Organizes according to $category ; ...............: - 1 movie ; ...............: - 2 game ; ...............: - 3 porn ; ...............: - 4 OS ; ...............: - 5 other ; ...............: - 6 series ; =============================================================================================================================== Func _GetCategory (ByRef $file, $line_num) If $exit = 1 Then Return $category = 0 ;Reset $category If StringRegExp ($file,"(?i).*(s\d{1,2}e\d{1,2}.*|series\d{1,2}).*",0) Then ;Series filter $category = 6 ElseIF StringRegExp ($file,"(?i).*\.\d{4}\.((dvd|br|bluray)rip|r5)\..*",0) Then ;Movie filter $category = 1 Else ;general filter $root = @WorkingDir $target_tmp = $file $error = FileChangeDir ($file) If $error = 0 Then ;failed to change Dir If FileExists ($file) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 $file = "" Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Directory does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Return EndIf EndIf $subfiles = FileFindFirstFile ("*") If @error Then ;If the dir is empty FileChangeDir ($root) _RemoveTarget (2, $file, @ScriptLineNumber, 'Empty dir.') ElseIf $subfiles = -1 Then ;error matcing FileWrite ($log,$time & '@ Error, no files matched the search string.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else While 1 $subfile = FileFindNextFile ($subfiles) If @error Then ExitLoop If StringRight ($subfile,3) = "iso" Then ;iso check If FileGetSize ($subfile) >= 700000000 Then ;700MB If StringRegExp ($subfile,"(?i).*(microsoft|adobe|OS(X|.+X))",0) Then $category = 4 Else $category = 2 ;size must be > 700MB, and not include major software names, must be .iso EndIf EndIf EndIf WEnd EndIf FileClose ($subfiles) EndIf If $category = 0 Then $category = 5 Switch $category Case 1 $file_dir = $movie_dir Case 2 $file_dir = $game_dir Case 4 $file_dir = $os_dir Case 5 $file_dir = $other_dir Case 6 $file_dir = $series_dir EndSwitch $file = $target_tmp $error = FileChangeDir ($root) If $error = 0 Then ;failed to change Dir If FileExists ($root) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & $root & '". Unknown reason, exiting.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Exit Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & $root & '". Directory does not exist, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 Return EndIf EndIf If FileExists ($file_dir & '\' & $file) Then If FileGetSize ($file) <= FileGetSize ($file_dir & '\' & $file) Then _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir is smaller than, or equal to, duplicate in destination directory.') Return Else _RemoveTarget (2, $file_dir & '\' & $file, @ScriptLineNumber, 'Dir is smaller than, or equal to, duplicate to be moved.') EndIf Else FileWrite ($log,$time & '@ Warning dir "' & @WorkingDir & '\' & $file & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Return EndIf $error = DirMove (@workingdir & '\' & $file,$file_dir & '\',1) If $error = 0 Then ;Fail to move If FileExists ($file_dir & '\' & $file) Then FileWrite ($log,$time & '@ Warning moving dir "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '". Directory exists, unable to overwrite.flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 Else FileWrite ($log,$time & '! Error moving dir "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '". Unknown Reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else FileWrite ($log,$time & '# Moved directory "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf Return EndFunc ; # _GetCategory Edited January 8, 2011 by DoubleMcLovin Link to comment Share on other sites More sharing options...
MvGulik Posted January 9, 2011 Share Posted January 9, 2011 You seem to have some confusion about variable scopes. At the top of your script you have both Local and Global variable declarations. That doesn't work.Actually it works perfectly. But thats of course not the point. (which did not seem to hit home by the looks of it.)Mode: Declaring a variable with Local in the root of your file will make is Local to the whole script ... also called Global scope.For fast examples ... fine. For anything else ... Don't. And mixing both Local and Global defs in a row is even more a creditability killer. (no matter how good the rest of the code looks.) "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
DoubleMcLovin Posted January 9, 2011 Author Share Posted January 9, 2011 So rather than use global or local, should I just dim? I have it set so that variables must be declared, most of those variables can be declared in the code, but they would be declared more than one time as the code loops which is why I put them in the root of the file like that in the first place. I am still pretty new to programing, this is my longest script so far and I am doing this to learn so if I am missing the point on this please elaborate a little. Link to comment Share on other sites More sharing options...
MvGulik Posted January 9, 2011 Share Posted January 9, 2011 So rather than use global or local, should I just dim?Nono, no need for Dim, just use Global for all variables that have/need a global scope. Doing so at the start of your script is a good way to prevent potential and hard to track down "variable not defined" errors. It also serves as a good Global variable lookup list.Side note: Dim is depreciated. However ... Dim is still in play as its useful for redeclaring a fresh array on a existing variable. With some direct array content if need be.I am still pretty new to programing, this is my longest script so far and I am doing this to learn so if I am missing the point on this please elaborate a little.Sure. The bigger your script gets the better of you are* by making sure the code explains itself as much as possible. And preventing any unclear or ambiguous coding practices go a long way in that. Not using Local at the Root level of your script is just one of those things.*Especially after you have not looked at a particular project code. But you do need to fix a bug in it. "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted January 9, 2011 Share Posted January 9, 2011 (edited) Dim IS Global and Local, just with a lot of ambiguity and confusion. Don't use Dim.@MvGulikHowever ... Dim is still in play as its useful for redeclaring a fresh array on a existing variable.Say what? Edited January 9, 2011 by AdmiralAlkex .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
MvGulik Posted January 9, 2011 Share Posted January 9, 2011 (edited) Side effect of sticking to using "#AutoIt3Wrapper_Au3Check_Parameters=-d -w1 -w2 -w3 -w4 -w5 -w6 -q". (With more data in your question I might be able to give a more appropriated answer ... I hope.)+ Can't remember the last time I used it. Dim that is. So that might be some old, possible in need of change, personal opinion. Edited January 9, 2011 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
DoubleMcLovin Posted January 9, 2011 Author Share Posted January 9, 2011 Ok, so I should use Global at the root level, and if I have a variable declared inside a function that is allowed to die at the end of the function I can use Local? Link to comment Share on other sites More sharing options...
MvGulik Posted January 9, 2011 Share Posted January 9, 2011 Ok, so I should use Global at the root level, and if I have a variable declared inside a function that is allowed to die at the end of the function I can use Local?Correct.Can -> preverbly "should" "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
DoubleMcLovin Posted January 9, 2011 Author Share Posted January 9, 2011 Well while this is all well and fine and very educational at that, even after correcting that my script still locks directories in kind of a "limbo" type of event as the file almost gets stuck when the script is trying to move it. Here is the updated code: expandcollapse popupOpt('MustDeclareVars', 1) #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #Auto Unpacker#=============================================================================================================== ; Name...........: Auto Unpacker ; Description ...: Searches set directory and unpacks files automatically ; Syntax ........: ; Parameters ....: ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: The change log readout has several characters before text. They are explained below: ; ...............: @ = Indicates a soft warning. Eg. An error that has already handled itself ; ...............: ! = Indicates an error that could not be automatically solved. ; ...............: # = Indicates a notice for a file displacement ; ...............: ^ = Indicates a file deletion ; ...............: % = Indicates a general notice ; =============================================================================================================================== ;watch dirs delineated by a "|" Global $wDir = "c:\system66\downloads\done\" Global $wDirs = StringSplit($wDir,"|") ;This is a list of words that can seperate "parts" of a files, or segments of files Global $arc_type = "rar|zip|tar" Global $arc_types = StringSplit($arc_type,"|") ;an array of different archive types Global $movie_dir = "c:\system66\media\movies\" Global $os_dir = "c:\system66\useful\os\" Global $game_dir = "c:\system66\media\games\" Global $series_dir = "c:\system66\media\series\" Global $other_dir = "c:\system66\downloads\extracted" ;extract dir for unkown files Global $delay = 500 ;Delay time in milliseconds If FileExists (@ScriptDir & "\logs\changes.log") Then FileDelete (@ScriptDir & "\logs\changes.log") Global $log = FileOpen (@ScriptDir & "\logs\changes.log",41) Global $target, $files, $target_tmp, $error, $root, $line_num, $file, $subfile, $subfiles, $exit, $move_target, $fType Global $time = @MDAY & "/" & @MON & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " || " Global $footer = @TAB & "LINE: " ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: Main ; Description ...: ; Author ........: DoubleMcLovin ; Remarks .......: ; =============================================================================================================================== While 1 For $i1 = 1 to $wDirs[0] ;Cycles through watched dirs FileSetAttrib ($wDirs[$i1] & "\*","-R",1) ;Removes the Read-Only attribute. $error = FileChangeDir ($wDirs[$i1]) If $error = 0 Then ;failed to change Dir If FileExists ($wDirs[$i1]) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & $wDirs[$i1] & '". Unknown reason, exiting.' & $footer & @ScriptLineNumber & @CRLF) Exit Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & $wDirs[$i1] & '". Directory does not exist. Exiting.' & $footer & @ScriptLineNumber & @CRLF) Exit EndIf EndIf $files = FileFindFirstFile ("*.*") If @error Then ;empty dir FileWrite ($log,$time & '@ "' & $wDirs[$i1] & '" is empty, sleeping for ' & $delay & ' milliseconds.' & $footer & @ScriptLineNumber & @CRLF) Sleep ($delay) ElseIf $files = -1 Then ;Error in search string FileWrite ($log,$time & '! Script encountered an error, closing.' & $footer & @ScriptLineNumber & @CRLF) Else While 1 $exit = 0 ; Resets exit code $fType = 0 ; Resets file type $target = FileFindNextFile ($files) ;Cycles through files in watched dirs If @error Then ExitLoop If @extended = 1 Then $fType = 2 ;directory check $error = FileExists (@WorkingDir & '\' & $target) ;Determine type for $fType If $error = 1 And $fType = 0 Then ;exists and is not a directory _GetType ($target) ElseIf $error = 0 Then ;DNE $fType = 0 ElseIf $fType = 2 Then ;directory Else ;bizzarre unexplainable error FileWrite ($log,$time & '! Script encountered an error, closing. Current dir\file: "' & @WorkingDir & '\' & $target & '".' & $footer & @ScriptLineNumber & @CRLF) Exit EndIf ;Perform actions based on $fType (archive, directory, file) Switch $fType Case 0 ;File does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $target & '" does not exist.' & $footer & @ScriptLineNumber & @CRLF) Case 1 ;Archive _RenameTarget ($target, 1, @ScriptLineNumber) _Extract ($wDirs[$i1],$target, @ScriptLineNumber) Case 2 ;Directory _RenameTarget ($target, 2, @ScriptLineNumber) _ExploreDir ($target, @ScriptLineNumber) _GetCategory ($target, @ScriptLineNumber) Case 3 ;File _RenameTarget ($target, 1, @ScriptLineNumber) _PutFile ($target, @ScriptLineNumber) EndSwitch ;~ If $exit = 1 Then ExitLoop WEnd EndIf FileClose ($files) FileClose ($subfiles) ;~ Exit Sleep ($delay) Next WEnd ; #Main ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _ExploreDir ; Description ...: Opens directories to interact with files therein ; Syntax ........: _ExploreDir ($fDir) ; Parameters ....: $fDir - Initial directory working in ; ...............: $line_num - Line number from where this func was called ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _ExploreDir ($fDir, $line_num) If $exit = 1 Then Return $root = @WorkingDir $error = FileChangeDir ($fDir) If $error = 0 Then ;failed to change Dir If FileExists ($fDir) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Unknown reason, flagging exit code.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) $exit = 1 Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Directory does not exist.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) Return EndIf EndIf $subfiles = FileFindFirstFile ("*") If @error Then ;If the dir is empty FileChangeDir ($root) _RemoveTarget (2, $fDir, @ScriptLineNumber, 'Dir is empty.') $fDir = "" ElseIf $subfiles = -1 Then ;error matcing FileWrite ($log,$time & '@ Error, no files matched the search string.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) Return Else While 1 $fType = 0 ;reset fType $subfile = FileFindNextFile ($subfiles) If @error Then ExitLoop If @extended = 1 Then $fType = 2 ;directory check $error = FileExists (@WorkingDir & '\' & $subfile) If $error = 1 And $fType = 0 Then ;exists _GetType ($subfile) ElseIf $error = 0 Then ;DNE $fType = 0 ElseIf $fType = 2 Then Else FileWrite ($log,$time & '! Script encountered an error, closing. Current dir\file: "' & @WorkingDir & '\' & $subfile & '".' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) Exit EndIf Switch $fType Case 0 ;File does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $subfile & '" does not exist.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) Case 1 ;Archive _Extract (@WorkingDir,$subfile, @ScriptLineNumber) $subfiles = FileFindFirstFile ('*.*') ;Reloads files to prevent loading removed archive parts Case 2 ;Directory _UnloadDir ($subfile, @ScriptLineNumber) Case 3 ;File _RenameTarget ($subfile, 1, @ScriptLineNumber) EndSwitch WEnd EndIf FileClose ($subfiles) $error = FileChangeDir ($root) If $error = 0 Then ;failed to change Dir If FileExists ($fDir) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Unknown reason.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) $fDir = "" Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $fDir & '". Directory does not exist.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) Return EndIf EndIf Return EndFunc ; # _ExploreDir ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _MoveTarget ; Description ...: Moves a file or folder ; Syntax ........: _MoveTarget (ByRef $file, $destination, $type, $line_num) ; Parameters ....: $file - File/Dir to be moved ($target) ; ...............: $destination - Where the file/folder is being moved ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; Return values .: $file - For $target association ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _MoveTarget (ByRef $file, $destination, $type, $line_num) If $exit = 1 Then Return Switch $type Case 1 ;File If FileExists ($file) Then $error = FileMove (@WorkingDir & '\' & $file,@WorkingDir & '\' & $destination,9) If $error = 1 Then ;success FileWrite ($log,$time & '# File "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $destination & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be moved to "' & @WorkingDir & '\' & $destination & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else ;If the file trying to be moved does not exist FileWrite ($log,$time & '@ File "' & @WorkingDir & '\' & $file & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $file = "" ;clears the target EndIf Case 2 ;Directory If FileExists ($file) Then $error = DirMove (@WorkingDir & '\' & $file,@WorkingDir & '\' & $destination,1) If $error = 1 Then ;success FileWrite ($log,$time & '# Directory "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $destination & '". ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be moved to "' & @WorkingDir & '\' & $destination & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else ;If the directory trying to be moved does not exist FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $file & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $file = "" ;clears the target EndIf EndSwitch $file = $destination ;sets the new name on the file for further work Return EndFunc ; # _MoveTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _RenameTarget ; Description ...: Renames file to remove bad syntax and put all files into a standard format ; Syntax ........: _RenameTarget (ByRef $file, $type, $line_num) ; Parameters ....: $file - Dir/File being renamed ($target) ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; Return values .: $file - New name for $target ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _RenameTarget (ByRef $file, $type, $line_num) If $exit = 1 Then Return Switch $type Case 1 ;File $move_target = StringReplace (StringReplace (StringRegExpReplace (StringRegExpReplace ($file," ","."),"[-()_]","."),"..","."),"..",".") If StringRight ($move_target,1) = "." Then $move_target = StringTrimRight($move_target,1) If FileExists (@WorkingDir & '\' & $move_target) Then If Not $move_target = $file Then FileWrite ($log,$time & '% File already exists: "' & @WorkingDir & '\' & $move_target & '". Overwriting.' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) EndIf EndIf _MoveTarget ($file, $move_target, 1, @ScriptLineNumber) If $exit = 1 Then Return Case 2 ;Dir $move_target = StringReplace (StringReplace (StringRegExpReplace (StringRegExpReplace ($file," ","."),"[-()_]","."),"..","."),"..",".",1) If StringRight ($move_target,1) = "." Then $move_target = StringTrimRight($move_target,1) If FileExists (@WorkingDir & '\' & $move_target) Then If FileGetSize (@WorkingDir & '\' & $file) <= FileGetSize (@WorkingDir & '\' & $move_target) Then ;If the newer is smaller than the older If Not $move_target = $file Then FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $file & '" flagged for deletion. Directory "' & @WorkingDir & '\' & $move_target & '" already exists. ' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $file, @ScriptLineNumber, "Copy in destination is larger than original.") EndIf ElseIf FileGetSize (@WorkingDir & '\' & $file) > FileGetSize (@WorkingDir & '\' & $move_target) Then FileWrite ($log,$time & '@ Directory "' & @WorkingDir & '\' & $move_target & '" flagged for deletion. Directory "' & @WorkingDir & '\' & $file & '" is larger. ' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $move_target, @ScriptLineNumber, "Copy in destination is smaller than original.") Return EndIf EndIf _MoveTarget ($file, $move_target, 2, @ScriptLineNumber) EndSwitch $file = $move_target ;Associates $target to new name Return EndFunc ; # _RenameTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _RemoveTarget ; Description ...: Deletes directory with error check ; Syntax ........: _RemoveTarget($type, $file, $line_num, $reason) ; Parameters ....: $dir - Directory to be removed ; ...............: $type - Determines file or folder ; | $type = 1 File ; | $type = 2 Directory ; ...............: $line_num - The line number where this call was referenced. Used for debugging purposes. ; ...............: $reason - A text string by user input for use in the log ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Removes recursively ; =============================================================================================================================== Func _RemoveTarget ($type, $file, $line_num, $reason) If $exit = 1 Then Return Switch $type Case 1 ;File $error = FileDelete ($file) If $error = 1 Then ;success FileWrite ($log,$time & '^ File "' & @WorkingDir & '\' & $file & '" was deleted. ' & $reason & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! File "' & @WorkingDir & '\' & $file & '" cannot be deleted. Unknown reason. ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf Case 2 ;Dir $error = DirRemove ($file,1) If $error = 1 Then ;success FileWrite ($log,$time & '^ Directory "' & @WorkingDir & '\' & $file & '" was deleted. ' & $reason & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else FileWrite ($log,$time & '! Directory "' & @WorkingDir & '\' & $file & '" cannot be deleted. Unknown reason.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf EndSwitch Return EndFunc ; # _RemoveTarget ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _PutFile ; Description ...: Organizes loose files ; Syntax ........: _PutFile($file, $line_num) ; Parameters ....: $file - File to be extracted ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: This function attempts to place loose files in the download directory into a directory named after themselves. ; If two or more files have similar names, they will be grouped in the same folder. Similar name is determined by removing all ; numeric characters from the name (excluding extension) for analysis. ; =============================================================================================================================== Func _PutFile ($file, $line_num) If $exit = 1 Then Return Switch StringRegExp ($file,"(?i).*\..{1,4}\b",0) ;performs a file check by looking for a decimal and 1-4 characters after that but only at the end (extenstion check) Case 1 ;If it appears to be a file ;Start by stripping name of extension and numbers $target_tmp = StringRegExpReplace (StringReplace ($file,StringRight ($file,4),""),"\d","") If StringRight ($target_tmp,1) = "." Then StringTrimRight ($target_tmp,1) ;in case of 4 character extension, this will remove the trailing decimal FileMove ($file,@WorkingDir & '\' & $target_tmp & '\',9) FileWrite ($log,$time & '# The file "' & @WorkingDir & '\' & $file & '" was moved to "' & @WorkingDir & '\' & $target_tmp & '". ' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Case 2 ;Does not appear to be a file EndSwitch Return EndFunc ; # _PutFile ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _GetType ; Description ...: Gets the file type for future operations ; Syntax ........: _GetType ($file) ; Parameters ....: $file - File being analyzed ; ...............: $fType - The handle for file type variable ; Return values .: $fType - A number corresponding to type of file, directory, or archive. ; ...............: | 1 Archive ; ...............: | 2 Directory ; ...............: | 3 File ; Author ........: DoubleMcLovin ; Remarks .......: None ; =============================================================================================================================== Func _GetType ($file) If $exit = 1 Then Return Local $fAttrib = FileGetAttrib ($file) Switch StringRegExp ($file,"(?i).*(\.part\d{1,}\.rar|\.(\d{3}|r\d{2}))\b") ;Didn't feel like making this an if Case 1 Switch StringRegExp ($file,"(?i).*(\.part(0{1,}1|1)\.rar\b") Case 1 $fType = 1 ;If it is the first part of the archive, deal with it. Case Else $fType = 4 ;If we are dealing with a part of an archive, don't touch it EndSwitch EndSwitch Switch StringRegExp ($file,"(?i).*\.((r|t)ar|zip)",0) ;Checks to see if file is an archive Case 1 $fType = 1 ;Archive Case Else $fType = 3 ;File EndSwitch Return EndFunc ; # _GetType ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _Extract ; Description ...: Extracts specified file ; Syntax ........: _Extract($fDir, $file) ; Parameters ....: $fDir - Directory working in ; ...............: $file - File to be extracted ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: This function simply extracts the directed file into the done directory ; =============================================================================================================================== Func _Extract ($fDir, $file, $line_num) If $exit = 1 Then Return Local $extract_code = RunWait ('"' & @ProgramFilesDir & '\winrar\winrar.exe" x "' & @WorkingDir & '\' & $file & '" "' & $fDir & '"') Switch $extract_code Case 0 ;extracted successfully FileWrite ($log,$time & '# Extracted archive "' & @WorkingDir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) _RemoveTarget (1, $file, @ScriptLineNumber, 'Successful extraction - deleting archive.') If StringRegExp ($file,"(?i).*\.(rar|zip)\b",0) = 1 Then ;if we have a zip or rar file check for parts and delete them too Local $rar_parts = FileFindFirstFile ("*") While 1 Local $rar_part = FileFindNextFile ($rar_parts) If @error Then ExitLoop ;out of files Switch StringRegExp ($rar_part,"(?i).*(part\d{1,}\.rar|\.(\d{3}|r\d{2}))\b",0) ;Checks to see if each file matches the parts filter Case 1 Local $rar_part_tmp = StringRegExpReplace ($rar_part,"(?i)(part\d{1,}\.(rar|zip)|\.(\d{3}|(z|r)\d{2}))\b","") Local $target_tmp = StringRegExpReplace ($file,"(?i)(part\d{1,}\.(rar|zip)|\.(\d{3}|(z|r)r\d{2}))\b","") If $rar_part_tmp = $target_tmp Then _RemoveTarget (1, $rar_part, @ScriptLineNumber, 'File is a part of an extracted archive.') EndSwitch WEnd EndIf Case 1 FileWrite ($log,$time & "! Failed to extract archive " & @WorkingDir & '\' & $file & '.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Case Else FileWrite ($log,$time & "@ Unknown result for extract of " & @WorkingDir & '\' & $file & '. Extract code = "' & $extract_code & '"' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndSwitch Return EndFunc ; # _Extract ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _UnloadDir ; Description ...: Pulls files out of a subdir and places them in current dir ; Syntax ........: _UnloadDir ($sub) ; Parameters ....: $sub - subdir to unload ; ...............: $line_num - line number that called this func ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Should never be called in watch dir ; =============================================================================================================================== Func _UnloadDir ($file, $line_num) If $exit = 1 Then Return If StringInStr ($file,"sub") Then ;ignore subtitles directories FileWrite ($log,$time & '% Subtitles dir located at "' & @WorkingDir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) ElseIf StringInStr ($file,"sample") Then _RemoveTarget (2, $file, @ScriptLineNumber, 'Sample dir.') Else ;Takes all files/folders from dir and moves them to dir's root FileMove (@WorkingDir & '\' & $file & "\*.*",@WorkingDir & '\' & $file,9) DirMove (@WorkingDir & '\' & $file & "\*",@WorkingDir & '\' & $file,1) FileWrite ($log,$time & '# Moved contents of "' & @WorkingDir & '\' & $file & '" to "' & @WorkingDir & '".' & $footer & 'i' & $line_num & ',' & 'o' & @ScriptLineNumber & @CRLF) _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir unloaded.') EndIf Return EndFunc ; # _UnloadDir ; #INTERNAL_USE_ONLY#============================================================================================================ ; Name...........: _GetCategory ; Description ...: Determines category of folder for classification ; Syntax ........: _GetCategory ($file, $line_num) ; Parameters ....: $file - Directory being reviewed ; ...............: $line_num - line number that called this function ; Return values .: None ; Author ........: DoubleMcLovin ; Remarks .......: Organizes according to $category ; ...............: - 1 movie ; ...............: - 2 game ; ...............: - 4 OS ; ...............: - 5 other ; ...............: - 6 series ; =============================================================================================================================== Func _GetCategory (ByRef $file, $line_num) If $exit = 1 Then Return Local $category ;Reset $category ; The next three are done in this order because the Movie filter will pick up the Series and Porn If StringRegExp ($file,"(?i).*(s\d{1,2}e\d{1,2}.*|series\d{1,2}).*",0) Then ;Series filter $category = 6 ElseIF StringRegExp ($file,"(?i).*\.\d{4}\.((dvd|br|bluray)rip|r5)\..*",0) Then ;Movie filter $category = 1 Else ;general filter $root = @WorkingDir $target_tmp = $file $error = FileChangeDir ($file) If $error = 0 Then ;failed to change Dir If FileExists ($file) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Unknown reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 $file = "" Return Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & @WorkingDir & '\' & $file & '". Directory does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Return EndIf EndIf $subfiles = FileFindFirstFile ("*") If @error Then ;If the dir is empty FileChangeDir ($root) _RemoveTarget (2, $file, @ScriptLineNumber, 'Empty dir.') ElseIf $subfiles = -1 Then ;error matcing FileWrite ($log,$time & '@ Error, no files matched the search string.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Else While 1 $subfile = FileFindNextFile ($subfiles) If @error Then ExitLoop If StringRight ($subfile,3) = "iso" Then ;iso check If FileGetSize ($subfile) >= 700000000 Then ;700MB If StringRegExp ($subfile,"(?i).*(microsoft|adobe|OS(X|.+X))",0) Then $category = 4 Else $category = 2 ;size must be > 700MB, and not include major software names, must be .iso EndIf EndIf EndIf WEnd EndIf FileClose ($subfiles) EndIf If $category = 0 Then $category = 5 Local $file_dir Switch $category Case 1 $file_dir = $movie_dir Case 2 $file_dir = $game_dir Case 4 $file_dir = $os_dir Case 5 $file_dir = $other_dir Case 6 $file_dir = $series_dir EndSwitch $file = $target_tmp $error = FileChangeDir ($root) If $error = 0 Then ;failed to change Dir If FileExists ($root) Then FileWrite ($log,$time & '! Could not change dir from "' & @WorkingDir & '\' & '" to "' & $root & '". Unknown reason, exiting.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Exit Else FileWrite ($log,$time & '@ Could not change dir from "' & @WorkingDir & '\' & '" to "' & $root & '". Directory does not exist, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 Return EndIf EndIf If FileExists ($file_dir & '\' & $file) Then If FileGetSize ($file) <= FileGetSize ($file_dir & '\' & $file) Then _RemoveTarget (2, $file, @ScriptLineNumber, 'Dir is smaller than, or equal to, duplicate in destination directory.') Return Else _RemoveTarget (2, $file_dir & '\' & $file, @ScriptLineNumber, 'Dir is smaller than, or equal to, duplicate to be moved.') EndIf Else FileWrite ($log,$time & '@ Warning dir "' & @WorkingDir & '\' & $file & '" does not exist.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) Return EndIf $error = DirMove (@workingdir & '\' & $file,$file_dir & '\',1) If $error = 0 Then ;Fail to move If FileExists ($file_dir & '\' & $file) Then FileWrite ($log,$time & '@ Warning moving dir "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '". Directory exists, unable to overwrite.flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 Else FileWrite ($log,$time & '! Error moving dir "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '". Unknown Reason, flagging exit code.' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) $exit = 1 EndIf Else FileWrite ($log,$time & '# Moved directory "' & @WorkingDir & '\' & $file & '" to "' & $file_dir & '\' & $file & '".' & $footer & 'o' & $line_num & "," & 'i' & @ScriptLineNumber & @CRLF) EndIf Return EndFunc ; # _GetCategory Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted January 9, 2011 Share Posted January 9, 2011 That is a lot of code to wade through. Just FYI, people will be a lot more interested in helping with a reproducer (short simple running example) than reading through the Bible. And when your script is only 1-2 dozen lines you are likely to find the problem yourself .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
DoubleMcLovin Posted January 9, 2011 Author Share Posted January 9, 2011 (edited) Sorry, I know that is a lot of code to work with. But I have been banging my head against the wall on it for weeks now. If no one feels like sifting through all that code, there is certainly no implied obligation. I am simply seeing if anyone is bored and more experienced than I. Edited January 9, 2011 by DoubleMcLovin Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted January 9, 2011 Moderators Share Posted January 9, 2011 (edited) Sorry, I know that is a lot of code to work with. But I have been banging my head against the wall on it for weeks now. If no one feels like sifting through all that code, there is certainly no implied obligation. I am simply seeing if anyone is bored and more experienced than I. The code is certainly unorganized, making it difficult to follow. If you have a directory locked, it probably has to do with either an unclosed file handle or something to do within your directory changes. I know just by skimming through it ( sorry, 500+ lines of code, I've never been so bored that I'd go through each step of it and try to make heads or tails of it ), but I did see where your extract function is missing a FileClose() call at least, so I'm left to assume you're abusing a global variable or you're leaving open handles. On another note, you may want to look at what cmd.exe could do for you. I'm sure you could cut about 300 lines off this code if I have an inkling of what it's (your code) even doing. Edit: DirMove (@WorkingDir & '\' & $file & "\*",@WorkingDir & '\' & $file,1) Are you sure that works? Something does not look right there. Edited January 9, 2011 by SmOke_N Something else stood out Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now