Solved

WriteLine if File Exist

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now