2

I want to make a program that gets user input and saves it in a text document, every time it saves a new document I want the file name to change

Here is what I have:

Option Explicit

Dim fso
Dim firstNameInput
Dim lastNameInput
Dim count
Dim testPath
Dim exists
Dim fileName
Dim fileStream
Dim filePath

Set fso = CreateObject("Scripting.FileSystemObject")

firstNameInput = inputbox("Please enter your name")
lastNameInput = inputbox("Enter your last name")

count = 1
do
    testPath = "C:\Users\Me\Desktop\Info\peopleInfo" & count & ".txt"
    exists = fso.FolderExists(testPath)
    if(exists) then
        count + 1
    else
        exit do
    end if
loop

fileName = "peopleInfo" & count & ".txt"
filePath = "C:\Users\Me\Desktop\Info\"
Set fileStream = fso.CreateTextFile(filePath & fileName)

fileStream.WriteLine firstNameInput
fileStream.WriteLine lastNameInput
fileStream.Close

What I have doesn't seem to be working...

So every time I open this program, I want it to save the file as peopleInfo1 then peopleInfo2 then peopleInfo3 , etc.

3 Answers 3

1

Try something like that :

    Option Explicit
    Const RootFolder = "C:\Users\Me\Desktop\Info"
    Dim fso,Folder,FirstFile,sFile,sFileNewName,firstNameInput,lastNameInput
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FolderExists(RootFolder) Then
        fso.CreateFolder(RootFolder)
    End If
    Set Folder = fso.GetFolder(RootFolder)
    Do
        firstNameInput = inputbox("Please enter your name")
    Loop Until firstNameInput <> ""

    Do
        lastNameInput = inputbox("Enter your last name")
    Loop Until lastNameInput <> ""

    FirstFile = RootFolder &"\peopleInfo.txt"
    If Not fso.FileExists(FirstFile) Then
        Call Write2File(RootFolder & "\peopleInfo.txt")
    Else
        sFileNewName = GetNewName(FirstFile)
        Call Write2File(sFileNewName)   
    End If
    '************************************************************************************************************
    Function GetNewName(sFile)
        Dim snamebase,sname,Count,sTarget,MaxIncrementation
        MaxIncrementation = 1000
        snamebase = Split(Right(sFile, Len(sFile) - InStrRev(sFile,"\")),".")(0)
        sname = snamebase
        Count = 0
        While Count < MaxIncrementation
            sTarget = Folder & "\" & sname & ".txt"
            If fso.FileExists(sTarget) Then
                Count = Count + 1
                sName = snamebase & "(" & Count & ")"
            Else
                GetNewName = sTarget
                Exit Function
            End If
        Wend
    End Function
    '************************************************************************************************************
    Sub Write2File(File)
        Dim fileStream
        Set fileStream = fso.CreateTextFile(File)
        fileStream.WriteLine firstNameInput
        fileStream.WriteLine lastNameInput
        fileStream.Close
    End Sub
    '************************************************************************************************************

Or Something like that :

Option Explicit
Dim Ws,fso,RootFolder,Folder,FirstFile,sFile,sFileNewName,firstNameInput,lastNameInput,Desktop
Set Ws = CreateObject("Wscript.Shell")
RootFolder = Ws.ExpandEnvironmentStrings("%USERPROFILE%\Desktop\Info")
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(RootFolder) Then
    fso.CreateFolder(RootFolder)
End If
Set Folder = fso.GetFolder(RootFolder)
Do
    firstNameInput = inputbox("Please enter your name")
Loop Until firstNameInput <> ""

Do
    lastNameInput = inputbox("Enter your last name")
Loop Until lastNameInput <> ""

FirstFile = RootFolder &"\peopleInfo.txt"
If Not fso.FileExists(FirstFile) Then
    Call Write2File(RootFolder & "\peopleInfo.txt")
Else
    sFileNewName = GetNewName(FirstFile)
    Call Write2File(sFileNewName)   
End If
'************************************************************************************************************
Function GetNewName(sFile)
    Dim snamebase,sname,Count,sTarget,MaxIncrementation
    MaxIncrementation = 1000
    snamebase = Split(Right(sFile, Len(sFile) - InStrRev(sFile,"\")),".")(0)
    sname = snamebase
    Count = 0
    While Count < MaxIncrementation
        sTarget = Folder & "\" & sname & ".txt"
        If fso.FileExists(sTarget) Then
            Count = Count + 1
            sName = snamebase & "(" & Count & ")"
        Else
            GetNewName = sTarget
            Exit Function
        End If
    Wend
End Function
'************************************************************************************************************
Sub Write2File(File)
    Dim fileStream
    Set fileStream = fso.CreateTextFile(File)
    fileStream.WriteLine firstNameInput
    fileStream.WriteLine lastNameInput
    fileStream.Close
End Sub
'************************************************************************************************************
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I got it working, just wondering if you could help with this --> stackoverflow.com/questions/29469545/…
1

The first problem is caused by your line:

exists = fso.FolderExists(testPath)

It should be

exists = fso.FileExists(testPath)

as you are looking for a file, not a folder.

The second problem is caused by your line

count + 1

It should be

count = count + 1

to assign the new/increased value to count.

1 Comment

Thank you I got it working, I was wondering if you could help me with this --> stackoverflow.com/questions/29469545/…
0

Count is always starts at 1 because you say so. Count = 1. Store count in a file.

2 Comments

Sorry but I'm very new to programming, can you please show an example of storing Count = 1 into a file, thanks
Say what your goal is? Why would you want to create 1 file with 1 record per program run?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.