Solved

VBS Which Is A File Sorter By Character String

Posted on 2006-06-17
6
218 Views
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.
Regards.
0
Comment
Question by:asgarcymed
  • 3
  • 2
6 Comments
 
LVL 20

Expert Comment

by:alainbryden
Comment Utility
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.

Regards,
Alain
0
 

Author Comment

by:asgarcymed
Comment Utility
Command line is enough. I will try to write it, but I am a newbie yet...
Thanks.
Regards.
0
 
LVL 10

Expert Comment

by:fostejo
Comment Utility
asgarcymed,

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

cheers,
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:asgarcymed
Comment Utility
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.
Thanks.
Regards.
0
 
LVL 10

Accepted Solution

by:
fostejo earned 500 total points
Comment Utility
asgarcymed,

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':

OPTION EXPLICIT

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 '/'
intArgs=objArgs.Unnamed.Count

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

' 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!"
   WScript.Quit
END IF

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

'Clean up after ourselves..
SET objFSO = NOTHING
SET objArgs = NOTHING


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

Author Comment

by:asgarcymed
Comment Utility
fostejo - thank you a lot, you helped me much! I will post new questions while I study, my knowledge grows, and new doubts appear.
Regards.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

18 Experts available now in Live!

Get 1:1 Help Now