locked
issue in using foreach loop and break , unable to count the *.temp file within a subfolder RRS feed

  • Question

  • i want to loop through subfolders and fetch the file name with a .temp extension and when i use foreach loop, it didnt work.

    how to use foreach to iterate through subfolders and find the existence of a .temp file 

    $folders = get-childitem - directory -path $mysharedfolder

    foreach($folder in $folders)

    {

    #found the file with *.temp then break / exit the loop

    }


    Das


    • Edited by DADasRF Thursday, July 23, 2020 9:58 AM
    Thursday, July 23, 2020 9:57 AM

All replies

  • What doesn't work? You can't break out of the 'foreach', or you don't find any files with a '.temp' suffix, or you don't find all the folders in the directory hierarchy?

    It doesn't help that you provided no code in the part of your script where you say you have a problem!


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, July 23, 2020 3:07 PM
  • Additionaly to Rich question, would be great if you will explain what you need todo, because logic in your words is a litle bit complicated.

    What you said could be done in 1 line without the loop

    gci $yourSharedFolder -file *temp -recurse | select -first 1


    The opinion expressed by me is not an official position of Microsoft

    Thursday, July 23, 2020 3:43 PM
  • Hi,
    As this thread has been quiet for a while, so here is a quick question.Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, July 28, 2020 7:25 AM