2

I want to implement the following functionality using either the Windows Command Prompt, Powershell, or a combination of the two:

Suppose we are given a source directory, sourcedir and a destination directory destinationdir, and we want to copy files, recursively, from the source to the destination.

We are also given a wildcard filter like so */foo/bar/*; the filter means find all files in sourcedir that contain /foo/bar/ anywhere in its path. For instance, sourcedir/foo/bar/file1.txt, and sourcedir/baz/foo/bar/file2.txt will be copied, while sourcedir/foo/baz/bar/file3.txt will not be copied.

3
  • 3
    Choose a language then think about how it might do it, try a few things, then post problems you are having to one of the groups. Commented Mar 14, 2019 at 6:15
  • ??? it's a requirement for a task I'm doing at work and the whole thing is hung up on this step. I've tried a few approaches involving Get-ChildItem and Where-Object, neither have really worked. Commented Mar 14, 2019 at 6:27
  • Actually, the root of the problem is neither Command Prompt or Powershell seem to support the regular **/ partial path wildcard Commented Mar 14, 2019 at 6:30

2 Answers 2

1

Right, figured this out with a bunch of random stuff. Turns out */foo/bar/* partial path does work in Get-ChildItem:

Get-ChildItem -Path "$($sourcedir)\$($filter)" -Recurse | Copy-Item -Destination $destinationdir

Sign up to request clarification or add additional context in comments.

1 Comment

May I suggest you start using either PowerShell Join-Path or .Net [[System.IO.Path]::Combine()](learn.microsoft.com/en-us/dotnet/api/…) to concatenate file paths. Using either will ensure the path you construct is syntactically correct. (you don't 'miss' a backslash by mistake)
1

You can try this

copy-Item $SourceDir $DestinationDir -recurse

1 Comment

This worked smoothly for me and is the simplest solution here. I simply put the full paths starting from the root and used wildcards in the source path as needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.