Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Replace Invalid/Illegal/UnSupport Characters in OneDrive for Business

$
0
0

 

Following Script can be set as a Scheduled task on End User Computers. This script will check for Invalid File Names inside OneDrive([!&{}~#%]) and Rename them. It also stores the Mapping file containing Old name and new name in End User OneDrive(OneDrive Mapping File.csv). Mapping file is appended and not recreated.

 

 

$OneDriveLocation =  $Env:OneDrive
$SyncFile = $OneDriveLocation + “OneDrive Mapping File.csv”

function CheckSyncFile()
{
    $GetSyncFile = Get-ChildItem $SyncFile -ErrorAction silentlycontinue
    if($GetSyncFile -eq $null)
    {
    $Columns = “Orignal File Name ” + “;” + “Updated File Name” + “;” + “Date”
    $columns | out-file -filepath $SyncFile -append
    }
    else
    {
    }

}

function RenameFile($RenameFileTo)
{
$OrignalFile = $Item.FullName
Rename-Item $item.FullName -NewName ($RenameFileTo)
$ChangeTime = Get-Date -Format “MM-dd-yyyy_hh-mm-ss”

$output = $OrignalFile + “;” + $item.Directory.FullName.ToString() + “” + $RenameFileTo + “,” + $ChangeTime
$output | out-file -filepath $SyncFile -append

}

function FindDuplicate ($NewFileName)

{

$TestFile = $item.Directory.FullName + “” + $newFileName
$CheckDup = Get-ChildItem $TestFile -ErrorAction silentlycontinue
    if($CheckDup -eq $null)
    {
    $RenameFileTo = $NewFileName
    }
    else
    {
    $logTime = Get-Date -displayhint time
    $logTime = $logTime.Minute.ToString()  + $logTime.Second.ToString()
    $SplitExtension = $NewFileName.Split(“.”)
    $FileName = $SplitExtension[0].ToString() + $logTime
    $RenameFileTo = ($NewFileName -replace $SplitExtension[0], $FileName)
    }
   
    RenameFile $RenameFileTo

}

function CheckFileNames ($item)
{

        if($Item.Name -match $UnsupportedChars)
        {
            $newFileName = $item.Name
            if ($Item.Name -match “&”) { $NewFileName = ($NewFileName -replace “&”, “and”) }
            if ($Item.Name -match “{“) { $NewFileName = ($NewFileName -replace “{“, “(“) }
            if ($Item.Name -match “}”) { $NewFileName = ($NewFileName -replace “}”, “)”) }
            if ($Item.Name -match “~”) { $NewFileName = ($NewFileName -replace “~”, “-“) }
            if ($Item.Name -match “#”) { $NewFileName = ($NewFileName -replace “#”, “-“) }
            if ($Item.Name -match “%”) { $NewFileName = ($NewFileName -replace “%”, “-“) }
            if ($Item.Name -match “!”) { $NewFileName = ($NewFileName -replace “!”, “-“) }
  
               if ($NewFileName -ne $item.Name)
                    {
                        FindDuplicate $NewFileName
                    }
              
        }
       

}

 

 

CheckSyncFile

$Items = Get-ChildItem $OneDriveLocation -Recurse -Exclude “OneDrive Mapping File.csv”
$UnsupportedChars = ‘[!&{}~#%]’

    foreach ($item in $items)
    {
 
    CheckFileNames $item
        
    }
     

 

Credit https://gallery.technet.microsoft.com/office/Check-for-unsupported-6676929a


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>