0

I have regular Jobs to clean up old User Windows-Profile directories on a central NAS. Last time I had Directories containing double square Brackets in the path (Import from Macromedia or so). This looks like: \server.ad.local\HOME1\Username\System\Anwendungsdaten\Macromedia\FlashPlayer\SharedObjects\G5EUZBX2\www.daserste.de\[[IMPORT]]\players.edgesuite.net\flash\plugins\osmf\advanced-streaming-plugin\v3.4\osmf2.0\AkamaiAdvancedStreamingPlugin.swf\HDCore.sol

As this Job should run automatically with Powershell I tired various things:

  1. Tried to replace the brackets an rename the folder - no success
  2. Tried LiteralPath for Remove-Item - no success
  3. Tried to Deleted with Scripting.FileSystemObject - no success

I always get the following error Message: The Element ... cannot be removed: A part of the Path "HDCore.sol" cannot be found.

Are there any ideas?

Tried to rename the Folder, tried remove-item with -LiteralPath, tried to use FileSystemObject. All of the actions gave the same result: Error

Just to complete: here are the functions I used last:


Function RemoveChar
{
    Param ($Path)
    $Pattern = '#|&|%|\[{1,}|\]{1,}|\^|\s|\.{2,}'
    if ($Path -imatch $Pattern){
        Rename-Item -Path $Path -NewName ($Path -replace $Pattern,'') -ErrorAction SilentlyContinue
        return ($Path -replace $Pattern,'')
    } else {
        return $Path
    }
}
 
Function Truncate
{
    Param ($Path)
    $total_path_length_threshold = 248  # maximal erlaubte Zeichenzahl (248 für Verzeichnisse, 260 für Dateien)
    $characters_to_truncate = 60        # Anzahl der zeichen, um die der name zu kürzen ist. Ein eindeutiger Index wird angehangen
    $virtual_drive = "v:"               # Für temp. Prozessing muss das Format "v:" haben
    $collection = cmd /c dir $Path /s /b |? {$_.length -gt $total_path_length_threshold }
    $count_paths = ($collection | measure).count - 1
   
    foreach ($pathlong in $collection) {
        $parent_path = Split-path -path $pathlong
        subst $virtual_drive $parent_path
        $leaf = split-path -leaf $pathlong
        $short_virtual_path = join-path $virtual_drive $leaf
        $item = Get-Item -LiteralPath $short_virtual_path
        if (Test-Path -LiteralPath $item) {
            $filename = $item.name
            $filename_extension = $item.Extension
            $basename = $item.BaseName   
            $basename_length = $basename.length
            $new_index = "X" + $counter + "X"
            $adjusted_characters_to_truncate = $characters_to_truncate + $new_index.length
            if ( $basename_length -gt $adjusted_characters_to_truncate ) {
                $length_to_use = $basename_length - $adjusted_characters_to_truncate
                $new_filename = $basename.substring(0, $length_to_use ) + $new_index + $filename_extension
                $new_path = $parent_path + $new_filename
                $Path = $new_path
                Rename-Item -LiteralPath $short_virtual_path -NewName $new_filename  
            }
        }
        subst v: /d
    }
    return $Path
}
 
Function removeRecursive
{
    param([String] $Path)
    $fso = new-object -com "Scripting.FileSystemObject"
 
    function proc {
         param($folder)
         $folder.Files | foreach-object {
         RemoveChar $_
         }
         $folder.Files | foreach-object {
            $short = $fso.GetFile($_.FullName).ShortPath
            LogWrite "$FullDate : Processing: $short"
            $fso.DeleteFile($short,$true)
         }
        $folder.SubFolders | foreach-object {
        proc $_
        }
    }
   proc $fso.GetFolder($Path)  
}
 

The function I call from main code is removeRecursive. And yes I tried Remove-Item -LiteralPath SomePath -Recursive -Force too but no success as well.

Uwe Krause
  • 13
  • 3
  • The `-Path` parameter of provider (file-handling) cmdlets (which the first _positional_ argument implicitly binds to), expects _wildcard_ expressions. Because PowerShell's wildcard language also supports expressions such as `[a-z]`, paths meant to be _literal_ ones are misinterpreted if they happen to contain `[` chars. To pass a literal path as such, use the `-LiteralPath` parameter explicitly. Alternatively, pass a file-information object such as produced by `Get-ChildItem` _via the pipeline_ to a provider cmdlet, which also binds to `-LiteralPath`. – mklement0 Nov 03 '22 at 03:35
  • If the linked duplicate doesn't help you solve your problem, please create a _new_ question post that reflects your latest attempts with the linked information in mind, ideally in the form of a [mcve]. – mklement0 Nov 03 '22 at 03:38
  • Used -LiteralPath already and the File was not deleted too. If I parse recursively thru the FSO, It stops at the [[Import]] Directory and does not show more . The LiteralPath was working with one Square Bracket, but will not work with two as it seems. – Uwe Krause Nov 03 '22 at 06:19
  • solution was to user Remove-Item2 instead Remove-Item. Don't understand why this question was marked duplicate. Even I has similarities, this was a new issue not mentioned in any other question before as the double [[ were the problem. – Uwe Krause Nov 25 '22 at 14:44
  • `[[` aren't a problem in paths, as long as you target them with `-LiteralPath`, that's why your question was marked as a duplicate. Try `$null = New-iIem -Force [[test]]; Remove-Item -LiteralPath [[test]]`. If you have a case where that _doesn't_ work, please create a new question post with a [mcve]. It's conceivable that you ran into the unrelated problem of encountering a path that was _too long_ - see [this answer](https://stackoverflow.com/a/58418901/45375). – mklement0 Nov 25 '22 at 15:05

0 Answers0