Sign in to follow this  
Followers 0
asgarcymed

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

3 posts in this topic

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.


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites



Try this:

#include <Array.au3>
$input_path = InputBox("Path...", "What is the target path?" & Chr(13) & "Example:" & Chr(13) & "C:\Directory", "", "", 250, 150)
Dim $extent
$i = 0
$search = FileFindFirstFile($input_path & "\" & "*.*")
While 1
    $file = FileFindNextFile($search)
    $size_bytes = FileGetSize($input_path & "\" & $file)
    $size_megas = $size_bytes / 1048576
    If $file = "" Then ExitLoop
    $ext = StringSplit($file, ".")
    $extent = $ext[$ext[0]]
    Select
        Case $size_megas > 0 And $size_megas <= 80
            FileMove($input_path & "\" & $file, $input_path & "\Small\" & $extent & "\", 8)
        Case $size_megas > 80 And $size_megas <= 170
            FileMove($input_path & "\" & $file, $input_path & "\Medium\" & $extent & "\", 8)
        Case $size_megas > 170
            FileMove($input_path & "\" & $file, $input_path & "\Big\" & $extent & "\", 8)
    EndSelect
    $i += 1
WEnd

MsgBox(0, "Finished!", $i & " files were moved; now exiting!")

Share this post


Link to post
Share on other sites

Absolutely perfect!!! Thank you a lot!!!

:P;):P;):)


MLMK - my blogging craziness...

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