Jump to content

Exit loop


Go to solution Solved by david1337,

Recommended Posts

Hi guys

I got help creating this little script that moves all content (folders and files) from A to B

It works perfect, but I just realized that it never exits the loop.

So anything below the "WEnd" is not being processed.

Does anyone have a suggestion?

Thanks

$source = 'C:\folderA'
$dest = 'C:\folderB'


FileMove($source & "\*.*", $dest, 1) ;Move all source files first
$hSearch = FileFindFirstFile($Source & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
$hFilename = FileFindNextFile($hSearch)
if @ERROR then exitloop ;No more files
DirMove($source & "\" & $hFilename, $Dest,1);move subdir and all contents to new location
WEnd
Link to comment
Share on other sites

david1337,

The loop does have an exit point.  Please read the Help file for FileFindNextFile.

kylomas

 

Hi kylomas

Thanks for answering.

Yeah it does have an exit point, but it's not working as expected.

I'm trying to figure out why. Okay I will read about FileFindNextFile and see if that helps me in any way.

Link to comment
Share on other sites

it has an exit loop on error or carnt find file

if you dont want it to loop

remove wend

or add

exitloop befor wend

i hope this helps

when do you need the script to exit

what the problem with this script

Link to comment
Share on other sites

david1337,

My fault, I misunderstood you to mean that there was no exit point.

I think your FileMove is wrong.  Does'nt operand 2 have to be a folder followed by a filename/pattern?

kylomas

edit: I know this doesn't answer your question but is something that is wrong.

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

Link to comment
Share on other sites

david1337,

The following works on my system with the dir struct setup in the code.  See the comments for FileMove...

$source = 'K:\TestA'
$dest = 'K:\TestB'

; create test files--------------------------------------
for $1 = 1 to 3
    filewrite($source & '\File # ' & $1,$1)
Next
dircreate($source & '\TestASubA')
for $1 = 1 to 3
    filewrite($source & '\TestASubA' & '\File # ' & $1,$1)
Next
; -------------------------------------------------------

FileMove($source & "\*.*", $dest & '\*.*', 9) ;   <  ----- dest need file pattern and changed to create dir if not present
$hSearch = FileFindFirstFile($source & "\*.*") ;Now find any remaining (in this case: folders)
If $hSearch = -1 Then Exit msgbox(0,'','No files') ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    If @error Then ExitLoop ;No more files
    DirMove($source & "\" & $hFilename, $dest, 1);move subdir and all contents to new location
WEnd
ConsoleWrite('Done' & @CRLF)

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

Link to comment
Share on other sites

david1337,

The following works on my system with the dir struct setup in the code.  See the comments for FileMove...

$source = 'K:\TestA'
$dest = 'K:\TestB'

; create test files--------------------------------------
for $1 = 1 to 3
    filewrite($source & '\File # ' & $1,$1)
Next
dircreate($source & '\TestASubA')
for $1 = 1 to 3
    filewrite($source & '\TestASubA' & '\File # ' & $1,$1)
Next
; -------------------------------------------------------

FileMove($source & "\*.*", $dest & '\*.*', 9) ;   <  ----- dest need file pattern and changed to create dir if not present
$hSearch = FileFindFirstFile($source & "\*.*") ;Now find any remaining (in this case: folders)
If $hSearch = -1 Then Exit msgbox(0,'','No files') ;No folders
While 1
    $hFilename = FileFindNextFile($hSearch)
    If @error Then ExitLoop ;No more files
    DirMove($source & "\" & $hFilename, $dest, 1);move subdir and all contents to new location
WEnd
ConsoleWrite('Done' & @CRLF)

kylomas

 

I will test it right away! :)

Link to comment
Share on other sites

it has an exit loop on error or carnt find file

if you dont want it to loop

remove wend

or add

exitloop befor wend

i hope this helps

when do you need the script to exit

what the problem with this script

 

Hi fbar

All I want it to do is to move all content from folderA to folderB, and then continue the script.

So I want to be able to put some code in under the existing code.

Link to comment
Share on other sites

Okay I did some testing and found out exactly what the problem was.

It's only when there is no content in folderA (when the script is executed), that it doesn't continue the script after the WEnd statement.

Does this help anyone?

$source = 'C:\folderA'
$dest = 'C:\folderB'


FileMove($source & "\*.*", $dest, 1) ;Move all source files first
$hSearch = FileFindFirstFile($Source & "\*.*") ;Now find any remaining (in this case: folders)
if $hSearch = -1 then exit ;No folders
While 1
$hFilename = FileFindNextFile($hSearch)
if @ERROR then exitloop ;No more files
DirMove($source & "\" & $hFilename, $Dest,1);move subdir and all contents to new location
WEnd
Edited by david1337
Link to comment
Share on other sites

  • Solution

I managed to solve this pretty easy.

I just used this script instead. No loops to worry about.

#include <Process.au3>

_RunDOS("xcopy C:\folderA C:\folderB /E /I") ;Copies ALL content from "folderA" to "folderB"

_RunDOS ("rmdir /s /q C:\Test\folderA") ;Deletes source folder + all content

DirCreate("C:\Test\folderA") ;Creates source folder again



; So basically what this script does is to move all content from folderA to folderB
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...