I am trying to create nested folders in a library based on an input. Below is my code. This works for all other values except few. I am not getting what wrong i do. It works for values less than 50 characters but not above it. But if i create a folder manually, it gets created through UI.
Creating the Log File
$Logfile = ".\Log-Success-$(get-date -format MMddyyHHmmss)-list.csv"
Function LogWrite { Param ([string]$logstring) Add-content $Logfile -value $logstring
}
Importing Partner Name list in CSV
$documentlib =Import-Csv -Path ".\PartnerFolderName.csv"
site collection URL
$webUrl = "http://someurl/rec/site1" $web = Get-SPWeb $webUrl
Input Name of New Record Type through Console
$folderNamePrefix = Read-host “Enter the new Value ”; # here i input the value "the value is always this big in thes feild"
Check whether Record Type name has only Alphanumeric characters with spaces and Hyphen included
if ($folderNamePrefix -match '^[\da-zA-Z -]+$') {
Write-Host $folderNamePrefix " is a Valid Value"
Fetching Each Partner name from CSV to Create Folder
foreach($lib in $documentlib) { $list = $web.Lists[$lib.DocumentLibrary]
Creating One subfolder within the folder
$subFolderURL=$webUrl+"/"+$lib.DocumentLibrary+"/folder1/folder2" # Location to create New Folder try {
$folder = $list.AddItem($subFolderURL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $folderNamePrefix) $folder.Update()
$SuccessMSG=$lib.DocumentLibrary +",The Folder structure for " +$folderNamePrefix +" is Created Successfully." LogWrite $SuccessMSG
}
If an Error is Encountered
catch {
$errormessage = $_.Exception.Message $MSG =$lib.DocumentLibrary +","+$errormessage LogWrite $MSG
} }
}
If value is Invalid and contains special characters
Else {
Write-Host $folderNamePrefix " contains Special Charecters that are restricted"
}
$web.Dispose() $documentlib =$null $folderNamePrefix =$null $folder=$null $list=$null $FolderPath=$null