top of page

Auto-Group files to folder using VB6

I'm sure you've seen a lot of tutorials out there, but rarely do you see how to apply those techniques in the real world so today we're going to look at how you can solve a problem (or requirement) using a certain technique.

Our problem or requirement in this case is you have a folder with lots of different files types.

For this demo, I just created a folder with 4 file types:

A folder with different file types

As for the technique, we will use the Scripting Runtime.

So, let's start the project:

Open up Visual Basic 6.0 and create a new project.

Next, add a reference to "Microsoft Scripting Runtime":

Referencing Microsoft Scripting Runtime

Then, we'll create the main routine (or method) of grouping the files and call it from the Form_Load event.

You may follow the codes below:

The method "GroupFilesByType" takes in one (1) parameter which is the folder that we want to group files from.

Most of the statements are self-explanatory with minor code comments but feel free to post your questions on this blog.

 

We're using a SUB routine to encapsulate the steps needed to group the files so we can

easily reuse it on other projects, etc.

 

Here's the output on the Immediate Window when you run the codes:

And here's the actual result on the files:

The folders were created based on the file extensions and every file of type gets moved to the right folder.

By the way, if you don't see the Immediate Window, you may show it via this menu:

One more thing...

You might notice the function "stringFormat" was used there.

Yes this is VB6, not VB.Net :)

I implemented that functionality cause it's better than concatenating strings.

If you want the code for that, just let me know on the comments below.

Thanks!

bottom of page