VBS Which Is A File Sorter By Character String

Posted on 2006-06-17
Last Modified: 2010-04-07
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.
Question by:asgarcymed
  • 3
  • 2
LVL 20

Expert Comment

ID: 16927790
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.


Author Comment

ID: 16929057
Command line is enough. I will try to write it, but I am a newbie yet...
LVL 10

Expert Comment

ID: 16929248

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Author Comment

ID: 16929352
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.
LVL 10

Accepted Solution

fostejo earned 500 total points
ID: 16930417

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 - you'll find them very useful it you're learning VBScript..

hope that helps ...

Author Comment

ID: 16930478
fostejo - thank you a lot, you helped me much! I will post new questions while I study, my knowledge grows, and new doubts appear.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now