Sign in to follow this  
Followers 0
jben

another loop problem

24 posts in this topic

#1 ·  Posted (edited)

Hi. Can someone help me with creating a loop. Never created one in VB before and feel that it may be required..

Basically the code below will be run about 10 times for different folders in the EXTRCT_TEMP directory. I know I could copy this code 10 times but it seems a bit excessive, therefore i'm thinking a loop is the way forward?...

The only item that will need to change through the loop is the FOLDERNAME which I have highlighted in BOLD. Thanks

$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\[b]FOLDERNAME[/b]", 0)
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)

        If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
Next

Update

-------

oops didn't seem to work..

This is the FOLDERNAME bit of the code I'm talking about $F_Array = __FldrListToArray("C:\EXTRCT_TEMP\FOLDERNAME", 0)

Edited by jben

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

As weaponx usually posts faster and always better, I'll make this a quick suggestion. Is there a reason you can't make the code a function, and call it when neccesary?

Or if not, wrap the above code in another For/Next loop?

Edited by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

I'm not sure...The function idea does sound good.. But i'm not an AutoIT expert so i'm a bit rubbish :-( .....but learning...

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here is the whole of the extraction code...Hope this make sense.. The code below will run for 10 different items.. My knowledge would enable me to just copy this 10 times over, but it seems a bit crazy to go about it in that way..

This is why i'm trying to figure out a way of declaring the folder names etc above and just calling the code below for each folder...Then it would be much easyier to read.

;------------------------------------------------------------------------------------------
; RENAMES CHILD ZIP
;------------------------------------------------------------------------------------------
$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\29944M_10386\", 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)

        If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
Next
;------------------------------------------------------------------------------------------
; EXTRACTS CHILD ZIP .NCP FILE
;------------------------------------------------------------------------------------------
$szZipFile = $targetDir &"DATA_CHILD.zip";this is the zip file to extract
$szDestDir = "C:\OUTPUT";this is the destination to extract the zip files

$aZipList = _ZipList2Array($szZipFile)
for $x =0 to UBound($aZipList)-1
    $file_type = StringLower(StringRight($aZipList[$x][1],4))
    If $file_type = ".ncp" Or $file_type = ".lcn" Then
      ConsoleWrite("Extracting:"&$aZipList[$x][1])
         $vResult = _ZipUnZipItem($szZipFile,$aZipList[$x][1],$szDestDir)
        ConsoleWrite(" -- "&_ZipFormatMessage($ZR_RECENT)&@lf)
        
    EndIf   
Next
;------------------------------------------------------------------------------------------
; MOVES FILES TO APPROPRIATE LOCATION.
;------------------------------------------------------------------------------------------
#include <date.au3>
Dim $date[3]=[@YEAR,@MON,@MDAY]
Local $sOutDir = "C:\OUTPUT\" & @YEAR & _WeekNumberISO() & "\Somerset_"& @YEAR & _WeekNumberISO()
If Not FileExists($sOutDir) Then DirCreate($sOutDir)
filemove ("C:\OUTPUT\MASTER\*.*", $sOutDir)
dirremove ("C:\OUTPUT\MASTER\")
Edited by jben

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I've come up with a loop idea...Does this seem feasable?..

$i = 0
Do
    $i = $i + 1
if $i=1 Then
        $folder=("29944M_10386\") 
    endif
if $i=2 Then
        $folder=("29897F_11234\") 
    endif
Until $i = 2
    
            $i2 = 0

#include <Array.au3>


$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\" & $folder, 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)

        If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
        Next

Next

Edited by jben

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I saw your do loop before you edited it that made even less sense :D but this is what I meant by making it a function

#include <Array.au3>
#include <File.au3>

Global $targetDir
Global $aZipList

$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\FOLDERNAME", 0)
$F_Array = _loop($F_Array)
_ArrayDisplay($F_Array)

Func _loop($F_Array)
    For $I = 1 To Ubound($F_Array) -1
    $targetDir = "C:\EXTRCT_TEMP\FOLDERNAME\" & $F_Array[$I] ;this code states that the targetdir is the information provided via the $F_Array
    ;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
    ;the string, which is then used in the code below to rename the .zip file.
    MsgBox(0, "", $targetDir)
    $aZipList = _FileListToArray($targetDir,"*.zip",0)
    ;If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
    Next
Return $aZipList
EndFunc

Once a function you can call it any number of times.

Also, I should add I haven't used FldrListToArray, but why doesn't Filelisttoarray flag 2 work?

PS I commented out the last line because I was testing to make sure I did things right not because there is anything wrong with it.

Edited by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

haha..I'm totally lost now..

Not sure what you mean by filelisttoarray flag 2 work?..

Not sure how that function would work..I see you have mentioned "FOLDERNAME" but that is the name of the folder that will be called in for 10 different folders..

C:\EXTRCT_TEMP\FOLDER1\

C:\EXTRCT_TEMP\FOLDER2\

C:\EXTRCT_TEMP\FOLDER3\

etc

If I can call in each folder then that would be ideal..Because the rest of the code would just work..As the other part of the code just renames the zip files in the folders..So when FOLDER1 has been called it will rename the .zip within this folder to DATA_CHILD.zip and so on...

Edited by jben

Share this post


Link to post
Share on other sites

OK... not that I'm sure I want to go down this road, but from the other stuff I've helped you with, you sure you need to rename the child zip? Anyway, read up on Func/EndFunc in the help file. You pass variables to functions the way I passed $F_Array. If you want to pass more then one you seperate them by a , and just make sure when you call the function you have each variable in the ()

for example

_loop($F_Array, $test1, $test2, $test3)

Func _loop($F_Array, $test1, $test2, $test3)

...

endfunc

make sense?


While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Do you mean like this:

and the reason I need to rename to CHILD_ZIP is because the name of the .zip file in the folder changes once a week..Therefore I felt by renaming the file it would enable me to extract the zip.

#include <Array.au3>
#include <File.au3>

Global $targetDir
Global $aZipList

;==========================================================
; THIS PROVIDES THE DETAILS TO THE FUNCTION
;==========================================================
_loop($F_Array, "\FOLDER1", "FOLDER2", "FOLDER3")

;==========================================================
;THIS IS THE FUNCTION
;==========================================================
Func _loop($F_Array, "\FOLDER1", "FOLDER2", "FOLDER3")
    For $I = 1 To Ubound($F_Array) -1
    $targetDir = "C:\EXTRCT_TEMP\" & $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
 ;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
 ;the string, which is then used in the code below to rename the .zip file.
    MsgBox(0, "", $targetDir)
    $aZipList = _FileListToArray($targetDir,"*.zip",0)
 ;If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
    Next
Return $aZipList
EndFunc
Edited by jben

Share this post


Link to post
Share on other sites

thinking about it...You've highlighted a good point..Theres not alot of point in me renaming the zip to DATA_CHILD as it doesn't matter if the file changes as I can just use a wildcard I guess?...

Not sure, trying to remember if theres any reason why I went about it this way.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I think I know why now...As far as I remember the AutoIT extraction plugin doesn't support wildcards...

update

------

actually i think i'm wrong...I think i've lost the plot haha

Edited by jben

Share this post


Link to post
Share on other sites

I'm still pretty new at this too but I do something similar in calling different department names.

CODE
$Links = FileOpen("departments.txt", 0)

; Check if file opened for reading OK

If $Links = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$oFrame = _IEFrameGetObjByName($oIE, "data")

$dept = FileReadLine($Links) ;file listing text of each hyperlink (fund name with space after it)

If @error = -1 Then ExitLoop

_IELinkClickByText($oFrame, $dept, 0, 0)

Sleep(3000)

_SaveTable() ;Call function that performs "Save As"

Sleep (200)

_IEAction($oIE, "back")

WEnd

FileClose($Links)

Departments.txt looks like

AFRICAN-AMERICAN

ART

ASIAN STUDIES

Could you do something similar with a file folder.txt being

C:\EXTRCT_TEMP\FOLDER1\

C:\EXTRCT_TEMP\FOLDER2\

C:\EXTRCT_TEMP\FOLDER3\

And so you would call $F_Array = __FldrListToArray($folder, 0)

Share this post


Link to post
Share on other sites

what do you mean by this

And so you would call $F_Array = __FldrListToArray($folder, 0)

thanks..I presume your code calls in the text via a .txt file...Looks like an interesting idea...I will try it..Just don't know what you mean by the quoted text above..

Share this post


Link to post
Share on other sites

I was thinking where I have

$Links = FileOpen("departments.txt", 0)

you would have

$Folder = FileOpen("folders.txt", 0)

So you would wrap your code in a while loop something like this

$Folder = FileOpen("Folders.txt", 0)
; Check if file opened for reading OK
If $Links = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
$F_Array = __FldrListToArray($folder, 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)

        If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
Next
WEnd

I couldn't find _FldrListsToArray in the help file to see if it would take a variable like that. Is it under User Functions or is it a Function you created?

Share this post


Link to post
Share on other sites

Thanks sue I'm going to try that...

I got the code from the forum, someone was helping me out...

thanks

Share this post


Link to post
Share on other sites

I found if I run the code it gets stuck..Doesn't seem to get to the point of renaming the file for some reason.Not sure why

$Folder = FileOpen (@scriptdir & "Folders.txt", 0)
; Check if file opened for reading OK
;If $Links = -1 Then
 ;  MsgBox(0, "Error", "Unable to open file.")
 ;  Exit
;EndIf
While 1
$F_Array = __FldrListToArray($folder, 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)

        If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
Next
WEnd

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

had to change this bit:

The code stated $links so i changed to $Folder

If $Folder = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit

Now I see the messagebox, so some sort of progress..

update

-------------

got past the msgbox but still doesn't work :-(

Edited by jben

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Heres my attempt with Sue's idea..Didn't seem to work...Sorry I didn't use your code someone, just couldnt figure it out..

Folders.txt

29944M_10386\

$Folder = FileOpen ("C:\Users\meeeee\Desktop\XTRACTOR\Folders.txt", 0)
If $Folder = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
While 1
$F_Array = __FldrListToArray("C:\EXTRCT_TEMP\" & $Folder, 0);the rest of this string is detected in the code below.
For $I = 1 To Ubound($F_Array) -1
$targetDir = $F_Array[$I];this code states that the targetdir is the information provided via the $F_Array
;the purpose in this is because the $F_Array obtains the folder within the 29944M_10386 folder to complete
;the string, which is then used in the code below to rename the .zip file.
$aZipList = _FileListToArray($targetDir,"*.zip",0)

        If IsArray($aZipList) Then FileMove($targetDir & $aZipList[1], $targetDir & "DATA_CHILD.zip");renames zip
        Next
        WEnd
Edited by jben

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

EDIT: Hey you may not care probably fine but I would take your full name out of the code

Well that code was failing because @scriptdir does not contain a trailing backslash.

But everytime I think I know what you are doing with your code I get confused again. Why would you have a for/next loop in a while/wend loop? Thats going to repeat ad-nauseum(sp??), and I doubt thats what you want. Plus in the above your not doing anything with the file you opened.

What really confuses me is you are moving from one method to another, and I'm not sure why at all. There are tons of ways to skin a cat; no sense in continuously trying a different method just because. From what I have seen, I would use func/endfunc for your renaming the child zip. That may be overkill, but again I'm confused why after you would need to rename the child zip at all. If its neccessary thats fine, but your branching in a lot of different directions that I'm not following.

Edited by someone

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd

Share this post


Link to post
Share on other sites

I'm going to get ahold of all my code and paste it here, maybe it will help..Sorry i'm confused myself :-(

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