0
votes

My folder structure looks like this:

SourceFolder
├─file1.txt
├─file1.doc
└─Subfolder1
  ├─file2.txt
  ├─file2.doc
  └─SubSubFolder
    ├─file3.txt
    └─doc3.txt

This script copies all *.txt files from folders, whose (folder) names contains the eng, to a destination folder. Only the files inside the folder.

$dest = "C:\Users\username\Desktop\Final"
$source = "C:\Users\username\Desktop\Test1"

Get-ChildItem $source -Filter "*.txt" -Recurse |
    Where-Object { $_.DirectoryName -match "eng" } |
    ForEach-Object { Copy-Item $_.fullname $dest }

In my situation the folders are in .rar format and I want the script to search .rar folders and copy the *.txt files from folder eng to destination. Is that possible with PowerShell?

1

1 Answers

0
votes

Rar is a proprietary archive format. Your .rar items aren't folders but compressed archives of that format. PowerShell can't transparently handle such archives, and I'm not aware of a third-party provider that would add this functionality.

Basically, no, what you're asking isn't possible.

What you can do is extract files from the archive with tools like 7-zip, e.g.:

7z.exe e your.rar *.txt -r

For extracting only files from a nested subfolder inside the archive prepend the extract pattern with that path:

7z.exe e your.rar "nested\subfolder\*.txt" -r