How to properly -filter multiple strings in a PowerShell copy script -


i using powershell script this answer file copy. problem arises when want include multiple file types using filter.

get-childitem $originalpath -filter "*.htm"  | `    foreach{ $targetfile = $htmpath + $_.fullname.substring($originalpath.length); `   new-item -itemtype file -path $targetfile -force;  `  copy-item $_.fullname -destination $targetfile } 

works dream. however, problem arises when want include multiple file types using filter.

get-childitem $originalpath `    -filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*")  | `    foreach{ $targetfile = $htmpath + $_.fullname.substring($originalpath.length); `   new-item -itemtype file -path $targetfile -force;  `  copy-item $_.fullname -destination $targetfile } 

gives me following error:

get-childitem : cannot convert 'system.object[]' type 'system.string' required parameter 'filter'. specified method not supported. @ f:\data\foo\cgm.ps1:121 char:36 + get-childitem $originalpath -filter <<<<  "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*" | `     + categoryinfo          : invalidargument: (:) [get-childitem], parameterbindingexception     + fullyqualifiederrorid : cannotconvertargument,microsoft.powershell.commands.getchilditemcommand 

i have various iterations of parentheses, no parentheses, -filter, -include, defining inclusions variable (e.g., $filefilter) , each time above error, , pointing whatever follows -filter.

the interesting exception when code -filter "*.gif,*.jpg,*.xls*,*.doc*,*.pdf*,*.wav*,*.ppt*". there no errors, , no results , nothing console. suspect i've inadvertently coded impicit and statement?

so doing wrong, , how can correct it?

-filter accepts single string. -include accepts multiple values, qualifies -path argument. trick append \* end of path, , use -include select multiple extensions. btw, quoting strings unnecessary in cmdlet arguments unless contain spaces or shell special characters.

get-childitem $originalpath\* -include *.gif, *.jpg, *.xls*, *.doc*, *.pdf*, *.wav*, .ppt* 

note work regardless of whether $originalpath ends in backslash, because multiple consecutive backslashes interpreted single path separator. example, try:

get-childitem c:\\\\\windows 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -