;===============================================================================
;
; Function Name:  FileRename()
; Description:    Move files of certain extensions to a directory and rename 
;                 to their creation dates.
;
; Author(s):      chaos945 <chaos945@msn.com>
;
;===============================================================================
  ;__________________________________________________________
  ; F L A G S
  ; 0 = all files
  ; 1 = single file
  ;__________________________________________________________

  ;__________________________________________________________
  ; R E T U R N S
  ; 0 = incorrect $iflag
  ; 1 = invalid extension
  ; 2 = source folder/path does not exist
  ; 3 = destination folder/path does not exist
  ; 4 = no files found
  ; 5 = unable to get file info
  ; Success = No errors
  ; Unknown Error = Script finished but there was an error
  ;__________________________________________________________

Func FileRename($iflag, $iextension, $isource, $idestination)
  ;check function for valid entry
	If $iflag < 0 Or $iflag > 1 Then
		SetError(1)
		Return 0
	EndIf

	If StringIsAlpha($iextension) = 0 Then
		SetError(1)
		Return 1
	EndIf
	
	If FileExists($isource) = 0 Then
		SetError(1)
		Return 2
	EndIf
	
	If FileExists($idestination) = 0 Then
		SetError(1)
		Return 3
	EndIf
	
	;begin action
		If $iflag = 0 Then
			$ihandle = FileFindFirstFile($isource & "*." & $iextension)
			If $ihandle = -1 Then
				SetError(1)
				Return 4
			Else
				Local $ictr = 0
				Dim $ilist[1]
				While 1
					$inext = FileFindNextFile($ihandle)
					If @error Then 
						SetError(0)
						ExitLoop
					EndIf
					_ArrayAdd($ilist, $inext)
					$ictr = $ictr + 1
				WEnd
				_ArrayDelete($ilist, 0)
				_ArrayInsert($ilist, 0, $ictr)
			EndIf
			
			For $ia = 1 to $ilist[0]
				$iinfo = FileGetTime($isource & $ilist[$ia], 1)
				If $iinfo = 1 Then
					SetError(1)
					Return 5
				Else	
					;;;enter date formatting
				EndIf
				
				FileCopy($isource, $idestination & $iinfo[1] & "-" & $iinfo[2] & "-" & $iinfo[0] & " " & $iinfo[3] & $iinfo[4] & "." & $iinfo[5] & "." & $iextension, 0)
				FileDelete($isource & $ilist[$ia])
			Next
		ElseIf $iflag = 1 Then
			;;;single file entry
		EndIf
		
		If not @error Then 
			Return "Success"
		Else
			Return "Unknown Error"
		EndIf
EndFunc


;===============================================================================
;
; Function Name:  _ArrayInsert()
; Description:    Add a new value at the specified position.
;
; Author(s):      Jos van der Zande <jdeb@autoitscript.com>
;
;===============================================================================
Func _ArrayInsert(ByRef $avArray, $iElement, $sValue)
;Func _ArrayInsert(ByRef $avArray, $iElement, $sValue = "") <= Removed = "" got ride of the error
	Local $iCntr = 0
	
	If Not IsArray($avArray) Then
		SetError(1)
		Return 0
	EndIf
	; Add 1 to the Array
	ReDim $avArray[UBound($avArray) + 1]
	; Move all entries one up till the specified Element
	For $iCntr = UBound($avArray) - 1 To $iElement + 1 Step - 1
		$avArray[$iCntr] = $avArray[$iCntr - 1]
	Next
	; add the value in the specified element
	$avArray[$iCntr] = $sValue
	Return 1
EndFunc   ;==>_ArrayInsert

;===============================================================================
;
; Function Name:  _ArrayDelete()
; Description:    Deletes the specified element from the given array, returning
;                 the adjusted array.
; Author(s)       Cephas <cephas@clergy.net>
; Modifications   Array is passed via Byref  - Jos van der zande
;===============================================================================
Func _ArrayDelete(ByRef $avArray, $iElement)
	Local $iCntr = 0, $iUpper = 0, $iNewSize = 0
	
	If (Not IsArray($avArray)) Then
		SetError(1)
		Return ""
	EndIf
	
	; We have to define this here so that we're sure that $avArray is an array
	; before we get it's size.
	Local $iUpper = UBound($avArray)    ; Size of original array
	
	; If the array is only 1 element in size then we can't delete the 1 element.
	If $iUpper = 1 Then
		SetError(2)
		Return ""
	EndIf
	
	Local $avNewArray[$iUpper - 1]
	If $iElement < 0 Then
		$iElement = 0
	EndIf
	If $iElement > ($iUpper - 1) Then
		$iElement = ($iUpper - 1)
	EndIf
	If $iElement > 0 Then
		For $iCntr = 0 To $iElement - 1
			$avNewArray[$iCntr] = $avArray[$iCntr]
		Next
	EndIf
	If $iElement < ($iUpper - 1) Then
		For $iCntr = ($iElement + 1) To ($iUpper - 1)
			$avNewArray[$iCntr - 1] = $avArray[$iCntr]
		Next
	EndIf
	$avArray = $avNewArray
	SetError(0)
	Return 1
EndFunc   ;==>_ArrayDelete

;===============================================================================
;
; Function Name:  _ArrayAdd()
; Description:    Adds a specified value at the end of an array, returning the
;                 adjusted array.
; Author(s):      Jos van der Zande <jdeb@autoitscript.com>
;
;===============================================================================
Func _ArrayAdd(ByRef $avArray, $sValue)
	If IsArray($avArray) Then
		ReDim $avArray[UBound($avArray) + 1]
		$avArray[UBound($avArray) - 1] = $sValue
		SetError(0)
		Return 1
	Else
		SetError(1)
		Return 0
	EndIf
EndFunc   ;==>_ArrayAdd
