我想创建一个程序来获取用户输入并将其保存在文本文档中,每次保存新文档时我希望文件名更改
这就是我所拥有的:
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
我所拥有的似乎并没有起作用......
因此,每次打开此程序时,我都希望将文件保存为peopleInfo1
,然后peopleInfo2
然后peopleInfo3
等。
答案 0 :(得分:1)
尝试类似的东西:
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
'************************************************************************************************************
或类似的东西:
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
'************************************************************************************************************
答案 1 :(得分:1)
第一个问题是由您的行引起的:
exists = fso.FolderExists(testPath)
应该是
exists = fso.FileExists(testPath)
因为您正在寻找文件,而不是文件夹。
第二个问题是由你的
引起的count + 1
应该是
count = count + 1
将新值/增加值分配给count。
答案 2 :(得分:0)
计数总是从1开始,因为你这么说。 Count = 1
。将计数存储在文件中。