Solved

VBS Error

Posted on 2013-11-04
6
395 Views
Last Modified: 2013-11-04
Hello Experts,

I have the following VBScript procedure:

Sub OUTPUTOrders()

'    'Dim fsoOUT As FileSystemObject '*** comment OUT for VBS
'    'Dim fsoFile 'As File '*** comment OUT for VBS
'    'Dim textStream As Scripting.textStream

	Dim fsoOUT, fsoFile
    
    Set fsoOUT = CreateObject("Scripting.FileSystemObject")
    
    If fsoOUT.FileExists(strToImport) Then
    
        Set textStream = fsoOUT.OpenTextFile(strToImport, ForReading)
        
        If textStream.AtEndOfStream = False Then
            strContent = textStream.ReadAll & strContent
        End If
        
        textStream.Close
        Call fsoOUT.DeleteFile(strToImport, True)
        
    End If

    Set fsoFile = fsoOUT.CreateTextFile(strToImport, True)
    fsoFile.Write (strContent)
    fsoFile.Close
    
    Set textStream = Nothing
    Set fsoOUT = Nothing
    Set fsoFile = Nothing
    
End Sub

Open in new window


... but I get Invalid call for

    Set fsoFile = fsoOUT.CreateTextFile(strToImport, True)

Open in new window


Any ideas?

Thanks.
0
Comment
Question by:APD_Toronto
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39622992
where do you set the value for strToImport, is it being set with a valid string value representing a file location ?
0
 

Author Comment

by:APD_Toronto
ID: 39623007
This Is the entire script

'Option Explicit
'Dim strContent As String
'Dim strTS As String
'Dim strToImport As String

'Private Sub Form_Load() '***uncomment for debugging***
    Call MainSub
'End Sub  '***uncomment for debugging***

Sub MainSub()

    'Dim strToScan As String
    
    'Client's settings, INLUDING trailing backslash:
        strToScan = "D:\Client FTP Folders\"
        strToImport = "L:\MAS450\TOIMPORT\ALLORDERS.TXT"
        
    'Aleks' settings, INCLUDING trailing backslash ***COMMENT OUT FOLLOWING 2 LINES***:
        strToScan = "S:\Client FTP Folders\"
        strToImport = "S:\ToImport\ALLORDERS.TXT"
    
    strTS = Now
    strContent = ""
            
'    'Dim fso As FileSystemObject '*** comment OUT for VBS
'    'Dim fsoFolder As Folder '*** comment OUT for VBS
'    'Dim fsoSubFolder As Folder '*** comment OUT for VBS
        
    'Dim currFolder As String
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    currFolder = strToScan
    
    Call ScanDIR(currFolder)
    
    Set fsoFolder = fso.GetFolder(strToScan)
    
    For Each fsoSubFolder In fsoFolder.SubFolders
    
        currFolder = fsoSubFolder.Path & "\"
        Call ScanDIR(currFolder)
    
    Next
    
    Set fso = Nothing
    Set fsoFolder = Nothing
    Set fsoSubFolder = Nothing
    
    Call OUTPUTOrders

End Sub

Sub ScanDIR(strFolder)

'    'Dim fsoIN As FileSystemObject '*** comment OUT for VBS
'    'Dim fsoINFolder As Folder  '*** comment OUT for VBS
'    'Dim fsoFile As File '*** comment OUT for VBS
'    'Dim textStream As Scripting.textStream
    
    'Dim strFileName As String
    'Dim intLen As Integer
        
    Set fsoIN = CreateObject("Scripting.FileSystemObject")
    Set fsoINFolder = fsoIN.GetFolder(strFolder)
    
    For Each fsoFile In fsoINFolder.Files
    
    
        If (Right(fsoFile.Name, 4) = ".ORD") And (fsoFile.DateCreated < strTS) Then
            
            intLen = Len(fsoFile.Name)
            strFileName = Left(fsoFile.Name, intLen - 4)
            fsoFile.Name = strFileName & ".DONE"
            
            Set textStream = fsoFile.OpenAsTextStream
            strContent = strContent & textStream.ReadAll
            
        End If
    
    Next
    
    Set fsoIN = Nothing
    Set fsoINFolder = Nothing
    Set fsoFile = Nothing
    Set textStream = Nothing

End Sub

Sub OUTPUTOrders()

'    'Dim fsoOUT As FileSystemObject '*** comment OUT for VBS
'    'Dim fsoFile 'As File '*** comment OUT for VBS
'    'Dim textStream As Scripting.textStream

	Dim fsoOUT, fsoFile
    
    Set fsoOUT = CreateObject("Scripting.FileSystemObject")
    
    If fsoOUT.FileExists(strToImport) Then
    
        Set textStream = fsoOUT.OpenTextFile(strToImport, ForReading)
        
        If textStream.AtEndOfStream = False Then
            strContent = textStream.ReadAll & strContent
        End If
        
        textStream.Close
        Call fsoOUT.DeleteFile(strToImport, True)
        
    End If

    Set fsoFile = fsoOUT.CreateTextFile(strToImport, True)
    fsoFile.Write (strContent)
    fsoFile.Close
    
    Set textStream = Nothing
    Set fsoOUT = Nothing
    Set fsoFile = Nothing
    
End Sub

Open in new window

0
 

Author Comment

by:APD_Toronto
ID: 39623009
strToImport is on Li.20
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39623017
Because the strToImport is defined in main sub it is out of scope for the OututOrders sub, either move the definition of the strToImport variable outside of the mainSub sub at the top of the script or within the OutputOrder sub. This also probably applies to other variables you have defined in the main sub used in the other subs.
0
 

Author Comment

by:APD_Toronto
ID: 39623137
I commented out lines 7, 10, 52, but now I get error on 102
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39623144
You need to define the constants as below :

Const ForReading = 1, ForWriting = 2, ForAppending = 3
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now