使用fso吧,前段时间我刚做了一个vb的文件和文件夹的重命名。

解决方案 »

  1.   

    fso好像没有文件及文件夹的重命名方法或函数呀
      

  2.   

    你对vb熟悉不??要不我给你vb的程序,javascript的就不知道了,
      

  3.   

    VB的也行,我主要是要了解一下思想,VB我可以看懂,谢谢了
      

  4.   

    VB的也行,我主要是要了解一下思想,VB我可以看懂,谢谢了
      

  5.   

    我的是在文件夹和文件的名称加一个前缀,主要的操作就是bianli这个子过程,你看一看吧。
    Private Sub bianli(g_path As String)
        Dim fso As New FileSystemObject
        Dim k As Folder
        Dim g_Folder As Folder
        'Dim f As Files
        
        Set k = fso.GetFolder(g_path)
        'Set f = fso.GetFile(g_path)
        Dim i As Integer
        If k.SubFolders.Count > 0 Then
            Dim z, q As Integer
            Dim cs1 As String
            Dim cs As String
             z = 1
            For Each g_Folder In k.SubFolders
               
                 cs1 = CStr(z)
                 q = 3 - Len(cs1)
                 Select Case q
                 Case 0
                      cs = cs1
                 Case 1
                      cs = "0" & cs1
                 Case 2
                      cs = "00" & cs1
                 Case Else
                 End Select
              
                 If Mid(g_Folder.Name, 1, 1) <> "w" Or Not IsNumeric(Mid(g_Folder.Name, 2, 3)) Then
                      g_Folder.Name = "w" & cs + g_Folder.Name
                 End If
                 ' List1.AddItem g_Folder.Name
                 z = z + 1
                
                 bianli (g_Folder.Path)
            Next
        End If
       Dim g_file As File
       Dim t, j As Integer
       t = k.SubFolders.Count + 1
       Dim cs3 As String
       Dim cs4 As String
       For Each g_file In k.Files        cs3 = CStr(t)
            j = 3 - Len(cs3)
            Select Case j
            Case 0
                 cs4 = cs3
            Case 1
                 cs4 = "0" & cs3
            Case 2
                 cs4 = "00" & cs3
            Case Else
            End Select
       
            If Mid(g_file.Name, 1, 1) <> "w" Or Not IsNumeric(Mid(g_file.Name, 2, 3)) Then
                g_file.Name = "w" & cs4 + g_file.Name
         
            End If
            List1.AddItem g_file.Name
            t = t + 1
       Next
    End SubPrivate Sub cmdexit_Click()
        Unload Me
    End SubPrivate Sub cmdreset_Click()
        List1.Clear
        
        Call reset(Text1.Text)
        Dir1.Refresh
    End SubPrivate Sub Dir1_Change()
        Text1.Text = Dir1.Path
        End SubPrivate Sub Drive1_Change()
        Dim cs As String
        
        If Drive1.Drive = "a:" Then
           MsgBox "错误的驱动器!"
         
         End If
        
        Dir1.Path = Drive1.Drive
        Text1.Text = Dir1.PathEnd Sub
    Private Sub cmdstate_Click()
         List1.Clear
         Call resetfile(Text1.Text)
         List1.Clear
         Call bianli(Text1.Text)
         Dir1.Refresh
    End SubPrivate Sub Form_Load()
        Dim a As String
        Dir1.Refresh
        Drive1.Drive = "e:"
        'a = Drive1.Drive
        Dir1.Path = "e:"
        Text1.Text = Dir1.Path
    End Sub
    Private Sub reset(g_path As String)
        Dim fso1 As New FileSystemObject
        Dim j As Folder
        Dim g_Folder As Folder
        Set j = fso1.GetFolder(g_path)
        Dim i As Integer
        'm_label2.Caption = j.Files.Count
        If j.SubFolders.Count > 0 Then
         
            ' z = 1
            For Each g_Folder In j.SubFolders
             '  Dim cs1 As String
             '  Dim cs As String
            '   Dim q As Integer
            '     cs1 = CStr(z)
            '     q = 3 - Len(cs1)
            '     Select Case q
            '     Case 0
            '          cs = cs1
           '      Case 1
             '         cs = "0" & cs1
            '     Case 2
            '          cs = "00" & cs1
            '     Case Else
             '    End Select
            If Mid(g_Folder.Name, 1, 1) = "w" And IsNumeric(Mid(g_Folder.Name, 2, 3)) Then
              '  On Error GoTo err
                Dim s As String
                Dim g_name As String
                s = g_Folder.Name
                g_name = Mid(g_Folder.Name, 5, Len(s) - 4)
                If fso1.FolderExists(g_path & "\" & g_name) Then
                    g_Folder.Name = g_rename(j.Path, g_name)
                Else
                    g_Folder.Name = Mid(g_Folder.Name, 5, Len(g_Folder.Name) - 4)
                   ' g_Folder.Name = g_name
                End If         '  List1.AddItem g_Folder.Name
          '  Else
             '   List1.AddItem g_Folder.Name
             '   z = z + 1
             
            End If
                reset (g_Folder.Path)
            Next
        End If
        Dim g_file As File
        'Dim t, j As Integer
        If j.Files.Count > 0 Then
              ' t = k.SubFolders.Count + 1
              For Each g_file In j.Files
            
                    If Mid(g_file.Name, 1, 1) = "w" And IsNumeric(Mid(g_file.Name, 2, 3)) Then
                 '    On Error GoTo err
                       '  Dim s As String
                        ' Dim g_name As String
                         s = g_file.Name
                         g_name = Mid(g_file.Name, 5, Len(s) - 4)
                         If fso1.FileExists(j.Path & "\" & g_name) Then
                             g_file.Name = g_rename1(j.Path, g_name)
                         Else
                             g_file.Name = g_name
                         End If                   'g_file.Name = Mid(g_file.Name, 5, Len(g_file.Name) - 4)
                         List1.AddItem g_file.Name
                        
                    Else
                         List1.AddItem g_file.Name
                         'z = z + 1
                    End If
             Next
        End If
    err:
    'MsgBox "文件或者文件夹重名!"
    End Sub
    Private Sub resetfile(g_path As String)
        Dim fso1 As New FileSystemObject
        Dim j As Folder
        Dim g_Folder As Folder
        Set j = fso1.GetFolder(g_path)
        
        If j.SubFolders.Count > 0 Then
         
            For Each g_Folder In j.SubFolders
            
            '    List1.AddItem g_Folder.Name
             
                reset (g_Folder.Path)
            Next
        End If
        Dim g_file As File
        
        If j.Files.Count > 0 Then
             For Each g_file In j.Files
            
                    If Mid(g_file.Name, 1, 1) = "w" And IsNumeric(Mid(g_file.Name, 2, 3)) Then
                          'On Error GoTo err
                          Dim s As String
                          Dim g_name As String
                         s = g_file.Name
                         g_name = Mid(g_file.Name, 5, Len(s) - 4)
                         If fso1.FileExists(j.Path & g_name) Then
                             g_file.Name = g_rename1(j.Path, g_name)
                         Else
                             g_file.Name = g_name
                         End If
                        'g_file.Name = Mid(g_file.Name, 5, Len(g_file.Name) - 4)
                         List1.AddItem g_file.Name
                       
                    Else
                         List1.AddItem g_file.Name
                      
                    End If
             Next
        End If
    err:
    'MsgBox "文件重名!"End SubPrivate Sub Form_Resize()
        Form1.Width = 7000
        Form1.Height = 5600
        
    End Sub
    Private Function g_rename1(p_path As String, s As String) As String
     Dim fso As New FileSystemObject
        Dim w As String
        Dim z As String
        Dim t As Integer
        Do While (1)
            t = t + 1
           w = Mid(s, 1, Len(s) - 4)
           z = Mid(s, Len(s) - 4, 4)
            w = w & "(" & t & ")"
            If Not fso.FileExists(p_path & w) Then
                    g_rename1 = w + z
                    Exit Do
            End If
        Loop
    End FunctionPrivate Function g_rename(p_path As String, s As String) As String
        Dim fso As New FileSystemObject
        Dim w As String
        Dim t As Integer
        Do While (1)
            t = t + 1
            w = s
            w = w & "(" & t & ")"
            If Not fso.FolderExists(p_path & w) Then
                    g_rename = w
                    Exit Do
            End If
        Loop
    End Function
    Private Function rename(filename As String) As String
        Dim i As Integer
        i = InStr(filename, 1, ".")
        rename = Mid(filename, 1, i) & "(" & i & ")" & Mid(filename, i, Len(filename) - i)
    End Function