Solved VBS scripting error, attempting to learn VBS basics.

January 25, 2016 at 07:36:00
Specs: Windows 7
I am attempting to write a VBScript that will create a folder c:\Backups and then copy information from the current users Documents folder. This is what I have so far and I can not figure out why I am getting an error.

REM Prevents the accidental reuse of a declared variable
Option Explicit

REM DIM declares in memory
Dim objWShell, userName, objFso, Wshshell, strDocuments, strLocation

Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
Set objWShell = WScript.CreateObject("WScript.Shell")
Set WshShell = Wscript.CreateObject("WScript.Shell")

userName = objWShell.expandEnvironmentStrings("%userName%")

If Not objFso.FolderExists("C:\" + "Backups" + "\") Then
objFso.CreateFolder ("C:\" + "Backups" + "\")
End If

If Not objFso.FolderExists("C:\Backups\" + userName +"\") Then
objFso.CreateFolder ("C:\Backups\" + userName + "\")
End If

strDocuments = ("C:\Users\" + userName + "\" + "Documents"+ "\")
strLocation = ("C:\Backups\" + userName + "\")

If objFso.FolderExists (strDocuments) Then
objFso.CopyFolder strDocuments, strLocation, True
End If

Any assistance would be appreciated as I am attempting to learn VBS and this portion of it is proving a challenge.

See More: VBS scripting error, attempting to learn VBS basics.

Report •

January 25, 2016 at 08:07:50
What is the error message you receive? May help us pin point the error

Report •

January 25, 2016 at 08:10:09
Path not is successfully creating the folders but not copying the files. Not sure what I am doing wrong or if having subfolders in the main is a problem.

Report •

January 26, 2016 at 00:17:36
✔ Best Answer
Well i have tried it and think i have found the issue, basically i removed the

 + "\"
from the end of StrDocuments

strDocuments = ("C:\Users\" + userName + "\" + "Documents"+ "\") 


strDocuments = ("C:\Users\" + userName + "\" + "Documents") 

This seems to work for me..

Report •

Related Solutions

January 26, 2016 at 03:16:32
AlwaysWillingToLearn, I thank you very much. I have been wrestling with this for 2 days now and could not figure out what I had done wrong in the coding. It worked for me as well and i'm really appreciative.

Report •

January 26, 2016 at 03:26:28
new2coding, im glad it worked for you, it was one of those small error hiding in a bunch of code. Have fun coding and come back if you need help from the community.... :)

Report •

Ask Question