Solved

WriteLine if File Exist

Posted on 2006-11-16
5
170 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

688 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