Jump to content

retrieving extensions from array


Recommended Posts

Using RecFileListToArray.au3 to create an array of all the files in a a given directory, i would like to then search the array for any files ending with MP4,AVI and MKV. If any are found then my script would excute a given action. My question is what would be the fastest approach to this. I havent been able to figure out a proper search method using _Arraysearch().

thank you

Edited by Saitoh183
Link to comment
Share on other sites

Hi, maybe this helps you to start your mission.

#include<Array.au3>
$re = _FileListRecFilter(@ScriptDir)
_ArrayDisplay($re)
ConsoleWrite(_ArrayToString($re, @CR) & @CRLF)
Func _FileListRecFilter($dir, $ext = '*.exe, *.ini')
Local $files = _getDOSOutput('dir /s /b ' & $ext)
$files_A = StringSplit(StringStripCR($files), @LF)
Return $files_A
EndFunc   ;==>_FileListRecFilter
Func _getDOSOutput($command)
Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
While 1
  $text &= StdoutRead($Pid, False, False)
  If @error Then ExitLoop
  Sleep(10)
WEnd
Return $text
EndFunc   ;==>_getDOSOutput

.

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

or

If StringRegExp($array[$i], ".*(\.avi|\.mkv|\.mp4)\z") Then ; do something

WoW...thanks, i will give it a try

Edit: this only returned the number of occurrence and not the actual extension like the other code did

Edited by Saitoh183
Link to comment
Share on other sites

My approach was not to find all files and then find the ones with a specific ending. Isn't it faster to just search for the files you are looking for and then react on all that files?

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Well what i need it for is to check for the first occurrence of a file with one of those 3 extensions and if its found run a program. Actually your method could work as well since if the answer is equal or greater then one then the statement is true. My issue now is that if there is nothing in the folder when this line executes

$Hasfiles = _RecFileListToArray($tv_down,"*.avi;*.mp4;*.mkv",1,1,0,0)

i get a error.. Subscript used with non-Array variable.:

here is the part of the script

if Stringright($Hasfiles[1], 4) = ".mp4" or Stringright($Hasfiles[1], 4) = ".avi" or Stringright($Hasfiles[1], 4) = ".mkv" Then
  If $state = "5" Then
   If $_guitype[1][1] = "True" then _ProgressSet($progon, Round (1/9*100,0), Round (1/9*100,0) & '%',"Running TheRenamer TV")
  Else
   _ProgressSet($Progress1, Round (1/9*100,0), Round (1/9*100,0) & '%',"Running TheRenamer TV")
  endif
  RunWait($fetchTV)
  If $state = "5" Then
   If $_guitype[1][1] = "True" then
    _ProgressSet($progon, Round (2/9*100,0), Round (2/9*100,0) & '%',"Finish TheRenamer TV")
    sleep(1000)
   Else
    sleep(1000)
   EndIf
  Else
   _ProgressSet($Progress1, Round (2/9*100,0), Round (2/9*100,0) & '%',"Finish TheRenamer TV")
   sleep(1000)
  EndIf
  if $Runf = False Then ExitLoop
  If $pausef = True Then _pause ()
  If $state = "5" Then
   If $_guitype[1][1] = "True" then
    _ProgressSet($progon, Round (2/9*100,0), Round (2/9*100,0) & '%',"Updating TV Logs")
    sleep(1000)
   Else
    sleep(1000)
   EndIf
  Else
   _ProgressSet($Progress1, Round (2/9*100,0), Round (2/9*100,0) & '%',"Updating TV Logs")
   Sleep(1000)
  EndIf
  $file1 = FileRead($renamerlog)
  $file2 = FileOpen($tvlog,1)
  $file3 = FileRead($historylog)
  ;URL for following expression: http://www.autoitscript.com/forum/topic/133363-remove-blank-lines-in-a-file/
  $sRtn = stringreplace(StringRegExpReplace(StringRegExpReplace($file3, "(\v)+", @CRLF), "(^\v*)|(\v*\Z)", ""), "######################## END Session", "")
  FileWriteLine($file2, "############ " & @MDAY & "/" & @MON & "/" & @YEAR & "############ " & @CRLF)
  FileWriteLine($file2, "############ " & @HOUR & "h:" & @MIN & "############ " & @CRLF)
  FileWriteLine($file2,$sRtn)
  FileWriteLine($file2,$file1)
  FileClose($file2)
  Filedelete($historylog)
  If $state = "5" Then
   If $_guitype[1][1] = "True" then
    _ProgressSet($progon, Round (3/9*100,0), Round (3/9*100,0) & '%',"Finish Updating TV Logs")
    sleep(1000)
   Else
    sleep(1000)
   EndIf
  Else
   _ProgressSet($Progress1, Round (3/9*100,0), Round (3/9*100,0) & '%',"Finish Updating TV Logs")
   sleep(1000)
  EndIf
  if $Runf = False Then ExitLoop
  If $pausef = True Then _pause ()
Else
  If $state = "5" Then
   If $_guitype[1][1] = "True" then
    _ProgressSet($progon, Round (3/9*100,0), Round (3/9*100,0) & '%',"No TV Shows Added")
    sleep(1000)
   Else
    sleep(1000)
   Endif
  Else
   _ProgressSet($Progress1, Round (3/9*100,0), Round (3/9*100,0) & '%',"No TV Shows Added")
   sleep(1000)
  EndIf
  if $Runf = False Then ExitLoop
  If $pausef = True Then _pause ()
Endif
Else
If $state = "5" Then
  If $_guitype[1][1] = "True" then
   _ProgressSet($progon, Round (3/9*100,0), Round (3/9*100,0) & '%')
   sleep(1000)
  Else
   sleep(1000)
  EndIf
Else
  _ProgressSet($Progress1, Round (3/9*100,0), Round (3/9*100,0) & '%')
  sleep(1000)
EndIf
Endif
Link to comment
Share on other sites

You need to check that $HasFiles is an array before you process it. Use something like this:

$Hasfiles = _RecFileListToArray($tv_down,"*.avi;*.mp4;*.mkv",1,1,0,0)
If IsArray($HasFiles) Then
; put your script here
Else
    MsgBox(64, "Error", "No Files found in that location")
Endif

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You need to check that $HasFiles is an array before you process it. Use something like this:

$Hasfiles = _RecFileListToArray($tv_down,"*.avi;*.mp4;*.mkv",1,1,0,0)
If IsArray($HasFiles) Then
; put your script here
Else
    MsgBox(64, "Error", "No Files found in that location")
Endif

or

If StringRegExp($array[$i], ".*(\.avi|\.mkv|\.mp4)\z") Then ; do something

Using theses 2 solutions seems to have fix my problem :graduated:

if IsArray($HasFiles) and StringRegExp($HasFiles[1], ".*(\.avi|\.mkv|\.mp4)\z") = 1 Then
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...