5

I got the the following code from stack overflow and it works fine.

$TargetFolder = “Pathofyourfolder”
$Files = Get-ChildItem $TargetFolder -Exclude (gc List.txt)  -Recurse
foreach ($File in $Files)
{ 
    write-host “Deleting File $File” -foregroundcolor “Red”;
    Remove-Item $File | out-null
}

Now I want to delete the files with file names on the list. I tried some variations of the above such as replacing Exclude with Include but without success. Can anyone help please?

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
C Lamb
  • 51
  • 1
  • 1
  • 3

4 Answers4

8
$targetFolder = "D:\TEST_123"
$fileList = "D:\DeleteList.txt"

Get-ChildItem -Path "$targetFolder\*" -Recurse -Include @(Get-Content $fileList) | Remove-Item -Verbose

For -Include to work you should specify \* at the end of a folder name and filename with extension in your deletion list. The code above works for me, deleting only specified files in folder and all of its subfolders.

I also used -Verbose instead of foreach and Write-Host.

n01d
  • 1,047
  • 8
  • 22
  • 1
    ++; Appending `\*` to the input path when using `-Include` and `-Exclude` is a good habit to form (but you wouldn't strictly need it here, because `-Recurse` is also present.) – mklement0 Jul 21 '16 at 06:56
8

You can try this :

 Get-Content .\filesToDelete.txt | ForEach-Object {Remove-Item $_}

it's easier and pretty self explanatory ($_ stands for the current variable of the for loop).

Oussama K
  • 81
  • 1
  • 2
  • https://stackoverflow.com/a/25766593/1193727 - for explanation. – resnyanskiy Aug 08 '21 at 06:48
  • 1
    The premise of the question is that the `.txt` file contains file _names_ only, so your command will invariably target files in the _current_ directory, whereas the command in the question targets a specific directory with `$TargetFolder`. Also, the _recursion_ aspect (`-Recurse`) is missing from your command. – mklement0 Nov 30 '22 at 02:47
2

To offer a simplification of n01d's helpful answer:

You can use Remove-Item directly:

Remove-Item $TargetFolder\* -Recurse -Include (Get-Content List.txt) -Verbose

Note the required \* appended to $TargetFolder.

-Include and -Exclude can be tricky (see this answer), but -Include should work here, as long as List.txt contains mere filenames (no path components).

As in n01d's answer, -Verbose is meant to replace the explicit foreach loop with the Write-Host calls.

Also note that Remove-Item writes nothing to the output stream, so there's no reason to pipe it to Out-Null

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • How would you provide the list in the same line? Instead of `(Get-Content List.txt)`? – not2qubit Nov 30 '22 at 01:12
  • @not2qubit, I don't understand: The command in this answer already does provide the `-Include` argument as part of the same line, with value `Get-Content List.txt) `. In case you're asking how to provide the list via a _variable_, say `$list`, it is as simple as `-Include $list`. – mklement0 Nov 30 '22 at 02:39
  • No, I mean, how to just specify the files, I mean something like: `rm file1.txt, file2.c, dir3` etc. – not2qubit Nov 30 '22 at 13:36
  • 1
    @not2qubit, `Remove-Item -LiteralPath file1.txt, file2.c, dir3 ...`. If you omit `-LiteralPath`, the array will bind to `-Path`, which _typically_ also works, but causes problems with file names containing `[` and `]` – mklement0 Nov 30 '22 at 13:54
1

I find that the -Include param really doesn't work the way you would expect, most of the time.

So I'd propose this code to simply get it working, fast.

$TargetFolder = “Pathofyourfolder”
$Files = Get-ChildItem $TargetFolder -Recurse| Where Name -in (gc List.txt) 
foreach ($File in $Files)
{ 
    write-host “Deleting File $File” -foregroundcolor “Red”;
    Remove-Item $File | out-null
}

You could make it a bit faster if you'd like by screwing with -include, but I frankly think it sucks, and this would work.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • "-include" does work. The program used to derive file names from the files themselves was adding spaces. Apparently not doing this earlier but now fixed. My apology and thanks to all respondents. I used to do this with bat files and DOS commands but seems not possible on Windows 10. Good to know there are people willing to help. – C Lamb Jul 23 '16 at 04:20