Hi guys,
New to the forums and looking for some assistance probably from an experienced VBScript programer. I'm looking to have a VBScript that runs on a folder that is selected by the user and runs on all subfolders checking the documents file path length to ensure it is under 256. This is the max characters acceptable in sharepoint. It also has been setup so that %20 is entered anywhere there is a space. Need it to check for special characters that sharepoint doesn't accept as well.
Below is the program, can't figure out what is wrong. Just won't seem to work on subfolders properly. Any assistance is greatly appreciated.
Full program uploaded here:
New to the forums and looking for some assistance probably from an experienced VBScript programer. I'm looking to have a VBScript that runs on a folder that is selected by the user and runs on all subfolders checking the documents file path length to ensure it is under 256. This is the max characters acceptable in sharepoint. It also has been setup so that %20 is entered anywhere there is a space. Need it to check for special characters that sharepoint doesn't accept as well.
Below is the program, can't figure out what is wrong. Just won't seem to work on subfolders properly. Any assistance is greatly appreciated.
Full program uploaded here:
Code:
Option Explicit
Private intLongestName As Integer
Private intCount As Integer
Sub processfolder()
Dim strFldr As String
Dim fso As Object
intLongestName = 0
Set fso = CreateObject("Scripting.FileSystemObject")
strFldr = GetFolder
If Len(strFldr) = 0 Then Exit Sub
Process_folder fso, strFldr, True
MsgBox "Process complete. " & CStr(intCount) & " files reviewed." & vbNewLine & vbNewLine & _
"The longest file name is " & CStr(intLongestName) & " characters long." & vbNewLine & vbNewLine & _
"The File Name and Path cannot exceed 255 characters."
End Sub
Sub Process_folder(fso As Object, strFldr As String, Optional blnResetCount As Boolean = False)
Dim fldr As Object
Dim fldrSub As Object
Dim fl As Object
Set fldr = fso.GetFolder(strFldr)
intCount = 0
For Each fl In fldr.Files
intCount = intCount + 1
If Len(fl.Name) > intLongestName Then
intLongestName = Len(Replace(fl.Name, " ", "%20"))
Debug.Print fl.Name
End If
ValidFileName fl.Name
Next
For Each fldrSub In fldr.SubFolders
Process_folder fso, fldrSub.Path
Next
End Sub
Sub protectFile()
ActiveDocument.Protect wdAllowOnlyFormFields
End Sub
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = "h:\"
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Sub ValidFileName(strFile As String)
Dim blnValidFile As Boolean
blnValidFile = True
If InStr(1, strFile, "~", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Tilde (~) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "#", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Number sign (#) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "%", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Percent (%) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "&", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Ampersand (&) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "*", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Asterisk (*) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "{", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Left Brace ({) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "}", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Right Brace (}) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "\\", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Double Backslash (\\) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, ":", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Colon (:) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "<", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Left Angle brackets (<) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, ">", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Right Angle brackets (>) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "?", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Question mark (?) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "/", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Slash (/) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "+", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Plus sign (+) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, "|", vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Pipe (|) character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
If InStr(1, strFile, Chr(34), vbTextCompare) > 0 Then
MsgBox "The file name: " & strFile & " cannot have the Quotation mark (" & Chr(34) & ") character when uploading to SharePoint.", vbCritical, "Check File Name"
blnValidFile = False
End If
blnValidFile = CheckFileName(strFile, ".Files", blnValidFile)
blnValidFile = CheckFileName(strFile, "_files", blnValidFile)
blnValidFile = CheckFileName(strFile, "-Dateien", blnValidFile)
blnValidFile = CheckFileName(strFile, "_fichiers", blnValidFile)
blnValidFile = CheckFileName(strFile, "_bestanden", blnValidFile)
blnValidFile = CheckFileName(strFile, "_file", blnValidFile)
blnValidFile = CheckFileName(strFile, "_archivos", blnValidFile)
blnValidFile = CheckFileName(strFile, "-filer", blnValidFile)
blnValidFile = CheckFileName(strFile, "_tiedostot", blnValidFile)
blnValidFile = CheckFileName(strFile, "_pliki", blnValidFile)
blnValidFile = CheckFileName(strFile, "_soubory", blnValidFile)
blnValidFile = CheckFileName(strFile, "_elemei", blnValidFile)
blnValidFile = CheckFileName(strFile, "_ficheiros", blnValidFile)
blnValidFile = CheckFileName(strFile, "_arquivos", blnValidFile)
blnValidFile = CheckFileName(strFile, "_dosyalar", blnValidFile)
blnValidFile = CheckFileName(strFile, "_datoteke", blnValidFile)
blnValidFile = CheckFileName(strFile, "_fitxers", blnValidFile)
blnValidFile = CheckFileName(strFile, "_failid", blnValidFile)
blnValidFile = CheckFileName(strFile, "_fails", blnValidFile)
blnValidFile = CheckFileName(strFile, "_bylos", blnValidFile)
blnValidFile = CheckFileName(strFile, "_fajlovi", blnValidFile)
blnValidFile = CheckFileName(strFile, "_fitxategiak", blnValidFile)
End Sub
Function CheckFileName(strFileName As String, strTrailingtext As String, blnValid As Boolean) As Boolean
If StrComp(Right(strFileName, Len(strTrailingtext)), strTrailingtext, vbTextCompare) = 0 Then
MsgBox "The file name: " & strFileName & " cannot end with " & strTrailingtext & ".", vbCritical, "Check File Name"
CheckFileName = False
Exit Function
End If
CheckFileName = blnValid
End Function