1

In a previous post, Delete Files in subfolders after encode, I got a working script, I confirmed it deleted one file in a test folder I created, but in my normal folders the script (as standalone as well as implemented in my main script) fails to delete the files while giving an error message like this: "Missing permissions...; You don't have the permissions and so on" when running the scripts as admin I get the same error. What can I do to fix this?

The Script I got:

Get-ChildItem *.mkv | where BaseName -notlike '*`[HEVC]' | foreach {
    # Convert the input file and send ffmpeg's output to the display,
    # by piping to Write-Host, rather than trough the pipeline.
    ffmpeg -i $_ -c:v libx265 -c:a copy -x265-params crf=25 "$($_.BaseName) [HEVC].mkv" -n |
       Write-Host

    # Conversion OK? Output the file-info object, which pipes it to Remove-Item.
    if ($LASTEXITCODE -eq 0) { $_ } 
 } | Remove-Item 

My script for all folders:

$current_path = Get-Location
$directories = get-childitem -path $current_path -Recurse -Directory
Foreach ($dir in $directories) {
    Set-Location $dir.fullname
    & "G:\Folder1\Compressing ps1 and bat\ffmpeg powershell HEVC.ps1"   
}
Matt
  • 3,658
  • 3
  • 14
  • 27
WorldTeacher
  • 93
  • 1
  • 10
  • I've updated my answer with the resolution. Assuming you agree with this summary, I suggest we bring closure to this; that is, please accept the answer. – mklement0 Nov 05 '20 at 22:56

1 Answers1

0

Note:

  • The OP's problem turned out the inability to delete files, due to permission problems specific to their environment.

    • Adding the -Force switch to the Remove-Item file-deletion call in the script resolved the issue, suggesting that the files had the ReadOnly file attribute set (with, e.g., attrib +R $file) - which is the only permission-related problem that -Force can overcome.
  • The answer below may still be of interest if you're dealing with permission errors related to directories that you'd like to ignore.


There are directories that even administrators aren't allowed to access:

  • There are hidden system junctions for backward compatibility in each user profile, as described in this answer; however, this doesn't apply to you, since those are hidden and your Get-ChildItem doesn't include -Force so as to include hidden items.

  • In the Windows directory (typically, C:\Windows), there are inaccessible subdirectories that aren't hidden, such as C:\WINDOWS\CSC\v2.0.6 and C:\WINDOWS\System32\LogFiles\WMI\RtBackup.

  • It is generally possible to set permissions to exclude even administrators.

Since in all these cases it is fair to assume that no files of interest are to be found there, you can choose to (initially) ignore these errors:

$current_path = Get-Location
$directories = Get-Childitem -ErrorAction SilentlyContinue -ErrorVariable errs -Path $current_path -Recurse -Directory
foreach ($dir in $directories) {
  Set-Location -ErrorAction SilentlyContinue -ErrorVariable +errs $dir.fullname
  if (-not $?) { continue }
  & "G:\Folder1\Compressing ps1 and bat\ffmpeg powershell HEVC.ps1"   
}

This will silence errors (-ErrorAction SilentlyContinue), but collect them in the (self-chosen) $errs variable (-ErrorVariable errs / -ErrorVariable +errs, the latter appends).
A quick way of printing these errors by their message without too much "noise" is to run
$errs | ForEach-Object ToString.

If you're confident that all errors stem from to-be-ignored permission errors, you can omit the
-ErrorVariable arguments altogether and replace -ErrorAction SilentlyContinue with
-ErrorAction Ignore, which simply quietly discards any errors.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224119/discussion-between-worldteacher-and-mklement0). – WorldTeacher Nov 04 '20 at 20:33