Sign in to follow this  
Followers 0
asgarcymed

File Sorter By Character String

7 posts in this topic

File Sorter By Character String

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!!)...

I need a script to automatically organize my files by a criterion I specify (character string).

Something like old MS-DOS, where we can refer to a character string, using *string*. For exemple «move *java* Java\» will move files which name contains java to the Java folder.

I want to use SPACES in string, and use MULTIPLE strings (« move *all* + *night* C:\Music\VA\All Night » yes, this is an incorrect syntax, but you get the idea of using multiple strings).

More examples from MS-DOS (correct syntax) :

move "Java for Newbies.pdf" "Java Newbies

move "*java*.pdf" "Mastering Java

move "*all night*" "C:\Music\VA\All Night"

for /f "delims=" %a in ('dir *all* /b ^| findstr /i "night"') do move "%a" "All Night

This is very useful to organize my eMule's downloads (all files are mixed inside the same Incoming folder, what is really very chaotic!).

It should have a "preview mode" (to see the results before moving - something like «dir *string*» command, at first; and «move *string* string\» thereafter ).

The best I could do (only one string):

$move_files = FileMove (@ScriptDir & "\*string*", @ScriptDir & "\string\", 8)

If $move_files = 1 Then

MsgBox (0, "Everything OK!", "Files were successfully moved!")

Else

MsgBox (0, "Damn!", "Something went wrong!")

EndIf

Thanks in advance.

Best regards.


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites



Example:

#include <File.au3>
Dim $path = "c:\work"
$FileArray = _FileListToArray($path, "*java*.pdf")

For $i = 1 To $FileArray[0]
    If StringInStr($FileArray[$i], "newbie") Then
        FileMove($path &"\"& $FileArray[$i], "d:\Java Newbies\", 8)
    Else
        FileMove($path &"\"& $FileArray[$i], "d:\Mastering Java\", 8)
    EndIf
Next

Share this post


Link to post
Share on other sites

Thank you!

When I run the script you posted, I get an error on line 4:

For $i = 1 To $FileArray[0]

Subscript used with non-Array variable.:

For $i = 1 To $FileArray[0]

For $i = 1 To $FileArray^ ERROR

>Exit code: 1 Time: 0.363

Suggestions?

Thanks in advance.

Best regards.


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites

add this line after $FileArray = _FileListToArray($path, "*java*.pdf")

if @error Then

MsgBox(0,"","NO FILE IN DIR: "&$path)

Exit

EndIf

you don't have a files in directory:"c:\work" --> Dim $path = "c:\work"

Share this post


Link to post
Share on other sites

CODE
#include <File.au3>

Dim $path = "c:\work"

$FileArray = _FileListToArray($path, "*java*.pdf")

if @error Then

MsgBox(0,"","NO FILE IN DIR: "&$path)

Exit

EndIf

For $i = 1 To $FileArray[0]

If StringInStr($FileArray[$i], "newbie") Then

FileMove($path &"\"& $FileArray[$i], "d:\Java Newbies\", 8)

Else

FileMove($path &"\"& $FileArray[$i], "d:\Mastering Java\", 8)

EndIf

Next

This script always show the MsgBox(0,"","NO FILE IN DIR: "&$path), but it is not true! I have files inside C:\Work\ such as "Java for Newbies.pdf" and "Mastering Java.pdf".

Can you debug?

Thanks in advance.

Best regards.


MLMK - my blogging craziness...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

the script work in my pc

sorry but are you sure that the directory (c:\work) exist or the files are in the directory?

the first time script move files in the new directory but the second time return MsgBox(0,"","NO FILE IN DIR: "&$path) because the files are moved to new directory

try this:

#include <File.au3>
Dim $path = "c:\work"

If FileExists($path) Then MsgBox(0,'','Exist '&$path)

$FileArray = _FileListToArray($path, "*java*.pdf")
if @error Then
MsgBox(0,"","NO FILE IN DIR: "&$path)
Exit
EndIf
For $i = 1 To $FileArray[0]
If StringInStr($FileArray[$i], "newbie") Then
FileMove($path &"\"& $FileArray[$i], "c:\work\Java Newbies\", 8)
Else
FileMove($path &"\"& $FileArray[$i], "c:\work\Mastering Java\", 8)
EndIf
Next
Edited by silvano

Share this post


Link to post
Share on other sites

Now it worked fine; thank you!

I would like you to give a look at:

http://www.autoitscript.com/forum/index.ph...mp;#entry412496

I need to use a always different and customized combination of filters/subfilters... I need to deeply learn this procedures/functions because they are very important to organize the eMule's downloads folder....

I really need to learn how to easy use many filters at once ($filter = rule1 AND rule2 AND rule3 AND rule4)... I am confused how to do it...


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