none
File Renaming for SharePoint RRS feed

  • Question

  • Has anyone any recommendations for the best way to prepare files for storing in SharePoint? Some of our users have CDs and DVDs with data coming in that needs to be moved to SharePoint but obviously there are special characters etc involved. Can you recommend what software you might use in your company? Thanks
    Monday, February 11, 2013 9:46 AM

Answers

  • Thanks for the script but I think that might be too much? We need something for every user, not just a select few. I have found simple renaming software that seems to work. Thanks so much
    • Marked as answer by SoapyJ Tuesday, February 12, 2013 12:44 PM
    Monday, February 11, 2013 11:51 AM

All replies

  • SharePoint allows you to upload documents  so in your scenario what you can do is :-

    You can zip the files and upload as one file in SharePoint


    Mark this post as answer if this resolves your issue.


    Everything about SQL Server | Experience inside SQL Server -Mohammad Nizamuddin

    Monday, February 11, 2013 9:57 AM
  • No, I don't think that zipping the files would work as the people need the ability to check in and out documents etc and open them direct from SharePoint rather than downloading zip files
    Monday, February 11, 2013 9:58 AM
  • I would use PowerShell. You can create a script that copies all the files from a local directory, into a SharePoint library, including creating the file structure and optionally adding metadata.

    As an example:

    function Copy-FilesToSP
    {
        [CmdletBinding()]
    	Param (
               [parameter(Mandatory=$true)][string]$LocalPath, 
               [parameter(Mandatory=$true)][string]$SiteUrl, 
               [parameter(Mandatory=$true)][string]$Library 
               [parameter(Mandatory=$false)][switch]$IncludeSubFolders,
               [parameter(Mandatory=$false)][switch]$Approve,
               [parameter(Mandatory=$false)][switch]$CheckIn,
               [parameter(Mandatory=$false)][switch]$Overwrite,
               [parameter(Mandatory=$false)][switch]$FlattenStructure
               )
        
        try
        {
               
    		#Get web and document library objects
    		$web = Get-SPWeb $SiteUrl
    		$docLibrary = $web.Lists[$Library]
    		$folder = $docLibrary.RootFolder
    		$files = $null;			
    		#Enumerate through all files
    		if($IncludeSubFolders) {
    			$files = Get-ChildItem $LocalPath -Recurse
    		}
    		else
    		{
    			$files = Get-ChildItem $LocalPath
    		}
            
    		$files | ForEach-Object{
    			try{
    			write-host "Processing Item:"$_.Name -foregroundcolor Yellow;
    			#Check if the object is a folder - if so, create it in doc library
    			if ($_.PSIsContainer) {
    				#write-host "Processing folder:" $_.Name -foregroundcolor Orange
    				if (($IncludeSubFolders) -and (!$FlattenStructure)) {
    					#Generate folder path for creation in SharePoint
    					#by looking at the parent folder on the local path
    					$spFolderPath = ($_.Parent.FullName.Replace($LocalPath,"")).Replace("\","/")
    					$spFolderPath = $spFolderPath.Replace("(","");
    					$spFolderPath = $spFolderPath.Replace(")","");
    					$spFolderPath = $spFolderPath.Replace("&","");										
    					$folderName = $_.Name.ToString().Replace("(","");
    					$folderName = $folderName.Replace(")","");
    					$folderName = $folderName.Replace("&","");					
    					
    					#Get the folder into which the new folder will be created
    					#by adding the folder path generated above, if one existed
    					if ($spFolderPath -eq "") {
    						$currentFolder = $web.GetFolder($folder.Url)
    					}
    					else
    					{
    						$currentFolder = $web.GetFolder($folder.Url + $spFolderPath)
    					}					
    					#Check to see if subfolder already exists
    					#and create it if not
    					$testFolder = $currentFolder.SubFolders[$folderName]
    					if ($testFolder -eq $null) {
    						write-host "Adding folder" $folderName "to" $docLibrary.Title "in" $web.Title "..." -foregroundcolor Green
    						$newFolder = $currentFolder.SubFolders.Add($folderName)											
    					}
    					else
    					{
    						write-host "Folder" $folderName "already exists in" $docLibrary.Title "and shall not be created" -foregroundcolor Cyan
    					}
    				}
    			}
    			else
    			{   
    				#write-host "Processing file:" $_.Name -foregroundcolor Orange
    				$fileTitle = $_.Name.ToString().SubString(0,($_.Name.ToString().Length - 4));
    				$fileName = $_.Name.ToString().Replace("(","");
    				$fileName = $fileName.Replace(")","");
    				$fileName = $fileName.Replace("&","");
    				$fileName = $fileName.Replace("#","");
    				$fileName = $fileName.Replace("%","");
    				$fileName = $fileName.Replace("*","");
    				$fileName = $fileName.Replace(":","");
    				$fileName = $fileName.Replace("?","");
    				$fileName = $fileName.Replace("'","");				
    				$fileName = $fileName.Replace("  "," ");
    				$fileName = $fileName.Replace(",","");
    				$fileName = $fileName.Replace("...","");
    				$fileName = $fileName.Replace("..",".");
    				if($fileName.Length > 127){$fileName = $fileName.SubString(0,123)+$fileName.SubString(($fileName.Length - 4),$fileName.Length)}
    				$fileFullPath = $_.DirectoryName+"\"+$fileName;				
    				$spFolderPath = ($_.DirectoryName.Replace($LocalPath,"")).Replace("\","/")
    				$spFolderPath = $spFolderPath.Replace("(","");
    				$spFolderPath = $spFolderPath.Replace(")","");
    				$spFolderPath = $spFolderPath.Replace("&","");					
    				#Generate file path for upload into SharePoint
    				if ($FlattenStructure) {
    					$spFilePath = ("/" + $fileName)
    				}
    				else
    				{
    					$spFilePath = ($spFolderPath+"/"+$fileName)
    				}
    				
    				$spFullPath = $folder.Url + $spFilePath
    				if($spFullPath.Length > 260){$spFullPath = $spFullPath.SubString(0,256)+$spFullPath.SubString(($spFullPath.Length - 4),$spFullPath.Length)}
    								
    				#Check if the file exists and the overwrite option is selected before adding the file
    				if ((!$web.GetFile($spFullPath).Exists) -or ($Overwrite)) {
    					#Add file
    					write-host "Copying" $_.Name "to" $spFullPath.Replace("/" + $_.Name,"") "in" $web.Title "..." -foregroundcolor Green
    					$spFile = $folder.Files.Add($spFullPath, $_.OpenRead(), $true)
    					$spItem = $spFile.Item									
    					$spItem["Title"] = $fileTitle;
    					$spItem.Update();
    			
    					#Check in file to document library (if required)
    					#MinorCheckIn=0, MajorCheckIn=1, OverwriteCheckIn=2
    					if ($CheckIn) {
    						if ($spFile.CheckOutStatus -ne "None") {
    							$spFile.CheckIn("File copied from " + $filePath, 1)
    							write-host $spfile.Name"checked in"
    						}
    					}				
    					#Approve file (if required)
    					if ($Approve) {
    						if ($spItem.ListItems.List.EnableModeration -eq $true) {
    							$spFile.Approve("File automatically approved after copying from " + $filePath)
    							if ($spItem["Approval Status"] -eq 0) { write-host $spfile.Name"approved" }
    						}
    					}
    				}
    				else
    				{
    					write-host "File"$_.Name "already exists in" $spFullPath.Replace("/" + $_.Name,"") "and will not be uploaded" -foregroundcolor Cyan
    				}
    			}
    			}
    			catch
    			{
    			write-host "Exception. Continuing with next file. Error:"$_  -foregroundcolor Red
    			return;
    			}		
    		}
        }
        catch [System.SystemException]
        { 
            write-host "The script has stopped because there has been an error.  "$_  -foregroundcolor Red
        }
        finally
        {
            $web.Dispose()
        }
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Monday, February 11, 2013 10:26 AM
  • Thanks for the script but I think that might be too much? We need something for every user, not just a select few. I have found simple renaming software that seems to work. Thanks so much
    • Marked as answer by SoapyJ Tuesday, February 12, 2013 12:44 PM
    Monday, February 11, 2013 11:51 AM
  • Others have been so kind to share with you, would you be so kind as to share the renaming software  that seems to work?
    Monday, May 16, 2016 6:30 PM
  • Hi Sorry but this was 3 years ago. I can't remember what the name was at all to be honest! I just know I did a lot of googling at the time
    Monday, May 16, 2016 6:46 PM