Jump to content

For Loop issue


Ram
 Share

Recommended Posts

Hi,

Here is what I am trying to achieve. I would like to copy source file (mp3) to particular folder. The source folder has many folders in it. I would like to copy one by file each in a loop until end of file in all the folders.

Eg: Source folder is C:\Music\ and many subfolders like MJ, Jo, WS etc.. now I would like to get one mp3 file from MJ folder copy it to destination folder which is same all the time (c:\Copy\) once the first file is copied from MJ I would like to copy one mp3 file from Jo folder and copy to the same destination as above. so keep doing the same until the end of file in all the folder. How do I go about doing it. I have tried the below:

#Include <File.au3>

$Copy = "C:\Temp\"

While 1
If FileExists("C:\Music\MJ\") Then
$FileList = _FileListToArray("C:\Music\MJ\", "*.mp3", 1);*.mp3 files only added to array
    If Not @error Then
      For $i = 1 To $FileList[1]
         FileCopy ("C:\Music\MJ\" & $FileList[$i], $copy & $FileList[$i]) 
         Next
       EndIf

Else
    
    If FileExists("C:\Music\Jo\") Then
      $FileList = _FileListToArray("C:\Music\JO\", "*.mp3", 1);*.mp3 files only added to array
        If Not @error Then
          For $i = 1 To $FileList[1]
            FileCopy ("C:\Music\JO\" & $FileList[$i], $copy & $FileList[$i]) 
          Next
       EndIf
 
EndIf
EndIf
WEnd

According above code I thought this will just move one file each but it is actually not doing anything.

Can you please help me to solve this issue?

Awaiting your response!

Thanks!

Edited by Ram
Link to comment
Share on other sites

At a glance, try

For $i = 1 To $FileList[0]

_FileListToArray help file

Yep. I know that..using For $i = 1 TO $fileList [0] just copies each file in MJ first and then copies each file in JO and then each file in WS.. I don't want it that way. something different.

I have two folders..

temp and copy.

The temp has many subfolders. MJ, Jo, WS (Subfolders have many mp3 files in each folder)

Now I would like to copy first mp3 file from Mj and copy to folder "copy" and it should automatically copy first mp3 file from Jo and copy to folder "copy" and then it should automatically copy first mp3 file from WS and and copy to folder "copy". this is should happened until the end of file in each folder.

I hope this help for giving some solution for the problem ?

Awaiting your response!

Thanks!

Link to comment
Share on other sites

OK something like this

#Include <File.au3>
$Copy = "C:\Temp\"
$SrcDir = "C:\Music\"
$aFileList = _FileListToArray($SrcDir, "*", 2)
If Not @error Then
    For $i = 1 To $aFileList[0]
        $aMp3 = _FileListToArray($SrcDir & $aFileList[$i], "*.mp3", 1)
        If Not @error Then FileCopy($SrcDir & $aFileList[$i] & "\" & $aMp3[1], $Copy)
    Next
Else
    If @error = 4 Then MsgBox(262144, "", "No Folder(s) Found")
    ConsoleWrite("_FileListToArray error=" & @error & @CRLF)
EndIf
Edited by picaxe
Link to comment
Share on other sites

OK something like this

#Include <File.au3>
$Copy = "C:\Temp\"
$SrcDir = "C:\Music\"
$aFileList = _FileListToArray($SrcDir, "*", 2)
If Not @error Then
    For $i = 1 To $aFileList[0]
        $aMp3 = _FileListToArray($SrcDir & $aFileList[$i], "*.mp3", 1)
        If Not @error Then FileCopy($SrcDir & $aFileList[$i] & "\" & $aMp3[1], $Copy)
    Next
Else
    If @error = 4 Then MsgBox(262144, "", "No Folder(s) Found")
    ConsoleWrite("_FileListToArray error=" & @error & @CRLF)
EndIf
I tried this but nothing happened...I excuted the code immediately it said completed. I am not sure why it is doing it that way?
Link to comment
Share on other sites

I misread your reply, so the previous script was looking for mp3's in subfolders of "c:\music" and not "c:\temp". Try this, it copies the first mp3 in each subfolder of c:\temp to folder "c:\mp3 copy".

#Include <File.au3>
$SrcDir = "C:\Temp\"
$DestDir = "C:\Mp3 Copy"
If Not FileExists($DestDir) Then DirCreate($DestDir)
$aFileList = _FileListToArray($SrcDir, "*", 2)
If Not @error Then
    For $i = 1 To $aFileList[0]
        $aMp3 = _FileListToArray($SrcDir & $aFileList[$i], "*.mp3", 1)
        If Not @error Then FileCopy($SrcDir & $aFileList[$i] & "\" & $aMp3[1], $DestDir)
    Next
Else
    If @error = 4 Then MsgBox(262144, "", "No Folder(s) Found")
    ConsoleWrite("_FileListToArray error=" & @error & @CRLF)
EndIf
Edited by picaxe
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...