Sign in to follow this  
Followers 0
trijames

Searching for files in subdirectories and moving them upward one directory

3 posts in this topic

#1 ·  Posted (edited)

Hello, I have the following directory structure:

stuff\
  script.au3
 
  \dir1\
    \archive1.rar
 
  \dir2\
    \archive2.rar
    \archive2b.rar
 
  \dir3\
    \archive3.zip

I want to move the archives (both rar and zip) up in the directory "stuff" where also the running script is.

This is the code I came up with but as I have written in the comments it can't find files and sets @error to 4 so archives are never found and moved, although it finds sub directories:

#include <File.au3>
$list = _FileListToArray(@ScriptDir, "*", 2)
If @error Then
MsgBox(16,"Error","Err "&@error)
Exit
EndIf
 
For $i = 1 To $list[0]
$path = @ScriptDir & "\" & $list[$i] & "\"
;MsgBox(0,"",$path) ; correctly displays the full path to the subdirectory ("C:\...\dir1\")
$i_list = _FileListToArray($path, "*.ZIP;*.RAR") ;for some reason can't find files and sets @error = 4
If Not @error Then ; @error = 4 : no file(s) found
  ; we never get here
  For $j = 1 to $i_list[0]
   FileMove($i_list[$j], "../")
  Next
EndIf
Next

AutoIt version is 3.3.6.1.

Any help would be greatly appreciated. Thank you

Edited by trijames

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can only search for one extension at a time, and it would have copied the files to the parent folder of Stuff, not to the stuff folder, so I modified the filemove destination also. I would make it a function and pass the extension you want to search for and call the function for each extension.

#include &lt;File.au3&gt;
$list = _FileListToArray(@ScriptDir, &quot;*&quot;, 2)
If @error Then
MsgBox(16,&quot;Error&quot;,&quot;Err &quot;&amp;@error)
Exit
EndIf
For $i = 1 To $list[0]
$path = @ScriptDir &amp; &quot;\&quot; &amp; $list[$i] &amp; &quot;\&quot;
$i_list = _FileListToArray($path, &quot;*.RAR&quot;) ;for some reason can't find files and sets @error = 4
if Not @error Then ; @error = 4 : no file(s) found
  For $j = 1 to $i_list[0]
   FileMove($path &amp; &quot;\&quot; &amp; $i_list[$j], &quot;.\&quot;)
  Next
EndIf
Next
Edited by IcemanND

Share this post


Link to post
Share on other sites

Thank you IcemanND but it didn't solve the problem. I was still getting the same result even with the changes that you have suggested. I had found the "*.RAR;*.ZIP" somewhere in the documentation and I assumed that it worked with _FileListToArray() too.

Anyway I have changed something and it is working perfectly: the first parameter used in calling FileMove was wrong too and something else.

I didn't feel like making a function so i declared an array of file extensions and cycle it.

This is the working code if anyone cares:

#include <File.au3>
$list = _FileListToArray(@ScriptDir, "*", 2)
If @error Then
Exit
EndIf
Global $exts[2] = ["*.rar","*.zip"]
For $x = 0 To UBound($exts)-1
$ext = $exts[$x]
For $i = 1 To $list[0]
  $i_list = _FileListToArray(@ScriptDir & "\" & $list[$i] & "\", $ext)
  If Not @error Then ; @error = 4 : no file(s) found
   For $j = 1 to $i_list[0]
    FileMove($list[$i]&"\"&$i_list[$j], @ScriptDir)
   Next
  EndIf
Next
Next

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