15. VbScript | Copy, Move, Rename Files & Folder

Video Walkthrough:

Try using the code in order to make sure you understand.

So this tutorial is about copying, moving, and renaming files and folders using VbScript.

Below is the format of the script to copy and to move the files and folders. Note that the root command is CreateObject(“Scripting.FileSystemObject”):

You put the current address of the file/folder where it says “LOCATION”, and put the new address where you want to move or copy the file/folder where it says “NEW LOCATION”.

Copy File into Folder

For example, let’s say I want to copy my picture on the desktop to a new folder on the desktop

Cat into Folder

The address of either folder or pic can be found by right clicking either folder or picture and selecting properties. Find the location descriptor.

b2 b3

Insert the locations into the code:

Note that the picture ends with .jpg, and the ending location must end with a backslash \

Move Folder into Folder

Let’s say I want to move a folder into another folder.

1

Both are on the desktop “C:\Users\rock\Desktop”. So all you have to do is add the folder names.

Again, the ending location must end with a backslash \

Output:

b3

Rename File or Folder

Video Walkthrough:

Renaming a file or folder would use the move command, since the copy command causes there to be duplicates.

To rename a file, let the NEW LOCATION be a file with a different name.

To rename a folder, don’t add the backslash \ at the end of the NEW LOCATION and have a different name for the folder

Move or Copy Multiple Files into Folder

If you only want to move the all the files, but not the folder use *.* or *

If you only want to move all the files of a specific type use * plus the extension (i.e. *.txt or *.jpg)

If you only want to move all the files of a specific name, regardless of type, use * after the filename and period (ie. catpic.* or file.*)

Example: Check Before Copy/Move

To check if a folder or file exists before moving or copying a file/folder:

Remember that the placement of the backslash \ affects how the VbScript reads & runs the code.


What's Your Opinion?