Sign in to follow this  
Followers 0
yevlar

Sending a Command once from within a loop without halting commands that follow

9 posts in this topic

Hi - I've been struggling with this all day and can't seem to figure out a solution, and was hoping you guys might have some ideas.

I have a script that is grabbing a file list from a folder and reading it into an array. Once this list is in the array I use StringRight to isolate the file extension. What I want to do then is to check the extensions of each line and send a command to the command prompt to run an checksum on the files, but I only want this checksum command to run once per extension (and output a logfile containing all of the checksum for files of that extension.)

So far I have two versions but neither works properly. The first repeats the checksum command for every file. The only way I've found to make it only run once is to use ExitLoop, which prevents further checksums from running.

For $i = 1 to UBound($filelist) -1
   $ext = StringRight($filelist[$i], 4)
   if $ext = ".mov" Then
   ControlSend($cmd, "", "", "md5sum *.mov > md5mov.log" & @crlf)
   ExitLoop; without this it runs the ControlSend command for every file. With, it prevents the following ElseIf from running.
   ElseIf $ext = ".mxf" Then
   ControlSend($cmd, "", "", "md5sum *.mxf > md5mxf.log" & @crlf)
   ExitLoop
   EndIf
Next

The second version runs both commands only once, but it runs them regardless of whether or not the file extension is present in the array, creating an empty .log file. I attempted adding an EndIf to both, but that (much like the ExitLoop in the above example) stopped the following For command from running.

For $i = 1 to UBound($filelist) -1
   $ext = StringRight($filelist[$i], 4)
   if $ext = ".mov" Then ExitLoop
   ControlSend($cmd, "", "", "md5sum *.mov > md5mov.log" & @crlf)
   ExitLoop ;without the second ExitLoop, the command continues repeating for every file in the folder regardless of extension
Next

For $mxf = 1 to UBound($filelist) -1
   $ext2 = StringRight($filelist[$mxf], 4)
   if $ext2 = ".mxf" Then ExitLoop
   ControlSend($cmd, "", "", "md5sum *.mxf > md5mxf.log" & @crlf)
   ExitLoop
Next

I also tried this, which failed to send any command to the prompt:

if $ext2 = ".mxf" Then ExitLoop
if $ext2 = ".mxf" Then ControlSend($cmd, "", "", "md5sum *.mxf > md5mxf.log" & @crlf)

Any help is, as always, greatly appreciated!

Thanks!

Share this post


Link to post
Share on other sites



I would use a case statement personally.

But at any rate, in the first block of code, you run the md5sum when $ext = ".mov" but in the second block you exitloop when $ext = ".mov"

I'm not sure if this helps at all.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

_ArrayUnique  https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayUnique.htm

and before that you put the ext at a field at your start array

or create a array with the ext you done and search into that array if the ext is done or not  _ArraySearch $ext[] with arrayadd

or when a ext is done remove all enteries from that array with _ArrayDelete so you got only the ones you have todo

Start to deleted from the end to the begin.

Edited by Spider001

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <File.au3>

Local $aFileList = _FileListToArray(@DesktopDir, "*.*",$FLTA_FILES,True)
_ArrayDisplay($aFileList)

While UBound($aFileList) > 1
    $ext = _GetPathSplit($aFileList[1])[4]
    ;run somthing

    For $j = $aFileList[0] to 1 Step - 1
      If StringRight($aFileList[$j],4) = $ext Then
         _ArrayDelete($aFileList, $j)
      EndIf
   Next
   $aFileList[0] = UBound($aFileList) - 1
 _ArrayDisplay($aFileList)
 WEnd

 Func _GetPathSplit($file)
   Local $sDrive = "", $sDir = "", $sFilename = "", $sExtension = ""
   Return _PathSplit($file, $sDrive, $sDir, $sFilename, $sExtension)
EndFunc

Edited by Spider001

Share this post


Link to post
Share on other sites

Thanks for the suggestions, guys! I think that a case statement is probably the best route to take (partly because Spider's code is, frankly, beyond my comprehension. ;-) I'll try a few things and get back to you if I can't figure it out.

I did also find another workaround - using the second set of code that I posted and just running FileDelete on the 0k files, but that I don't think would be considered "good programming." 

Thanks again!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

yevlar,

This works for me...

#include <array.au3>
#include <File.au3>

Local $filelist = _arraytostring(_FileListToArray(@DesktopDir)) ; get the filelist in string format
local $aEXT     =   stringregexp($filelist,'\.(\w+)\|',3)       ; get array of extentions only
$aEXT           = _ArrayUnique($aEXT)                           ; strip all duplicate extentions

_arraydisplay($aEXT)

For $i = 1 To UBound($aEXT) - 1
    switch stringlower($aEXT[$i])
        case 'url'
            ;ControlSend($cmd, "", "", "md5sum *.url > md5mov.log" & @CRLF)
            ConsoleWrite('Processing ' & stringlower($aEXT[$i]) & @CRLF)
        case 'lnk'
            ;ControlSend($cmd, "", "", "md5sum *.lnk > md5mxf.log" & @CRLF)
            ConsoleWrite('Processing ' & stringlower($aEXT[$i]) & @CRLF)
    endswitch
Next

kylomas

edit:  This code handles files with multiple nodes, e.g. my.test.txt.txt and extentions can be any length.

edit2: Change "lnk" and "url" to whatever you want...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Awesome, Kylomas! I'll give that a try on Monday and will update then. Thanks!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Kylomas - That worked! But there is one weird thing - for some reason it will skip over an extension if there's only one file with that extension - i.e. in the test folder I'm using I had one .rar file, and the ArrayDisplay and subsequent Switch loop would skip it completely, but as soon as I added a second .rar, the code detected it and everything ran fine. Any ideas what might cause that?

Thanks again!

EDIT:

Also, the code above sends everything to the cmd without waiting for the previous process to finish.

Edited by yevlar

Share this post


Link to post
Share on other sites

yevlar,

Corrected the routine to get files, was dropping the last extention.

As far as processing files you can do something like this...

#include <array.au3>
#include <File.au3>

Local $filelist = _arraytostring(_FileListToArray(@DesktopDir)) ; get the filelist in string format
local $aEXT     =   stringregexp($filelist & '|','\.(\w+)\|',3) ; get array of extentions only
$aEXT           = _ArrayUnique($aEXT)                           ; strip all duplicate extentions

_arraydisplay($aEXT)

For $i = 1 To UBound($aEXT) - 1
    switch stringlower($aEXT[$i])
        case 'url'
            _do_files('url')
        case 'lnk'
            _do_files('lnk')
    endswitch
    Next


func _do_files($ext)
    local $aFiles = _FileListToArray(@desktopdir, '*.' & $ext)
    for $1 = 1 to $aFiles[0]
        ;
        ;
        ; this is where you would do your thing
        ;
        ;
        ConsoleWrite('Processing file = ' & $aFiles[$1] & @CRLF)
    Next
endfunc

You may be able to invoke the command processor with RunWait or ShellExecuteWait for each file, I have no idea.  You'll have to play with it.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
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
Sign in to follow this  
Followers 0