0
votes

I need to move set of specific files having different extension to another folder.

I have following filtered files in the directory.

file1.txt
file2.xml
file3.dll

I have kept the above files in the variable $files and I need to move each of files to another folder.

Below is the code I tried.

foreach ($fileType in $files) {
    Get-ChildItem -Path C:\Files -Filter "$fileType*." -Recurse |
        Move-Item -Destination C:\Dest
}

I am getting following error

Get-ChildItem : Illegal characters in path.
At line:1 char:38
+ ... lude_files){Get-ChildItem -Path C:\Files

Appreciate if anyone can help on this?

2
The code and sample data you posted wouldn't throw that error. Please create a minimal reproducible example that demonstrates your problem, test-run that code to ensure that it actually does demonstrate the problem, then edit your question and copy/paste that code as well as the full error thrown by that code.Ansgar Wiechers
What is $filetype?js2010
@js2010 The loop variable.Ansgar Wiechers
We haven't heard from you.. Did any of the given answers solve your problem? If so, please consider accepting it by clicking ✓ on the left. This will help others with a similar question finding it more easily.Theo

2 Answers

0
votes

An easy way to do this

ls C:\files | Foreach {
        Move-Item -Path C:\files\$filetype -Destination C:\dest
}
0
votes

If all the files you are after share a common 'starts-with' name like file as in your example, the below should do what you want. It uses the -Include parameter where you can add an array of (in this case) extensions to look for.

Get-ChildItem -Path 'C:\Files' -Filter 'file*' -Include '*.txt','*.xml','*.dll' -Recurse |
    Move-Item -Destination 'C:\Dest'

Note: the -Include parameter only works when also used together with the -Recurse switch, OR by appending \* after the path (like in C:\Files\*)