Solved

VBS Error

Posted on 2013-11-04
6
397 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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