We help IT Professionals succeed at work.

"Expected end of statement" error in VBScript code.

donnie91910
donnie91910 asked
on
High Priority
41 Views
Last Modified: 2020-03-17
I have written VBScript code to look for specific files in a folder and when the code finds the files the code will move the files to another folder.  I am able to find one file and move it, but when I try to find two or more files I get the "Expected end of statement" error.  I have included the code that I have written.  

Dim objFSO
Dim sSourceFolder
Dim sDestFolder
Dim sDBFile
Dim sDateTimeStamp
'Const OVER_WRITE_FILES = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
sSourceFolder = "C:\Oceanside\"
sBackupFolder = "C:\Files_Found\"
sDBFile = "ROWP19-0578 PERMIT","ROWP19-0579 PERMIT"  'Expected end of statement error on this line when I try to search for two files
sDBFileExt = "pdf"
 
'If the backup folder doesn't exist, create it.
If Not objFSO.FolderExists(sBackupFolder) Then
    objFSO.CreateFolder(sBackupFolder)
End If
 
'Copy the file as long as the file can be found
If objFSO.FileExists(sSourceFolder & "\" & sDBFile & "." & sDBFileExt) Then
    objFSO.CopyFile sSourceFolder & "\" & sDBFile & "." & sDBFileExt,_
			sBackupFolder & "\" & sDBFile & "." & sDBFileExt
MsgBox sSourceFolder & " was copied to " & sBackupFolder
End if
 
Set objFSO = Nothing

Open in new window

Any help is greatly appreciated.
Comment
Watch Question

Serge FournierAnalyst Programmer
CERTIFIED EXPERT

Commented:
You have to define an array:

sDBFile_arr = array("ROWP19-0578 PERMIT","ROWP19-0579 PERMIT" )

then loop each element in array

for each sdbfile in sdbfile_arr
'code
next
Software Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT
Commented:
And here's the example:

Dim objFSO
Dim sSourceFolder
Dim sDestFolder
Dim sDBFile
Dim sDateTimeStamp
'Const OVER_WRITE_FILES = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
sSourceFolder = "C:\Oceanside\"
sBackupFolder = "C:\Files_Found\"
sDBFile = array("ROWP19-0578 PERMIT", "ROWP19-0579 PERMIT")

sDBFileExt = "pdf"
 
'If the backup folder doesn't exist, create it.
If Not objFSO.FolderExists(sBackupFolder) Then
    objFSO.CreateFolder(sBackupFolder)
End If

'Copy the file as long as the file can be found
For i = 0 to Ubound(sDBFile)
	If objFSO.FileExists(sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt) Then
		objFSO.CopyFile sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt,_
					  sBackupFolder & "\" & sDBFile(i) & "." & sDBFileExt
		MsgBox sSourceFolder & "\" & sDBFile(i) & "." & sDBFileExt & " was copied to " & sBackupFolder
	End if
Next

Set objFSO = Nothing

Open in new window

Author

Commented:
The array code worked. Thanks to both of you.