Link to home
Start Free TrialLog in
Avatar of asgarcymed
asgarcymedFlag for Portugal

asked on

AutoIt - Automating file moving, first according to its size, and thereafter according to its extension...

I need a Script to automate file moving, first according to its size, and thereafter according to its extension...

I am an absolute beginner about Programming in General and I am starting to learn programming by learning AutoIt (which seems to be superb!! - easy to learn, yet very powerful!!)...

First,  I need a script to automate file moving according to its size. Inside a directory I have many files with different sizes. I need to automate the following actions:

1) Create "Big", "Medium", "Small" directories (such as DOS' «md Big», «md Medium», «md Small»)

2) Get file sizes

3) IF size<= 80 MB THEN move files to "Small" directory
    IF 81<size< 170 MB THEN move files to "Medium" directory
    IF size>=171  MB THEN move files to "Big" directory



I could make the following script "Move by Size.au3":


$input_path = InputBox ("Path...", "What is the target path?" & Chr(13) & "Example:" & Chr(13) & "C:\Directory", "", "", 250, 150)

$search = FileFindFirstFile ($input_path & "\" & "*.*")
While 1
    $file = FileFindNextFile ($search)
      $size_bytes = FileGetSize ($input_path & "\" & $file)
      $size_megas = $size_bytes / 1048576
      Select
      Case $file = ""
            MsgBox (0, "Finished!", "The files were moved; now exiting!")
            ExitLoop
      Case $size_megas > 0 And $size_megas <= 80
            FileMove ($input_path & "\" & $file, $input_path & "\Small\", 8)
      Case $size_megas > 80  And $size_megas <= 170
            FileMove ($input_path & "\" & $file, $input_path & "\Medium\", 8)
      Case $size_megas > 170
            FileMove ($input_path & "\" & $file, $input_path & "\Big\", 8)
      EndSelect
WEnd
Exit


This script worked fine, but maybe it is not the most correct.... Is it?...


Next step - make another script to automate file moving according to its extension. Inside a directory I have many files with different extensions. I need to automate the following actions:

1) Get all different files' extensions inside a directory (example: .pdf / .doc / .abw / .rtf)

2) Create a directory for each extension with extension's name
 (example, like DOS Batch (.BAT) files:
 md pdf
 md doc
 md abw
 md rtf)

3) Move each file to inside the folder with its extension
 (example, like DOS Batch (.BAT) files:
 (move *.pdf pdf\
move *.doc doc\
move *.abw abw\
move *.rtf rtf\)

Note - I assume that all files have am extension which corresponds to the 3 last right characters of their name -- StringRight ("name.ext", 3)


I could make the following script "Move by Extension.au3":


$input_path = InputBox ("Path...", "What is the target path?" & Chr(13) & "Example:" & Chr(13) & "C:\Directory", "", "", 250, 150)

$search = FileFindFirstFile ($input_path & "\" & "*.*")

While $search = 1
 $file = FileFindNextFile ($search)
 $extension = StringRight ($input_path & "\" & $file, 3)
If $file = "" Then
MsgBox (0, "Finished!", "The files were moved; now exiting!")
Exit
Else
FileMove ($input_path & "\" & $file, $input_path & "\" & $extension & "\", 8)
EndIf
WEnd


This script worked fine, but maybe it is not the most correct.... Is it?...


Now the big problem - I tried to make an one-only Script  <=> {1= "Move by Size.au3" + 2= "Move by Extension.au3" = "Move by Size and Thereafter by Extension. Such would make first the file moving according to its size, and thereafter according to extension)...

\Small\doc\

\Small\pdf\

\Small\zip\

\Medium\doc\

\Medium\pdf\

\Medium\zip\

\Big\doc\

\Big\pdf\

\Big\zip\


I made many attempts/trials but it did not work; I tried many things; I feel confused and lost... Please help me!...


Thanks in advance.
Best regards.
SOLUTION
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asgarcymed

ASKER

gimosuby - your solution is perfect! I was missing the concept of arrays!

nayernaguib - Without using arrays (what is worse!), it could work if  I put,  at the beginning of each of the 4 blocks,  (one for each copy):
$path1 = $input_path
$path2 = $input_path & "\Small"
$path3 = $input_path & "\Medium"
$path4 = $input_path & "\Big"
However, the source-code becomes bigger and confusing; the script takes more time to be run; and it overloads CPU while running....

Thank you very much!!
Avatar of gimosuby
gimosuby

Anytime.
Thanks for the points!
I just gave you the quick-and-dirty solution. I've never written an AutoIt script before. In fact, I've never seen the application! :)

And by the way, good catch! I forgot to store the initial path in a separate variable (or to create separate variable for each block). My code would generate something like C:\Directory\Small\Medium\Big!

_______________

  Nayer Naguib