简体   繁体   中英

Copy specific type files from subfolders using a Powershell script

I have many folders, some of which have subfolders. I want to find and copy to special directory all files of one type, for example .txt, from them. I use this script and it works, but it copies all folders on the way to my file too. How can I avoid this? I thought about deleting all empty folders after copying, but I'm sure that the more right way exists.

$folders = [IO.Directory]::GetDirectories("S:\other","*","AllDirectories")

Foreach ($dir in $folders) {
    Copy-Item -Path $dir -Filter *.txt -Recurse -Destination 'D:\FinishFolder'
}

使用Get-ChildItem发现文件,然后Get-ChildItem复制它们:

Get-ChildItem S:\other -Filter *.txt -Recurse |Copy-Item -Destination 'D:\FinishFolder'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM