check multiple files in folder and subfolders

Clinton van Axel 126 Reputation points
2021-02-23T22:09:01.95+00:00

Hi all,

Is it possible to check for multiple files in folders and subfolders.
After the check do a copy.

I already made the copy part.

Get-Childitem "C:\Temp\*" -Include "test3.txt","test8.txt","test1.txt","test9.txt" -Recurse | foreach-object{Copy-Item -Path $_ -Destination "Q:\" -PassThru -recurse -Force}

I need help with the if part and how to use the test-path.

Windows for business | Windows Server | User experience | PowerShell
{count} votes

7 answers

Sort by: Most helpful
  1. Siu Yip 1 Reputation point
    2022-07-14T17:40:57.593+00:00

    similar to this command" dir test.txt /s /p

    0 comments No comments

  2. Andreas Baumgarten 129.5K Reputation points MVP Volunteer Moderator
    2022-07-14T19:24:41.54+00:00

    Hi @Siu Yip ,

    here we go:

    $filename = "2.txt"  
    $rootfolder = "./Junk"  
    Get-ChildItem -Path $rootfolder -Directory -Recurse | ForEach-Object {  
        Write-Host "Checking folder $($_.FullName) for $filename" -ForegroundColor Yellow  
        if (Test-Path -Path "$($_.Fullname)/$filename" -PathType Leaf) {  
            Write-Host "File $filename exists in $($_.FullName)" -ForegroundColor Red  
        }  
        else { Write-Host "File $filename does not exist in $($_.FullName)" -ForegroundColor Blue }  
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.