Home > Software engineering >  How to get all the files in a specific folder?
How to get all the files in a specific folder?

Time:06-11

I want to grab all the files from my computer with the word car in the filename. I want to grab all the files inside a folder with the foldername car too, even when those files doesn't contain car in their filename.

I have wrote the first part, so I can grab all the files from my computer with the word car. However I don't know how I can do the second part.

Get-ChildItem “C:\” -Recurse -filter *car*  | Copy -Destination  ”C:\Test”

CodePudding user response:

Use two Get-ChildItem calls, processing only files first, via the -File switch, and then only directories, using -Directory:

# Files only
Get-ChildItem -File C:\ -Recurse -Filter *car* |
  Copy -Destination C:\Test -WhatIf

# Directories only: for each matching dir.,
# copy its (immediate) files.
Get-ChildItem -Directory C:\ -Recurse -Filter *car* | 
  Get-ChildItem -File |
  Copy -Destination C:\Test -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

  • Related