MGMorrison
MGMorrison

Reputation: 1

VBS Script to locate all files of certain extensions and copy them to a specific destination

I'm making a project out of creating a script to use at work to automate one of our processes.

I'd like the script to check an input for username to search the specified user profile path for any files of .doc,.docx,.pdf,.pst ect. and copy them as is to a created folder on a network drive location.

My main question is what is the command or chain of commands to check folders and sub folders starting at the specified userpath, for JUST files with those extensions and I guess copy them but without getting to a situation where it just copies the same file over and over and over again. Sorry if that's confusing.

Upvotes: 0

Views: 7749

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

This answer provides sample code for recursively traversing a folder tree. A list of extensions could be handled by creating a dictionary:

Set extensions = CreateObject("Scripting.Dictionary")
extensions.CompareMode = vbTextCompare  'case-insensitive
extensions.Add "doc", True
extensions.Add "docx", True
extensions.Add "pdf", True
extensions.Add "pst", True
...

and then checking the extension of the processed files like this:

For Each f In fldr.Files
  If extensions.Exists(objFso.GetExtensionName(f.Name)) Then
    f.Copy targetFolder & "\"
  End If
Next

The trailing backslash is required when the destination is a folder, otherwise you'd have to specify the full target path including the target filename.

Upvotes: 2

Sudhir
Sudhir

Reputation: 1472

I think I have understood most of the requirements, and this can be more easily achieved by using a .BAT file approach within windows. This batch (.Bat) file can run commands such as copy / delete etc.

So create a file called test.bat, and inside the file add the below script:

::XCOPY source [destination]
XCOPY "C:\Temp\*.doc" "C:\Temp\another"

What does this do? Well it uses an XCOPY Command to copy any files within the C:\Temp direcory which have a .doc extension. The files will be copied over to a folder called C:\Temp\another.

The XCOPY takes two primary arguments: source and destination. Source is where the file currently lives, and destination is where you want to copy the files to. More info of all of the options available can be found on:

http://support.microsoft.com/kb/240268

In order to run the file, just double click it, or schedule it to run whenever required.

Let me know if this meets your requirement, I didn't fully understand the bit about an input for a username?

Upvotes: 0

Related Questions