Solved

WriteLine if File Exist

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 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