3

I am working on a script that modifies images in a folder, for the most part, it is done I use Get-ChildItem with the directory.

So what I am currently doing is making a variable called

$fileExtension = [IO.Path]::GetExtension($files[$counter].Name)

with an if statement that is like:

if($fileExtension -eq '.png' or
    $fileExtensiono -eq '.jpg'. . . )

Is there a better way of doing this? I really want it to execute this function that modifies images if the file is an image, a folder could contain other items such as a zip which will crash the script if it picks up on it.

4
  • If ('.png', '.jpg', ... -Contains $fileExtention) {... Commented Aug 30, 2019 at 16:41
  • Now I did find an issue doing it that way, the thing that I am doing to these images is adding a number to the image itself. So Where I am doing Get-ChildItem I think I need it to just give me images. So I found that by using -Recusive and -Include @("*.png", etc.) works but if the folder has sub folders it crashes the script. How to I use the Get-ChildItem to just focus on the current directory only. Excluding Recursive the Include does not work. Commented Aug 30, 2019 at 16:44
  • I'd restrict to desired extensions firsthand when doing the Get-ChildItem *|Where-Object Extension -in '.png','.jpg' or Get-ChildItem * -include '*.png','*.jpg' Commented Aug 30, 2019 at 16:44
  • @LotPings - That was it! Where-Object was the solution ^-^ So if it has a file type other than common image file types it doesn't list them at all which allows the count to stay correct. Thank you so much! Commented Aug 30, 2019 at 16:49

1 Answer 1

3

try Something like this:

get-childitem 'c:\temp\*' -file -include '*.png', '*.jpg'
Sign up to request clarification or add additional context in comments.

Comments

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.