VBS Which Is A File Sorter By Character String

I need a VBS 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 would be very good to have a "preview mode" (to see the results before moving - something like «dir *string*» command, at first; and «move *string* string\» thereafter ).

A great thank you in advance.
Who is Participating?
fostejoConnect With a Mentor Commented:

Have a look at the following; I've purposely kept it as simple as possible and commented where I can.  Basically it allows you to type a command in the form of 'Movefiles <filespec1>...<filespecN> <Destination Directory>'   - for instance;

Movefiles *.pap patterns*.* pap.lic "Paint Artist*.*" c:\downloads\PaintArtistPro

Note the following caveats!
1. The destination directory should always be the last parameter
2. File specifications with spaces should be enclosed with double quotes
3. Error/Sanity checking is minimal - the script checks for prior existence of the destination folder and for at least two parameters.
4. There is no 'List' capability - this is possible, but would make the script much longer as there's no native way in VBScript to acheive it.
5. There is no status output

Save the following as 'Movefiles.vbs':


DIM intArgs, intLoop
DIM objFSO, objArgs
DIM strDestination

SET objFSO = CreateObject("Scripting.FileSystemObject")

'Create object to represent command line parameters
SET objArgs = WScript.Arguments

'Set intArgs to the number of parameters not preceded by a '/'

' We must specify at least one source filespec and the destination folder
IF intArgs < 2 THEN
    WScript.Echo "Syntax: MoveFiles <filespec>...<filespec> <destination folder>"

' Add a backslash onto the end if the destination folder so MoveFile knows its a directory.
strDestination=objArgs.Unnamed(intArgs-1) & "\"

' Check the destination foler already exists..
IF NOT objFSO.FolderExists(strDestination) THEN
   WScript.Echo"Warning: Destination folder '" & strDestination & "' Doesn't exist!"

' As we're 'zero' based, the last parameter (intArgs-1) is actually the Destination Folder
FOR intLoop = 0 to intArgs-2
    objFSO.MoveFile objArgs.Unnamed(intLoop), strDestination

'Clean up after ourselves..

As the script is moving files, I'd strongly recommend that any testing is done is a 'test' subdirectory with a few test files away from your live files - just to be on the safe side!

Also, I'd suggest having a look at the VBScript documentation help files from http://www.microsoft.com/downloads/details.aspx?familyid=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en - you'll find them very useful it you're learning VBScript..

hope that helps ...
Wow you want us to write the whole program for you? Don't people usually get contracted for something like that? What if you gave it an attempt and posted your progress here so that we could help you out. First, is this something you want running from a user interface or a command line? If you want interface first you'll need to make a form with your farious inputs commands and so on, and then create your submit request routine. Once you've prepared all that, it's be no problem helping you write the script that will parse the command and move the files for you.

asgarcymedAuthor Commented:
Command line is enough. I will try to write it, but I am a newbie yet...
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


It sounds like you've already got 85% of a solution here .. by using the DOS Move command - the only thing that it doesn't do natively, is action a move on multiple source file specifications - so you couldn't do 'Move *.bin *vmo* "fred bloggs.txt" c:\emule\down\stuff' for instance.

To get around that limitation you could wrap a batch/command file around it to pick up the various parameters and call the Move command itself with each one - so you effectively end up with, in this case, executing three seperate commands similar to;

Move *.bin c:\emule\down\stuff
Move *vmo* c:\emule\down\stuff
Move "fred bloggs.txt" c:\emule\down\stuff

The same logic applies on the VBScript side, utilizing the .MoveFile method for instance which can also handle wildcards and works in a similar fashion to the DOS Move command.

Does this sound like it's on the right lines? (or am I missing something fundemental?!)- If so, I'm sure we can provide a example DOS/VBScript..

asgarcymedAuthor Commented:
fostejo - thank you a lot! You understood everything perfectly! The main goal is to move files according to more than one string for each file. For example : I specify strings => aaaa+bbbb+cccc => This would move the file “This is an Example of Multiple Strings aaaa bbbb cccc.ext”; but would not move the file “This is other Example of Multiple Strings aaaa bbbb.ext” (because cccc string is missing). Got the idea ?
My problem is that I know little about VBS, yet (I am now beginning from 0).That is the reason why I asked you to make the entire script (I guess it is not a too much big or complex one, and I can pay attention to each line of code, and learn with it).
By now, a command-line VBS is enough. Maybe therefore I will aim to make a new one, with an easy GUI.
asgarcymedAuthor Commented:
fostejo - thank you a lot, you helped me much! I will post new questions while I study, my knowledge grows, and new doubts appear.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.