Avatar of LostInWindows
LostInWindows
Flag 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".
QBBackupFileManagement.vbs
VB Script

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
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!"
   WScript.Quit
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 & "]"
           f1.delete(True)
      End If
Next

End Sub
'=============================================================================

Open in new window

~bp
LostInWindows

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

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
LostInWindows

ASKER
Thank you, I really appreciate it!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bill Prew

Welcome, thanks for the feedback.

~bp