0

I am trying to upload a few hundred folders each with files inside of them into SharePoint, but unfortunately SharePoint doesn't allow any special characters like "%".

I'm trying to use a VBA code that can automatically go into each subfolder and replace any special characters contained within the files such as "%", "#", etc.

So far what I have is:

Sub ChangeFileName()

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("C:\Users\Documents\TEST\Subfolder")
'Currently the way I have it requires me to change my path a few hundred times
For Each File In Folder.Files
    sNewFile = File.Name
    sNewFile = Replace(sNewFile, "%", "_")
    sNewFile = Replace(sNewFile, "#", "_")
'^and so on`
    If (sNewFile <> File.Name) Then
        File.Move (File.ParentFolder + "\" + sNewFile)
    End If

Next

End Sub

However for the script above, you need the specific sub-folder path. Wondering if there's any way to automatically replace the special characters of files within subfolders. I can also paste all the specific subfolder paths into column A of my Excel worksheet if that helps.

Thank you!

  • see if this helps: https://stackoverflow.com/questions/22645347/loop-through-all-subfolders-using-vba – Scott Craner Jun 08 '17 at 15:21
  • I've tried tweaking it to that but I think my problem is I don't know exactly where to merge the two scripts. I assumed under the "For Each File in Folder.Files", I would just add "sNewFile = File. Name" and "sNewFile = Replace (sNewFile, "%", "_") but it doesn't work Also just a clarification question: For the code in the link you posted, I noticed that they don't have a sub name and end sub for the first part before the "sub" starts. So I added it is that correct? – user8087933 Jun 08 '17 at 15:34

1 Answers1

0

I use this code

Sub GetFileFromFolder()

    Dim fd As FileDialog
    Dim strFolder As String
    Dim colResult As Collection
    Dim i As Long, k As Long
    Dim vSplit
    Dim strFn As String
    Dim vR() As String
    Dim p As String
    Dim iLevel As Integer, cnt As Long



    'iLevel = InputBox(" Subfolder step : ex) 2 ")
        p = Application.PathSeparator
        Set fd = Application.FileDialog(msoFileDialogFolderPicker)
        With fd
            .Show
            .InitialView = msoFileDialogViewList
            .Title = "Select your Root folder"
            .AllowMultiSelect = False

            If .SelectedItems.Count = 0 Then
            Else
                strFolder = .SelectedItems(1)
                Set colResult = SearchFolder(strFolder)

                i = colResult.Count

                For k = 1 To i

                    vSplit = Split(colResult(k), p)
                    strFn = vSplit(UBound(vSplit))
                    strFn = Replace(strFn, "%", "_")
                    strFn = Replace(strFn, "#", "_")

                    'If UBound(vSplit) - UBound(Split(strFolder, p)) = iLevel Then
                        cnt = cnt + 1
                        ReDim Preserve vR(1 To 3, 1 To cnt)
                        On Error Resume Next
                        Err.Clear
                        Name colResult(k) As strFolder & strFn
                        vR(1, cnt) = colResult(k)

                        If Err.Number = 58 Then
                            strFn = Split(strFn, ".")(0) & "_" & vSplit(UBound(vSplit) - 1) & "_" & Date & "." & Split(strFn, ".")(1)
                            Name colResult(k) As strFolder & strFn
                            vR(2, cnt) = strFolder & strFn
                            vR(3, cnt) = "Changed name " 'When filename is duplicated chage filename
                        Else
                            vR(2, cnt) = strFolder & strFn
                        End If
                   ' End If
                Next k

                ActiveSheet.UsedRange.Offset(1).Clear
                Range("a3").Resize(1, 3) = Array("Old file", "New file", "Ect")
                If cnt > 0 Then
                    Range("a4").Resize(cnt, 3) = WorksheetFunction.Transpose(vR)
                End If
                 With ActiveSheet.UsedRange
                    .Borders.LineStyle = xlContinuous
                    .Columns.AutoFit
                    .Font.Size = 9
                End With
            End If
        End With
        MsgBox cnt & " files moved!! "
End Sub
Function SearchFolder(strRoot As String)
    Dim FS As Object

    Dim fsFD As Object
    Dim f As Object
    Dim colFile As Collection
    Dim p As String

    On Error Resume Next
    p = Application.PathSeparator
    If Right(strRoot, 1) = p Then
    Else
        strRoot = strRoot & p
    End If

    Set FS = CreateObject("Scripting.FileSystemObject")
    Set fsFD = FS.GetFolder(strRoot)
    Set colFile = New Collection
    For Each f In fsFD.Files
        colFile.Add f.Path
    Next f

        SearchSubfolder colFile, fsFD


    Set SearchFolder = colFile
    Set fsFD = Nothing
    Set FS = Nothing
    Set colFile = Nothing

End Function
Sub SearchSubfolder(colFile As Collection, objFolder As Object)
    Dim sbFolder As Object
    Dim f As Object
    For Each sbFolder In objFolder.subfolders
        SearchSubfolder colFile, sbFolder
        For Each f In sbFolder.Files
            colFile.Add f.Path
        Next f
    Next sbFolder

End Sub
Dy.Lee
  • 7,527
  • 1
  • 12
  • 14