Current time: 02-05-2012, 05:48 PM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Naming CreateTextFile using File System Object
10-16-2009, 04:21 AM
Post: #1
Naming CreateTextFile using File System Object
Here's my script:

    VBS Programming
  1. WScript.Echo "You will be asked to enter transaction amounts." & vbCr & vbCr & "Press Cancel at any time to see the running balance."
  2.  
  3. arrVals = Array()
  4. Const ForWriting = 2
  5. Set objFso = CreateObject("Scripting.FileSystemObject")
  6. 'Set objFile = objFso.OpenTextFile("C:\testfile.txt", ForWriting, true)
  7. Set objFile = objFSO.CreateTextFile( _
  8. "c:\Documents and Settings\Bones\Desktop\CheckBook\" _
  9. & Month(Date) & "_" & Day(Date) & "_" & Year(Date) _
  10. & ".txt", ForWriting)
  11.  
  12. ' Loop until you get a starting value or Cancel to exit script
  13. Do While True
  14. strStart = InputBox("Enter starting balance as a decimal. (e.g. 234.56)" & vbCr & vbCr & "Enter debit amounts as negatives. (e.g. -23.40)")
  15. If strStart = "" Then WScript.Quit ' user pressed Cancel or entered blank info
  16.  
  17. If IsNumeric(strStart) Then ' checks if the entered text can be interpreted as a number (simple typo check)
  18. objFile.WriteLine "" & strStart
  19. Exit Do ' exits loop if the entry is a valid number
  20. Else
  21. WScript.Echo "There was an error in your last entry."
  22. End If
  23. Loop
  24.  
  25. intCount = 0 ' transaction counter
  26. ' Loop through transactions
  27. Do While True
  28. strAmt = InputBox("Enter transaction amount")
  29. If strAmt = "" Then Exit Do ' user pressed Cancel or enter blank info
  30. objFile.WriteLine "" & strAmt
  31. If IsNumeric(strAmt) Then
  32. ReDim Preserve arrVals(intCount) 'resize array to accomodate current number of values
  33. arrVals(intCount) = strAmt ' insert current transaction into array
  34. intCount = intCount + 1 ' increment the counter
  35. 'objFile.WriteLine "" & strAmt
  36. Else
  37. WScript.Echo "There was an error in your last entry. Please enter it again."
  38. End If
  39. Loop
  40.  
  41. ' Add the values in the array.
  42. curTotal = CCur(0.00) ' initialize total as currency value of 0
  43. For i = 0 to UBound(arrVals)
  44. curTotal = curTotal + CCur(arrVals(i))
  45. Next
  46. objFile.WriteBlankLines(3)
  47. objFile.WriteLine "Total:"
  48. objFile.WriteBlankLines(1)
  49. objFile.WriteLine "$" & (CCur(strStart) + curTotal)
  50. objFile.Close
  51. WScript.Echo "Running Total: $" & (CCur(strStart) + curTotal)



The script works fine. I even have it creating a text file named "today's date". If I run the script more than once per day it overwrites the text file created before it. My question is:

How do you get it to name each new text file different than the first while still maintaining the system date?

undefined
Find all posts by this user
Quote this message in a reply
10-16-2009, 09:43 AM
Post: #2
RE: Naming CreateTextFile using File System Object
(10-16-2009 04:21 AM)hondabones Wrote:  How do you get it to name each new text file different than the first while still maintaining the system date?

Add the time.

Scripting problems? Windows questions? Ask the Windows Guru!

Stay up to date with all of my latest content. Follow me on Twitter!
[Image: tsig.php]
Help us help you! Post your exact error message with these easy tips!
Visit this user's website Find all posts by this user
Quote this message in a reply
10-16-2009, 03:28 PM
Post: #3
RE: Naming CreateTextFile using File System Object
(10-16-2009 09:43 AM)Nilpo Wrote:  
(10-16-2009 04:21 AM)hondabones Wrote:  How do you get it to name each new text file different than the first while still maintaining the system date?

Add the time.

Yep, That makes sense.

It would be better if the script could read an existing text file then recall the "Running Balance: $"

Use this amount for the strStart

User would then input new amounts

the script would then write these amounts with the new balance to the same text file without overwriting the original text.

How could I do that?

undefined
Find all posts by this user
Quote this message in a reply
10-16-2009, 03:54 PM (This post was last modified: 10-16-2009 04:36 PM by Nilpo.)
Post: #4
RE: Naming CreateTextFile using File System Object
Well, you could do that too.

To begin, you'll need to use the constant ForAppending to write to the file without overwriting its existing contents.
    VBS Programming
  1. Const ForAppending = 8

Of course, you'd use that as the second parameter in the OpenTextFile field.

Then you'll want to read in the text file and remember the last line. I can't tell you how to parse out the amount because I don't know what that line looks like for you. But here's how to retrieve the last line of the file.
    VBS Programming
  1. Do Until objFile.AtEndOfStream
  2. strLine = objFile.ReadLine
  3. Loop

Basically, this will assign each line of the file to strLine. When the loop exits, strLine will contain the last line of the file and the file's pointer will be at the end of the file--exactly where you want to start writing.

If your file has a line something like "Running Balance: $" where the amount is immediately following a dollar sign and running to the end of the line, then you can parse it easily like this:
    VBS Programming
  1. strBal = Right(strLine, Len(strLine) - InStr(strLine, "$"))


Scripting problems? Windows questions? Ask the Windows Guru!

Stay up to date with all of my latest content. Follow me on Twitter!
[Image: tsig.php]
Help us help you! Post your exact error message with these easy tips!
Visit this user's website Find all posts by this user
Quote this message in a reply
10-16-2009, 04:21 PM (This post was last modified: 10-16-2009 04:29 PM by hondabones.)
Post: #5
RE: Naming CreateTextFile using File System Object
I could just read from the bottom up like this:

    VBS Programming
  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  2. Set objFile = objFSO.GetFile("C:\test.txt")
  3. If objFile.Size > 0 Then
  4. Set objReadFile = objFSO.OpenTextFile("C:\test.txt", 1)
  5. i = 1
  6. Do Until objReadFile.AtEndOfStream
  7. Redim Preserve arrData(i)
  8. arrData(i) = objReadFile.ReadLine
  9. i = i + 1
  10. Loop
  11. 'objFile.Close
  12. For l = Ubound(arrData) to LBound(arrData) Step -1
  13. 'Wscript.Echo "Record", l, "=", arrData(l)
  14. Wscript.Echo arrData(l)
  15.  
  16. Next
  17. End If



But that reads every line and echoes each line back.

undefined
Find all posts by this user
Quote this message in a reply
10-16-2009, 04:28 PM
Post: #6
RE: Naming CreateTextFile using File System Object
(10-16-2009 03:54 PM)Nilpo Wrote:  Well, you could do that too.

To begin, you'll need to use the constant ForAppending to write to the file without overwriting its existing contents.
    VBS Programming
  1. Const ForAppending = 8

Of course, you'd use that as the second parameter in the OpenTextFile field.

Then you'll want to read in the text file and remember the last line. I can't tell you how to parse out the amount because I don't know what that line looks like for you. But here's how to retrieve the last line of the file.
    VBS Programming
  1. Do Until objFile.EOF
  2. strLine = objFile.ReadLine
  3. Loop

Basically, this will assign each line of the file to strLine. When the loop exits, strLine will contain the last line of the file and the file's pointer will be at the end of the file--exactly where you want to start writing.

If your file has a line something like "Running Balance: $" where the amount is immediately following a dollar sign and running to the end of the line, then you can parse it easily like this:
    VBS Programming
  1. strBal = Right(strLine, Len(strLine) - InStr(strLine, "$"))


so i would then enter strBal for the input strStart?

undefined
Find all posts by this user
Quote this message in a reply
10-16-2009, 04:35 PM
Post: #7
RE: Naming CreateTextFile using File System Object
(10-16-2009 04:28 PM)hondabones Wrote:  so i would then enter strBal for the input strStart?

Yup.

Scripting problems? Windows questions? Ask the Windows Guru!

Stay up to date with all of my latest content. Follow me on Twitter!
[Image: tsig.php]
Help us help you! Post your exact error message with these easy tips!
Visit this user's website Find all posts by this user
Quote this message in a reply
10-16-2009, 04:37 PM
Post: #8
RE: Naming CreateTextFile using File System Object
(10-16-2009 04:21 PM)hondabones Wrote:  I could just read from the bottom up like this:

    VBS Programming
  1. Set objFSO = CreateObject("Scripting.FileSystemObject")
  2. Set objFile = objFSO.GetFile("C:\test.txt")
  3. If objFile.Size > 0 Then
  4. Set objReadFile = objFSO.OpenTextFile("C:\test.txt", 1)
  5. i = 1
  6. Do Until objReadFile.AtEndOfStream
  7. Redim Preserve arrData(i)
  8. arrData(i) = objReadFile.ReadLine
  9. i = i + 1
  10. Loop
  11. 'objFile.Close
  12. For l = Ubound(arrData) to LBound(arrData) Step -1
  13. 'Wscript.Echo "Record", l, "=", arrData(l)
  14. Wscript.Echo arrData(l)
  15.  
  16. Next
  17. End If



But that reads every line and echoes each line back.

Big time over kill. Don't make it more complicated than it is. And don't use unnecessary objects/arrays/loops. It will just make your code less efficient.

Scripting problems? Windows questions? Ask the Windows Guru!

Stay up to date with all of my latest content. Follow me on Twitter!
[Image: tsig.php]
Help us help you! Post your exact error message with these easy tips!
Visit this user's website Find all posts by this user
Quote this message in a reply
10-16-2009, 04:40 PM
Post: #9
RE: Naming CreateTextFile using File System Object
(10-16-2009 04:37 PM)Nilpo Wrote:  Big time over kill. Don't make it more complicated than it is. And don't use unnecessary objects/arrays/loops. It will just make your code less efficient.

How do I use your method but read from the bottom select the first line of text and use that for strBal?

undefined
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


Forum Permissions
You cannot post new threads.
You cannot post replies.
You cannot post attachments.
HTML is turned off.
MyCode is turned on.
Smilies are turned on.
[img] is turned on.