locked
Using Recursion to Write to a TextFile RRS feed

  • Question

  • Hello all,

    I've always got more questions than answers, so please bare with me.  Please refer to the Looping bgColor here:  http://social.technet.microsoft.com/Forums/en-us/newthread?forum=ITCG 

    To sum it up, "Works Great!".  The reason that particular example doesn't look very pleasing is because the color specifications are too big of a jump, ie:  ffffff,  eeeeee,  dddddd.  It runs smooth if the colors are specified as follows:  ffffff, fefefe, fdfdfd, fcfcfc, etc.  This allows for a smooth transition of color and you won't get dizzy watching the background flutter a bit.  Ok, all fine and dandy.

    I tried unsuccessfully to use a recursion statement to increment up the array and then back down the array.  IE:  The script fades the background color from white to black and then resets.  I tried using:

    If c = 0 Then
     document.body.bgColor = "#" & Color(c)
     c = (c+1)Mod 16
     ElseIf c = 15 Then
     document.body.bgColor = "#" & Color(c)
     c = (c-1)Mod 16
    End If

    Which did not work.  So that is my first little question but not what this post is about, however.  You see I just worked around this by adding to the array ie:

     Dim Color(30)  'Dynamic Array
     Dim Clr     
     Dim c   'Used to increment

    Color(0) = "ffffff"
    Color(1) = "eeeeee"
    Color(2) = "dddddd"
    Color(3) = "cccccc"
    Color(4) = "bbbbbb"
    Color(5) = "aaaaaa"
    Color(6) = "999999"
    Color(7) = "888888"
    Color(8) = "777777"
    Color(9) = "666666"
    Color(10) = "555555"
    Color(11) = "444444"
    Color(12) = "333333"
    Color(13) = "222222"
    Color(14) = "111111"
    Color(15) = "000000"
    Color(16) = "111111"
    Color(17) = "222222"
    Color(18) = "333333"
    Color(19) = "444444"
    Color(20) = "555555"
    Color(21) = "666666"
    Color(22) = "777777"
    Color(23) = "888888"
    Color(24) = "999999"
    Color(25) = "aaaaaa"
    Color(26) = "bbbbbb"
    Color(27) = "cccccc"
    Color(28) = "dddddd"
    Color(29) = "eeeeee"


    Sub Window_Onload
    c = 0
    Intrval = setInterval("changeColor", 150)
    End Sub

    Sub changeColor
     document.body.bgColor = "#" & Color(c)
     c = (c+1)Mod 30
    End Sub

    Ok, but remember that style looks glitchy because of the leap in color.  I do not want to specify, by hand, a 256 variable sized array.  So I thought (Hey!), now is a good time to practice writing to text files.  I am trying to write a script that will write the array for me.  I see this is a bit complex but I figure maybe if I can at least get the script to write :

    Color(0) =
    Color(1) =
    Color(2) =
    Color(3) =
    (etc....)
    (etc....)

    Then I could start with the hexidecimal counting after that.  Here is what I wrote so far:

    CountingColorFile = "I:\My Documents\My Wallpaper\In Progress\Fading Colors\BlackGrayWhite.txt"
    Const ForWriting = 2
    Const ForAppending = 8
    Dim Color(10)
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If objFSO.FileExists(CountingColorFile) Then
     Set objFile = objFSO.OpenTextFile(CountingColorFile, ForAppending)
     objFile.WriteLine "Color" & c
     c = (c+1)
    Else
     Set objFile = objFSO.CreateTextFile(CountingColorFile)
     objFile.Close
     Set objFile = objFSO.OpenTextFile(CountingColorFile, ForWriting)
     objFile.Write "Color"
    End If
    objFile.Close

    This is pretty fun, at least I think so.  Thanks to all you's at the "Scripting Guy's Official Forum" !!!


    Student
    Monday, April 11, 2011 6:24 PM

Answers

  • If you have more questions, please feel free to post back here.

    Bill

    • Marked as answer by Bill_Stewart Friday, April 22, 2011 9:26 PM
    Friday, April 22, 2011 9:25 PM

All replies

  • Yeah Tom!

    Wow, and with only a few lines of code you accomplished the exact idea I had with the text color changing appropriately and everything.  I guess I will be comparing the above code with script56.chm for a while.  You are the man.  Thanks.

    Uh, I did just realize one thing.  I'd rather mark this as the answer to the "Looping bgColor" post and possibly still get information on how to write to a text file like the above question.  I know I do not need it for this circumstance anymore, but I'd still like to know.  Is is possible to have the posts rearranged in that manner?

     Ok, I did a little work.  I am unsure of how to use an array with what I have below to write to the document Color(0) and incrementing the interger inside the parentheses by 1 to a specified number.  Or maybe I wanted to keep counting to ten.  Yes, yes before someone goes on to tell me the incredible number of programs that already do this for me, please:  save your little typing fingers and let's, as heavy metal guitarists would way, "shred" the scripting of a text file a bit.  I'd like to get good at something :-)

    CountingColorFile = "I:\My Documents\My Wallpaper\In Progress\Fading Colors\BlackGrayWhite.txt"
    n = "0"
    Const ForWriting = 2
    Const ForAppending = 8
    Dim Color(10)
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If objFSO.FileExists(CountingColorFile) Then
     Set objFile = objFSO.OpenTextFile(CountingColorFile, ForAppending)
     objFile.WriteLine "Color" & "(" & n & ")"
    Else
     Set objFile = objFSO.CreateTextFile(CountingColorFile)
     objFile.Close
     Set objFile = objFSO.OpenTextFile(CountingColorFile, ForWriting)
     objFile.Write "Color"
    End If
    objFile.Close

    And please have Tom's solution removed from here and placed in the thread:  "Looping bgColor".  This way readers (And myself) can stay better focused on the topic at hand.  Thanks :-)  


    Student
    Monday, April 11, 2011 7:34 PM
  • If you have more questions, please feel free to post back here.

    Bill

    • Marked as answer by Bill_Stewart Friday, April 22, 2011 9:26 PM
    Friday, April 22, 2011 9:25 PM