Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

VBS problem

  • 09-04-2009 12:00pm
    #1
    Registered Users, Registered Users 2 Posts: 946 ✭✭✭


    Hey guys, im having some trouble writing this vb script.
    Im new to vb and scripting in general really, but this is just something im trying to row together.
    Basically im trying to read each line from a text file, insert it into an array and then print it from the array to another file...
    but i cant seem to get it to work!!

    Anyone see anything wrong with the code, the error im getting is that the object variable objTextFile is undefined on line 30.. i dont get it cos its defined at the begining.

    Any help is much appericated


    strDirectory = "C:\Files\"
    strFileRead = "text.txt"
    strFileWrite = "textCopy.txt"
    
    Const ForReading = 1
    Const ForAppending = 8
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strDirectory & strFileRead, ForReading)
    Set objTextFile = objFSO.OpenTextFile (strDirectory & strFileWrite, ForAppending)
    
    Dim arrFileLines()
    i = 0
    Do Until objFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objFile.ReadLine
    i = i + 1
    Loop
    objFile.Close
    
    LowerVal = LBound(arrFileLines)
    UpperVal = UBound(arrFileLines)
    For i = LowerVal To UpperVal
    
    objTextFile.WriteLine(arrFileLines(i)) 
    objTextFile.Close
    Next
    


Comments

  • Closed Accounts Posts: 5 StuckInTheFudd


    does the text file that you are writing to exist?
    There is an optional parameter on OpenTextFile for creating the file if it dosen't exist.
    If this defaults to false and the file does not exist then that might be why you are getting the error?


  • Registered Users, Registered Users 2 Posts: 946 ✭✭✭Lord Derpington


    Yea i was thinking about that too, it does exist.
    I was going to put in the create if it doesnt option but i didnt see the need seeing as what im goin to use it for and it will be only myself using it

    Ive been thinking about it and if i can get it to run in the command line and not give me notice boxes i can just use output redirection to put it into a text file, but i cant get cscript.echo to run without givinig errors and wscript.echo just gives me the same info boxes


  • Registered Users, Registered Users 2 Posts: 946 ✭✭✭Lord Derpington


    Fixed it, i was closing the file inside the loop.... stupid mistake...


Advertisement