
vb如何建立文件夹并保存文件
请教一下:在form1中一个text控件,输入一段字符作为一个文件夹的名字,然后,在form2中打开这个文件夹,并将一个txt文件保存进这个文件夹,如何处理?这样的形式:...
请教一下:在form1中一个text控件,输入一段字符作为一个文件夹的名字,然后,在form2中打开这个文件夹,并将一个txt文件保存进这个文件夹,如何处理?
这样的形式:d:\ text1.text \123.txt 不知道text1中的字符怎么转化为文件名,然后后面如何打开?每次启动程序,文件夹名都要重新命名
请赐教 展开
这样的形式:d:\ text1.text \123.txt 不知道text1中的字符怎么转化为文件名,然后后面如何打开?每次启动程序,文件夹名都要重新命名
请赐教 展开
3个回答
展开全部
Form1的文件名确保输入正确.你输入多级目录或者单级目录都可以。下面代码可以参照。参数就传递你Text1.text。
Public Function CreateDir(path As String) As Boolean
Static start, pos As Integer
Static directory As String
Static result As Boolean
result = True
' initialize the error trap
On Error GoTo errCreation
' if null string why bother....
If path = "" Then Err.Raise vbObjectError + 1
' start will always be null
' the first time through
If start = Empty Then
start = 1
Else
start = pos + 1
End If
' find "\" if the char exists
pos = InStr(start, path, Chr$(92))
If (pos <> 0) Then
' not at the last directory in the path string...
directory = directory + Mid$(path, start, pos - start) + Chr$(92)
If InStr(1, Mid$(path, start, pos - start), Chr$(58)) = 0 And Dir(directory, vbDirectory) = "" Then
MkDir Mid$(directory, 1, Len(directory) - 1)
End If
' call itself
result = CreateDir(path)
ElseIf (pos = 0) Then
' the last directory or the only in the path string
directory = directory + Mid$(path, start, Len(path) - start + 1)
MkDir Mid$(directory, 1, Len(directory))
directory = ""
End If
' success return true
CreateDir = result
Exit Function
' if it gets here, an exception was thrown
' propogate the error to the calling function
errCreation:
Err.Clear
result = False
CreateDir = result
End Function
至于打开该文件夹。你可以用VB中的
Shell "explorer " & FILENAME, vbNormalNoFocus
FILENAME是文件夹或者文件名都可以。
每次启动程序,文件夹名都要重新命名,下面代码参照:
Dim i As Integer
i = 1
Set fs = CreateObject("scripting.filesystemobject")
Set fd = fs.GetFolder("d:\test")
fd.Name = "pic"
或者
Name "OldPathname" As "NewPathname" 也可以
只能给你一个实现的大概
Public Function CreateDir(path As String) As Boolean
Static start, pos As Integer
Static directory As String
Static result As Boolean
result = True
' initialize the error trap
On Error GoTo errCreation
' if null string why bother....
If path = "" Then Err.Raise vbObjectError + 1
' start will always be null
' the first time through
If start = Empty Then
start = 1
Else
start = pos + 1
End If
' find "\" if the char exists
pos = InStr(start, path, Chr$(92))
If (pos <> 0) Then
' not at the last directory in the path string...
directory = directory + Mid$(path, start, pos - start) + Chr$(92)
If InStr(1, Mid$(path, start, pos - start), Chr$(58)) = 0 And Dir(directory, vbDirectory) = "" Then
MkDir Mid$(directory, 1, Len(directory) - 1)
End If
' call itself
result = CreateDir(path)
ElseIf (pos = 0) Then
' the last directory or the only in the path string
directory = directory + Mid$(path, start, Len(path) - start + 1)
MkDir Mid$(directory, 1, Len(directory))
directory = ""
End If
' success return true
CreateDir = result
Exit Function
' if it gets here, an exception was thrown
' propogate the error to the calling function
errCreation:
Err.Clear
result = False
CreateDir = result
End Function
至于打开该文件夹。你可以用VB中的
Shell "explorer " & FILENAME, vbNormalNoFocus
FILENAME是文件夹或者文件名都可以。
每次启动程序,文件夹名都要重新命名,下面代码参照:
Dim i As Integer
i = 1
Set fs = CreateObject("scripting.filesystemobject")
Set fd = fs.GetFolder("d:\test")
fd.Name = "pic"
或者
Name "OldPathname" As "NewPathname" 也可以
只能给你一个实现的大概
展开全部
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |