Link to home
Start Free TrialLog in
Avatar of SimplyGeeky
SimplyGeekyFlag for United States of America

asked on

VBS CopyFolder "path not found" error

I'm working on a script to backup servers running under Microsoft Virtual Server, modifying a script posted at redmongmag.com (http://redmondmag.com/columns/print.asp?EditorialsID=2324)

VS needs to be backed up using Volume Shadow Copy methods to get a proper backup.

I have a batch file running that does basic file system stuff like rotate the backups (I want to keep 14 days' worth) first because I already had a batch file that did that on other servers. After the rotate, it calls the attached VBScript with
wscript vsbackup.vbs HOSTNAME DOW

Where HOSTNAME is the name of the folder (just the last part, e.g., "MYSERVER") that contains the VS files and DOW is a 3-character day of the week (Mon Tue Wed, etc.)

The script works up to the line
objFSO.CopyFolder strSource , strBackupDir, TRUE

At which point it throws a PathNotFound error. I tried echo'ing the paths strSource and strBackupDir and they both seem to exist. I could even run copy /y from a command prompt. I don't know much about VBS (Ok, anything really) so I don't know how CopyFolder works. If it really works the same as the built-in copy or xcopy commands, then I'll just write a batch file to do that and have the VBS file call that batch file, unless someone here has any better ideas.
'by Chris Wolf, http://redmondmag.com/columns/print.asp?EditorialsID=2324
 
Set objShell = CreateObject ("WScript.Shell")
 
ServerName = WScript.Arguments.Item(0)
dow = WScript.Arguments.Item(1)
 
' Backup target folder or UNC path
strBackupDir = "D:\Backup\Local\VS\" & ServerName & "\" & dow & ".0\"
 
'Line 10: Drive containing Virtual Machines
strVMdrive = "D:" 
 
'VM folder path
'if you don't specify a particular VM, all found in subfolders will be backed up!
strVMfolder = "VirtualMachines\" & ServerName & "\"
 
'available drive letter used to mount shadow copy
strTempDrive = "Q:"
 
'Line 20: Prepare shadow copy commands
sExCmd = "CreateVSS.cmd"
Set oFileSys = CreateObject("Scripting.FileSystemObject")
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)
 
'create backup folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
if Not(oFileSys.FolderExists(strBackupDir)) then Set objFolder = objFSO.CreateFolder(strBackupDir)
 
'Line 30: Create Shadow copy of VM drive
oExCmd.WriteLine "vshadow.exe -script=setvar1.cmd -p " &_
  strVMdrive
oExCmd.WriteLine "call setvar1.cmd"
oExCmd.WriteLine "vshadow.exe -el=%SHADOW_ID_1%," &_
  strTempDrive
oExCmd.Close
Result = objShell.run(sExCmd,vbMinimized, TRUE)
 
'Line 39: Copy the virtual machine files from the shadow copy
strSource = strTempDrive & "\" & strVMfolder
objFSO.CopyFolder strSource , strBackupDir, TRUE
 
 
' Delete created shadow copy instance
if oFileSys.FileExists(sExCmd) then oFileSys.DeleteFile(sExCmd)
set oExCmd = oFileSys.CreateTextFile(sExCmd, CopyOverwrite)
oExCmd.WriteLine "Echo y | vshadow.exe -da"
oExCmd.Close
Result = objShell.run(sExCmd,vbMinimized, TRUE)
 
'Backup complete!
wscript.echo("Backup complete!")

Open in new window

Avatar of sungenwang
sungenwang
Flag of United States of America image

Make sure the source path exist...

This is from Script56.chm, downloadable from Microsoft scripting:

CopyFolder Method
See Also
CopyFile Method | Copy Method | CreateFolder Method | DeleteFolder Method | MoveFolder Method
Applies To: FileSystemObject Object
Language

          o JScript
          o VBScript
          o Show All

Recursively copies a folder from one location to another.
object.CopyFolder ( source, destination[, overwrite] );
Arguments
object

      Required. Always the name of a FileSystemObject.

source

      Required. Character string folder specification, which can include wildcard characters, for one or more folders to be copied.

destination

      Required. Character string destination where the folder and subfolders from source are to be copied. Wildcard characters are not allowed.

overwrite

      Optional. Boolean value that indicates if existing folders are to be overwritten. If true, files are overwritten; if false, they are not. The default is true.

Remarks
Wildcard characters can only be used in the last path component of the source argument. For example, you can use:
[JScript]
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFolder ("c:\\mydocuments\\letters\\*", "c:\\tempfolder\\")
[VBScript]
FileSystemObject.CopyFolder "c:\mydocuments\letters\*", "c:\tempfolder\"
But you cannot use:
[JScript]
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFolder ("c:\\mydocuments\\*\\*", "c:\\tempfolder\\")
[VBScript]
FileSystemObject.CopyFolder "c:\mydocuments\*\*", "c:\tempfolder\"
If source contains wildcard characters or destination ends with a path separator (\), it is assumed that destination is an existing folder in which to copy matching folders and subfolders. Otherwise, destination is assumed to be the name of a folder to create. In either case, four things can happen when an individual folder is copied.

          o If destination does not exist, the source folder and all its contents gets copied. This is the usual case.
          o If destination is an existing file, an error occurs.
          o If destination is a directory, an attempt is made to copy the folder and all its contents. If a file contained in source already exists in destination, an error occurs if overwrite is false. Otherwise, it will attempt to copy the file over the existing file.
          o If destination is a read-only directory, an error occurs if an attempt is made to copy an existing read-only file into that directory and overwrite is false.

An error also occurs if a source using wildcard characters doesn't match any folders.
The CopyFolder method stops on the first error it encounters. No attempt is made to roll back any changes made before an error occurs.
Avatar of SimplyGeeky

ASKER

If you look, Line 29 creates the destination folder if it doesn't already exist, and overwrite TRUE is on, so it should work.
Avatar of Todd Gerbert
Leave the trailing \ off of strBackupDir
Actually, leave trailing backslash off of both source and dest dir names.
Still fails at line 42, path not found.

I added some checking (see code below).
It still fails with the same error at the same line.

The commented out lines were my attempt to get another batch file which basically contains

xcopy /y/s/e q:\virtualmachines\%1\ d:\backups\local\vs\%2.0\

'Line 39: Copy the virtual machine files from the shadow copy
strSource = strTempDrive & "\" & strVMfolder
if Not(oFileSys.FolderExists(strSource)) then wsh.echo("source not found: " & strSource)
if Not(oFileSys.FolderExists(strBackupDir)) then wsh.echo("backupdir not found: " & strBackupDir)
objFSO.CopyFolder strSource , strBackupDir, TRUE
'set oExCmd = "do_the_copy.cmd "& ServerName & " " & dow
'Result = objShell.run(sExCmd,vbMinimized, TRUE)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SimplyGeeky
SimplyGeeky
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, above this line:
objFSO.CopyFolder strSource , strBackupDir, TRUE

put this
MsgBox "Attempting to copy" & VbCrLf & strSource & VbCrLf & "to" & VbCrLf & strBackupDir & VbCrLf & _
      "Source exists: " & objFSO.FolderExists(strSource) & VbCrLf & _
      "Desitination exists: " & objFSO.FolderExists(strBackupDir) & VbCrLf & _
      "Is destination a file: " & objFSO.FileExists(strBackupDir)


and you'll see the paths you're trying to copy, as well as whether each path exists, and also if the destination folder is already a file.

If the source folder doesn't exist, you'll have a problem.  If the the destination folder doesn't exist, it will try to create it anyway.  If the destination path is already a file name, you will have an error, and will need to delete that first.

Regards,

Rob.