Avatar of LostInWindows
LostInWindowsFlag for Canada

asked on 

File Copy/Move using vbs

I am trying to do some regular file management using vbs.
I am not very proficient in vb script but I am missing something here.

I have a script that needs to accomplish this:
Using Existing Directory d:\Backups
Create a sub folder called YYDDMM
Copy a file called D:\QuickBooks\Daily\*.QBB to D:\Backups\YYDDMM\

I can do everything except the last line. When I use a msgbox it shows that my destination path is blank.

Can you advise what is missing or wrong in this script?
It continuously fails on line 58 saying
"Invalid attempt to call Exec without a command".
VB Script

Avatar of undefined
Last Comment
Bill Prew
Avatar of Bill Prew
Bill Prew

Try this:

' Description:
' Tool for handling backingups of files in QuickBooks
' Will generate directory names with the following style: YYYYMMDD
' E.g.: 20070228
' QuickBooks is scripted to automatically 

Option Explicit
' Before we get too far into this whole process let's make sure that the backup Path actually exists

Const BackupPath = "D:\Backups\QuickBooks\Backup\"
Const sSourceFile = "D:\Backups\QuickBooks\Daily\*.QBB"
Const OverwriteExisting = True

Dim fso, Folder, filedate, f, file, filesys, FolderCollection, iDaysOld, iReturn
Dim sBackupPrefix, objshell, strcmd

set filesys = CreateObject("Scripting.FileSystemObject")
Set fso = CreateObject("Scripting.FileSystemObject")

'Wscript.Echo Now & " - backup script started."

if Not fso.FolderExists(BackupPath) then
   WScript.Echo "The folder " & BackupPath & " does not exist!"
end if

'This portion of the script will delete all backups in the folder that are more than 7 days old

'Wscript.Echo Now & " - Deleting old folders..."

DeleteBackup("D:\Backups\QuickBooks\Backup")   ' Call the Delete Subroutine

'Now we copy the QuickBooks files

'Set folder = fso.GetFolder(BackupPath)
filedate = NOW
Set f = Nothing

' Assigns a name of the new file
sBackupPrefix = BackupPath & year(now) & right("00" & Month(now),2) & right("00" & day(now),2)
msgbox ssourcefile & chr(20) & sbackupprefix

Set folder = filesys.CreateFolder(sBackupPrefix)

'Wscript.Echo "Backup folder will be: [" & sBackupPrefix & "]"

Set objShell = CreateObject("WScript.Shell")

fso.CopyFile (sSourceFile, sBackupPrefix)

Set objshell = nothing

'Wscript.Echo Now & " - script ended."

Sub DeleteBackup(ParentFolder)
Dim fs, f, f1, s, sf, d
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(ParentFolder)
Set sf = f.SubFolders
For Each f1 in sf
      s = f1.DateCreated
      d = DateDiff("d", s, Now)
      If d > 7 Then
           'Wscript.Echo "Deleting folder: [" & f1.Path & "]"
      End If

End Sub

Open in new window

Avatar of LostInWindows
Flag of Canada image


Line 56 which says:    fso.CopyFile (sSourceFile, sBackupPrefix)
I get an error message saying "Cannot use parenthesis when calling a sub"
Avatar of Bill Prew
Bill Prew

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of LostInWindows
Flag of Canada image


Thank you, I really appreciate it!
Avatar of Bill Prew
Bill Prew

Welcome, thanks for the feedback.

VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo