Solved

VBS Error

Posted on 2013-11-04
6
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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