Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Find and Replace

$
0
0

Hi, I'm doing a find a replace and when I run the below I get this error.  Any suggestions for how to get around this file limitation?  I'm running this Powershell as administrator and running Set-ExecutionPolicy Remotesigned first byitself.

 

 

 

Script:

 

 

$exclude = @("*.cspkg", "*.doc", "*.docx", "*.txt")

$excludeMatch = @("Archive")

$from = "\\servername\foldername\foldername\foldername"

$to = "\\servername\foldername\foldername\foldername\recover"

Get-ChildItem -Path $from -Recurse -Exclude $exclude | 

          where { $excludeMatch -eq $null -or $_.FullName.Replace($from, "") -notmatch $excludeMatch } |

          Copy-Item -Destination {

            if ($_.PSIsContainer) {

              Join-Path $to $_.Parent.FullName.Substring($from.length)

            } else {

              Join-Path $to $_.FullName.Substring($from.length)

            }

           } -Force -Exclude $exclude 

 

Start-Sleep -Second 45 

 

Get-ChildItem "\\servername\foldername\foldername\foldername\recover" -recurse |

    Foreach-Object {

       $c = ($_ | Get-Content) -join "`r`n"

 

        $c = $c.Replace('value1','value2')

 

        $c = $c.Replace('value1','value2')

 

        $c = $c.Replace('value1','value2')

 

        [IO.File]::WriteAllText($_.FullName, $c)

    }

 

Errors:

 

1.  

 

Copy-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characte

rs, and the directory name must be less than 248 characters.

At line:7 char:20

+           Copy-Item <<<<  -Destination {

    + CategoryInfo          : NotSpecified: (:) [Copy-Item], PathTooLongException

    + FullyQualifiedErrorId : System.IO.PathTooLongException,Microsoft.PowerShell.Commands.CopyItemCommand

 

 

 

2.  

 

Get-Content : Access to the path '\\servername\foldername\folddername\foldername\foldername\foldername' is denied.

At line:19 char:30

+        $c = ($_ | Get-Content <<<< ) -join "`r`n"

    + CategoryInfo          : PermissionDenied: (\\servername\folder...es\recover\foldername:String) [Get-Content], UnauthorizedAccess 

   Exception

    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

 

 3.  I basically want files dumped inot the 'recover' directory with the replaced values but when I go to the recover folder I have tons of extra folders that I don't need?

 

\\Servername\FolderName\recover\Folder1\Folder2\SameFolder1\SameFolder2\SameFolder1\SameFolder2


Viewing all articles
Browse latest Browse all 6937

Trending Articles