locked
Remove folders in PowerShell errors out with long file names RRS feed

  • Question

  • If i try to remove a directory with PowerShell and there is a long file name it fails.

    I have found the below workaround by using a dos command however i can no longer use the Try -Erroraction and catch function.

    Is there anyway around this with .net 4.5?

    Thanks!  

    try {Remove-Item $env:temp\errors.txt} catch {""}
    Write-Output "" | Out-File $env:temp\errors.txt
    
    import-csv "$env:temp\pathname.txt" -delimiter "," | export-csv "$env:temp\worklog.csv" -NoTypeInformation
    
    $Mylist = (Import-csv "$env:temp\worklog.csv")
    
    Foreach ($Line in $Mylist) {
    
    
    $WPATH = $Line.path
    $WFOLDER = $lINE.NAME
    
    wRITE-HOST "$Wpath\$Wfolder"
    # try {Remove-Item -Recurse -Force  "$Wpath\$Wfolder"  -ErrorAction Stop
    try {Cmd /C "rmdir /S /Q "$Wpath\$Wfolder""  
    Write-Host "Removed $Wpath\$Wfolder" }
     catch {Write-Output "Failed removing $Wpath\$Wfolder" |Out-File $env:temp\errors.txt -Append
    }
    
    }
    
    

    Sunday, February 8, 2015 12:12 AM

All replies