[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3270
  • Last Modified:

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.
0
asgarcymed
Asked:
asgarcymed
  • 2
  • 2
2 Solutions
 
nayernaguibCommented:
I think you need to repeat the second block (which moves files acoording to extension) 3 times, one for each directory created by the first block. Don't forget to replace the statement:

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

at the beginning of each of the three blocks, with the following statements (one for each copy):

  $input_path = $input_path & "\Small"
  $input_path = $input_path & "\Medium"
  $input_path = $input_path & "\Big"

_______________

  Nayer Naguib
0
 
gimosubyCommented:
Good morning,
The following script should do it:
--------------------------------------------------------------------------------------------------

$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 = "" ; Now that moving by size is done, perform moving by extension
                  Dim $Folders[3]
                  $Folders[0] = "\Small\"
                  $Folders[1] = "\Medium\"
                  $Folders[2] = "\Big\"
                  FOR $CurrentFolder IN $Folders
                        $search = FileFindFirstFile ($input_path & $CurrentFolder & "*.*")
                        While $search > 0
                              $file = FileFindNextFile ($search)
                              If @error Then ExitLoop
                              $extension = StringRight ($file, 3)
                              If FileGetAttrib ( $input_path & $CurrentFolder & $file ) <> "D" Then
                                    FileMove ($input_path & $CurrentFolder & $file, $input_path & $CurrentFolder & $extension & "\", 8)
                              EndIf
                        WEnd
                  NEXT            
            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

--------------------------------------------------------------------------------------------------

Good Luck!
0
 
asgarcymedAuthor Commented:
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!!
0
 
gimosubyCommented:
Anytime.
Thanks for the points!
0
 
nayernaguibCommented:
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now