Solved

VBS Error

Posted on 2013-11-04
6
394 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
Comment Utility
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
Comment Utility
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
Comment Utility
strToImport is on Li.20
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
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
Comment Utility
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
Comment Utility
You need to define the constants as below :

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

8 Experts available now in Live!

Get 1:1 Help Now