Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change the path of the data source in my Excel pivot table with macro 4 47
using Access 8 75
Can we place a tooltip on the actual vb6 form 5 48
Child Form in front 4 49
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

839 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