Solved

WriteLine if File Exist

Posted on 2006-11-16
5
163 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
  • 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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now