Home > Net >  Unsupported Characters
Unsupported Characters

Time:11-18

i have a huge amount of data which has Filenames with Unsupported Characters since i wanna move these Files to a different Location which is kinda impossible without renaming those. When running my code he gives error that he kind of cant find the Document he wants to rename Thanks in advance for any assistance.

function AdjustFilename {
    param (
        [String]$Path
    )

    if ($Path.Chars($Path.Length - 1) -ne '\') { $Path = $Path   '\' }

    

    $filelist = get-ChildItem  -LiteralPath $path -Recurse -File
    foreach ($filename in $filelist) {
    
        [String]$newfilename = $filename

        $newfilename = $newfilename.Replace('?', '').Replace('/', '').Replace('\', '').Replace('{', '').Replace('}', '').Replace('<', '').Replace('>', '').Replace('|', '').Replace(':', '').Replace('*', '')

        $fullpath =  $filename.Fullname

        if ("$filename" -ne "$newfilename") {
           rename-item -LiteralPath $fullpath -NewName $newfilename

        }

    }

    $pathlist = get-ChildItem -LiteralPath $path -Recurse -Directory
    foreach ($subpath in $pathlist) {
        AdjustFilename "$($Path)$($subpath)"
    }
    
}

AdjustFilename "C:\Temp\"

CodePudding user response:

I have made a few adjustments, the $filename was an object. And since you already use -Recurse, you don't need the bottom part because it will do things double.

function AdjustFilename {
    param (
        [String]$Path
    )

    if ($Path.Chars($Path.Length - 1) -ne '\') { $Path = $Path   '\' }

    

    $filelist = get-ChildItem  -LiteralPath $path -Recurse -File
    foreach ($filename in $filelist) {
    
        [String]$newfilename = $filename.Name

        $newfilename = $newfilename.Replace('?', '').Replace('/', '').Replace('\', '').Replace('{', '').Replace('}', '').Replace('<', '').Replace('>', '').Replace('|', '').Replace(':', '').Replace('*', '')

        $fullpath =  $filename.Fullname

        if ("$filename" -ne "$newfilename") {
           rename-item -LiteralPath $fullpath -NewName $newfilename
        }

    }
}

AdjustFilename "C:\Temp\"

CodePudding user response:

.NET has a very handy method which will return all invalid filename characters. You can use that to create a regular expression string to replace them all with nothing:

function Remove-InvalidFilenameCharacters {
    param (
        [String]$Path
    )
    # create a regex to replace the invalid filename characters
    $invalidChars = '[{0}]' -f [RegEx]::Escape([System.IO.Path]::GetInvalidFileNameChars())
    $filelist = Get-ChildItem  -LiteralPath $Path -Recurse -File
    foreach ($file in $filelist) {
        # remove all invalid characters from the file name
        $newfilename = $file.Name -replace $invalidChars
        # you don't have to test if the newname is different, because if this
        # is the case, Rename-Item doesn't do anything to that file (No-Op)
        $file | Rename-Item -NewName $newfilename
    }
}

Remove-InvalidFilenameCharacters "C:\Temp"
  • Related