| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 431 人关注过本帖
标题:如何使输出的文件名自动设置成读取的文件夹名?
取消只看楼主 加入收藏
wzhaiai
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-9-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
如何使输出的文件名自动设置成读取的文件夹名?
我有一个小工具,是选取某一个文件夹后,处理该文件夹下所有的数据后,自动生成一个文件并且在里面输出结果,我现在想让该输出的文件的文件名自动变为刚刚选取的上层文件夹的文件名,请问各位大神,代码怎么写?(附已完成的代码)
Private Sub Command1_Click()
Dim a(1048576) As String   
Dim aa(1048576) As String   
Dim aaa(1048576) As String   
Label4.Visible = True   '显示“正在处理,请耐心等待”
DoEvents   
sosuofile Text1.Text   

n = 0
p = 0

For m = 0 To List1.ListCount - 1

j = 0
k = 0

If LCase$(Right(List1.List(m), 3)) = "txt" Then

    Open List1.List(m) For Input As #1  
        Do While Not EOF(1)
        Line Input #1, temp
        
        If temp <> "" Then
            If Left(temp, 1) <> "-" Then  
                If j >= 2 Then
                    aa(j) = Split(temp, " ")(0)
                    temp1 = aa(j)
                    'If aa(j) - aa(j - 1) <> 1 Then
                        a(k) = temp
                        k = k + 1
                        j = j + 1
                    'End If
                Else
                    a(k) = temp
                    aa(j) = Split(temp, " ")(0)
                j = j + 1
                k = k + 1
                End If
            
            'MsgBox "当前数字=" & temp1
            Else

            If temp1 = "" Then temp1 = "0000"
            
            If Mid(temp1, 1, 2) = "23" And Mid(temp1, 3, 2) = "59" Then
            aaa(p) = "0000"
            End If
            
            If Mid(temp1, 1, 2) <> "23" And Mid(temp1, 3, 2) = "59" Then
            aaa(p) = Format(CInt(Mid(temp1, 1, 2)) + 1, "00") & "00"
            End If
            
            If Mid(temp1, 1, 2) <> "23" And Mid(temp1, 3, 2) <> "59" Then
            aaa(p) = Format(CInt(Mid(temp1, 1, 2)), "00") & Format(CInt(Mid(temp1, 3, 2)) + 1, "00")
            
            'MsgBox aaa(p)
            End If
            
            If Mid(temp1, 1, 2) = "23" And Mid(temp1, 3, 2) <> "59" Then
            aaa(p) = Format(CInt(Mid(temp1, 1, 2)), "00") & Format(CInt(Mid(temp1, 3, 2)) + 1, "00")
            'MsgBox aaa(p)
            End If


            temp1 = aaa(p)
            p = p + 1
            
            
            End If
        End If
        
        Loop
    Close #1
   
    i = p - 1
   
    temp2 = List1.List(m)
   
        Open Mid(List1.List(m), 1, Len(List1.List(m)) - 4) & ".bak" For Output As #1   
            For j = 0 To i
                'If Round(TimeValue(Format(Mid(aa(j), 1, 2) & ":" & Mid(aa(j), 3, 2) & ":00", "hh:mm:ss")) - TimeValue(Format(Mid(aa(j - 1), 1, 2) & ":" & Mid(aa(j - 1), 3, 2) & ":00", "hh:mm:ss")), 12) <> 0.000694444444 _
                'And Round(TimeValue(Format(Mid(aa(j), 1, 2) & ":" & Mid(aa(j), 3, 2) & ":00", "hh:mm:ss")) - TimeValue(Format(Mid(aa(j - 1), 1, 2) & ":" & Mid(aa(j - 1), 3, 2) & ":00", "hh:mm:ss")), 12) <> -0.999305555556 Then  
                If aaa(j) <> "" Then
                Print #1, aaa(j)
                                
                Open App.Path & "\缺测总数.bak" For Append As #2
               
                If temp2 = List1.List(m) Then
                Print #2, temp2
                temp2 = ""
                End If
               
                Print #2, aaa(j)
               
                Close #2
               
                aaa(j) = ""   '清空原数组的值
                n = n + 1
                End If
            Next j
        Close #1

End If

Next m

Open App.Path & "\缺测总数.bak" For Append As #2
Print #2, "缺测总数:" & n
Close #2

Label4.Visible = False  '重新隐藏“正在处理,请耐心等待”
MsgBox "ok", 64, "恭喜" '提示完成
End Sub

Private Sub sosuofile(mypath As String)
Dim myname As String
Dim a As String
Dim b() As String
Dim dir_i() As String
Dim i, idir As Long
If Right(mypath, 1) <> "\" Then mypath = mypath + "\"
myname = Dir(mypath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While myname <> ""
If myname <> "." And myname <> ".." Then
If (GetAttr(mypath & myname) And vbDirectory) = vbDirectory Then
idir = idir + 1
ReDim Preserve dir_i(idir) As String
dir_i(idir - 1) = myname
Else
List1.AddItem mypath & myname
End If
End If
myname = Dir
Loop
For i = 0 To idir - 1
Call sosuofile(mypath + dir_i(i))
Next i
ReDim dir_i(0) As String
End Sub
搜索更多相关主题的帖子: 文件夹 如何 
2014-09-25 22:27
快速回复:如何使输出的文件名自动设置成读取的文件夹名?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049800 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved