?
Solved

WriteLine if File Exist

Posted on 2006-11-16
5
Medium Priority
?
173 Views
Last Modified: 2010-05-18
Im trying to write a line to text file if file exist or create file then write line if it doesn't exist..
Problem: It overwrites the top line with last piece of info that goes through it..... and doesn't add the info to already created files.


See Here

strNewUsers = (strGroup)

      If objfilesys.FileExists(FpathS & strGroup & " New Users.txt") then
      strNewUsers.WriteLine ("Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup)
      Else
          Set strNewUsers = objfilesys.CreateTextFile(FpathS & strGroup & " New Users.txt", True)
          strNewUsers.WriteLine ("Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup)"
      End If
      
strNewUsers.Close
0
Comment
Question by:dion_p1
[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
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17962454
You need to open strNewUsers as Append Mode.

Brian
0
 

Author Comment

by:dion_p1
ID: 17962456
how do i do that?
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17962462
Post your full code...
0
 

Author Comment

by:dion_p1
ID: 17962487
Set objFileSys = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

FpathS = ""

Set oInputFile = objFileSys.OpenTextFile(FpathS)
strInputData = Split(oInputFile.ReadAll, vbNewline)
On Error Resume Next
For each strData In strInputData
          strUserID= Ucase(split(strData, ",") (0))
          strFirstname = Lcase(split(strData, ",") (1))
          strFirstname = Ucase(left(strFirstname, 1)) & mid(strFirstname, 2, len(strFirstname))
          strSurname = Lcase(split(strData, ",") (2))
          strSurname = Ucase(left(strSurname, 1)) & mid(strSurname, 2, len(strSurname))
          strSurname = Replace(strSurname, "'", "")
          strGroup = Ucase(split(strData, ",") (3))
          strYear = Lcase (Split(strData, ",") (4))
          strHomeFolder = Lcase(split(strData, ",")(4))
      strEnter = LCase(Split(strData, ",")(5))
      strExit = LCase (Split(strData, ",")(6))
      strPassword = Lcase(split(strData, ",")(7))

      strNewUsers = (strGroup)

      If objfilesys.FileExists(FpathS & strGroup & " New Users.txt") then
      strNewUsers.WriteLine ("Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup)
      Else
          Set strNewUsers = objfilesys.CreateTextFile(FpathS & strGroup & " New Users.txt", True)
          strNewUsers.WriteLine ("Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup)
      End If
      
      strNewUsers.Close

Next
0
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 2000 total points
ID: 17964165
you dont even need to check for file existance, simply use this code

    Dim oText
    Set oText = objfilesys.OpenTextFile(FpathS & strGroup & " New Users.txt", ForAppending, True)
    oText.WriteLine "Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup
    oText.Close

and you can remove the following code
'======================================
 If objfilesys.FileExists(FpathS & strGroup & " New Users.txt") then
     strNewUsers.WriteLine ("Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup)
     Else
         Set strNewUsers = objfilesys.CreateTextFile(FpathS & strGroup & " New Users.txt", True)
         strNewUsers.WriteLine ("Username: " & strUserID & " Password: " & strPassword & " Group: " & strGroup)
     End If
'======================================
0

Featured Post

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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, left to enroll

800 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